tree-sitter-javascript icon indicating copy to clipboard operation
tree-sitter-javascript copied to clipboard

Incorrect parsing when keywords which are valid variable names are involved

Open NotWearingPants opened this issue 3 years ago • 0 comments

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

let + 5

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

The output of tree-sitter from the playground is the following:

program [0, 0] - [1, 0]
  ERROR [0, 0] - [0, 3]
  expression_statement [0, 4] - [0, 7]
    unary_expression [0, 4] - [0, 7]
      argument: number [0, 6] - [0, 7]

This should be an expression_statement of a binary_expression that adds the identifier let to the constant 5.


for (let in {});

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript Even TypeScript is wrong sometimes, you can run the code in the browser or Node.js to verify it's valid.

The output of tree-sitter from the playground is the following:

program [0, 0] - [1, 0]
  ERROR [0, 0] - [0, 16]
    lexical_declaration [0, 5] - [0, 16]
      ERROR [0, 9] - [0, 11]
        identifier [0, 9] - [0, 11]
      variable_declarator [0, 12] - [0, 14]
        name: object_pattern [0, 12] - [0, 14]
      ERROR [0, 14] - [0, 15]```

The expected result is a for_in_statement, like what we get when we change let to letz or something else.


Specifically let and async are valid variable names. const/class are not, which are handled correctly.

By the way this test is from babel's test suite, maybe you should run their tests as well.

NotWearingPants avatar Sep 28 '21 07:09 NotWearingPants