tree-sitter-grammars / tree-sitter-hcl

HCL grammar for tree-sitter
https://tree-sitter-grammars.github.io/tree-sitter-hcl/
Apache License 2.0
92 stars 20 forks source link

Incorrect parsing of unary expression on variable_expression #56

Open MXfive opened 3 weeks ago

MXfive commented 3 weeks ago

Describe the bug

Incorrect parsing leading to missing highlights of variables within unary operations.

To Reproduce

======
Unary operation on variable with additional expression
=====

foo = !var.bar

------

(config_file
  (body
    (attribute
      (identifier)
      (expression
        (operation
          (unary_operation
            (expression
              (variable_expr
                (identifier))
              (get_attr
                (identifier)))))))))

Expected behavior

The test case should pass

Screenshots

Screenshot 2024-08-27 at 11 46 38
MXfive commented 2 weeks ago

There is also a similar issue with binary_operation.

=====
attribute as conditional expression
=====

foo = local.bar == local.baz

-----

(config_file
  (body
    (attribute
      (identifier)
      (expression
        (operation
          (binary_operation
            (variable_expr
              (identifier))
            (get_attr
              (identifier))
            (variable_expr
              (identifier))
            (get_attr
              (identifier))))))))

Screenshot 2024-08-27 at 21 38 54

Looking a the behaviour of binary_operation, I think the proper way of resolving that would be something like in tree-sitter-go, where each side of the binary operation is set as field, rather than just a normal child node. This also maps to unary_operation quite nicely too.

Also would be a nice addition to capture the operator as a field also.