tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
314 stars 108 forks source link

Private property "in" object parsing error #266

Closed helixbass closed 8 months ago

helixbass commented 8 months ago

The following piece of code is valid but it is parsed incorrectly:

class C { #x; static foo(bar) { return #x in bar; } }

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

https://www.typescriptlang.org/play?#code/MYGwhgzhAEDC0G9oGIAeBuaEAuZsEthoAzAe1IAoAjMAJwEpFpaBTbAV1oDsVVp8eNWpgC+0EUA

The output of tree-sitter parse is the following:

(program [0, 0] - [1, 0]
  (class_declaration [0, 0] - [0, 53]
    name: (identifier [0, 6] - [0, 7])
    body: (class_body [0, 8] - [0, 53]
      member: (field_definition [0, 10] - [0, 12]
        property: (private_property_identifier [0, 10] - [0, 12]))
      member: (method_definition [0, 14] - [0, 51]
        name: (property_identifier [0, 21] - [0, 24])
        parameters: (formal_parameters [0, 24] - [0, 29]
          (identifier [0, 25] - [0, 28]))
        body: (statement_block [0, 30] - [0, 51]
          (return_statement [0, 32] - [0, 49]
            (ERROR [0, 39] - [0, 44]
              (private_property_identifier [0, 39] - [0, 41]))
            (identifier [0, 45] - [0, 48])))))))
/Users/jrosse/prj/tmp-js/tmp.js 0 ms    (ERROR [0, 39] - [0, 44])

It looks like this should parse the #x in bar part like:

(binary_expression
  left: (private_property_identifier)
  operator: "in"
  right: (identifier)
)

From the MDN docs:

You can use the in operator to check whether an externally defined object possesses a private property. This will return true if the private field or method exists, and false otherwise.

amaanq commented 8 months ago

Thanks!

helixbass commented 8 months ago

I see this parsing correctly now, thanks