Skip to content

bug: tree-sitter parses complex declaration as pseudo-class instead of CSS custom property #7

@kensternberg-authentik

Description

@kensternberg-authentik

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.24.5

Describe the bug

In the following Sass rule_set.block:

.#{$backdrop} {
  --#{$backdrop}--Position: fixed;
  --#{$backdrop}--ZIndex: var(--#{$pf-global}--ZIndex--lg);
}

tree-sitter-scss correctly parses the first declaration as property name. It ERRORs on the second declaration, and claims its best-fit is as a pseudo_class_selector.

I believe part of the confusion comes from an inappropriate declaration in tree-sitter-css, namely this one:

    _selector: $ => choice(
      $.universal_selector,
      alias($.identifier, $.tag_name),
      $.class_selector,

// ... much later....

    identifier: _ => /(--|-?[a-zA-Z_\xA0-\xFF])[a-zA-Z0-9-_\xA0-\xFF]*/,

To the best of my knowledge, a tag_name cannot start with the -- sequence, yet here it's allowed, creating an ambiguity. Unfortunately, the confusion of tag_name with identifier goes pretty deep and recursive in the definition of _selector, leading to the problem.

Steps To Reproduce/Bad Parse Tree

  1. Attempt to parse this file with $ tree-sitter parse file.scss:
.#{$backdrop} {
  --#{$backdrop}--Position: fixed;
  --#{$backdrop}--ZIndex: var(--#{$pf-global}--ZIndex--lg);
}
  1. See this message:
      (ERROR [2, 2] - [2, 59]
        (pseudo_class_selector [2, 2] - [2, 58]
          (tag_name [2, 2] - [2, 24]
            (identifier [2, 2] - [2, 4])
            (interpolation [2, 4] - [2, 16]

Expected Behavior/Parse Tree

I would expect the second declaration to be correctly interpreted as a declaration.

Repro

The example is given above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions