tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
341 stars 104 forks source link

export = 0 #197

Closed sguillia closed 2 years ago

sguillia commented 2 years ago

Hi!

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

export = 0

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript: https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAXjgBiA

The output of tree-sitter parse is the following (tree-sitter version 0.20.0, grammar at 111b07762e86efab9a918b7c721f720c37e76b0a):

(program [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 10]
    (assignment_expression [0, 0] - [0, 10]
      left: (identifier [0, 0] - [0, 6])
      right: (number [0, 9] - [0, 10]))))

I honestly expected the parser to return an export_statement instead of an expression_statement:

https://github.com/tree-sitter/tree-sitter-typescript/blob/111b07762e86efab9a918b7c721f720c37e76b0a/common/define-grammar.js#L277

It seems like the precedence of that line in the grammar is incorrect

In Javascript, it is indeed an expression_statement In Typescript, it should be an export_statement as per documentation: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

mjambon commented 2 years ago

thanks for the report!