tree-sitter / tree-sitter-typescript

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

Fix type_query precedence #213

Closed nmote closed 2 years ago

nmote commented 2 years ago

The precedence for type_query needs to be higher than that of _type_query_subscript_expression. Otherwise, the parser will generate incorrect parses, and even fail to parse valid code in some cases.

The code type X1 = typeof Y[keyof typeof Z]; should actually have typeof Y grouped together. I verified this in astexplorer.net, and I verified that TypeScript itself does parse this line. Currently, it fails to parse using this tree-sitter grammar. Because of the incorrect precedence, it expects an expression between the brackets instead of a type.

I also used AST Explorer to check how TypeScript parses the other tests that changed. In both cases, the updated parse is correct.

Checklist:

nmote commented 2 years ago

I've updated it to use the new precedences mechanism

nmote commented 2 years ago

In fact, I think we might be able to get rid of _type_query_subscript_expression altogether...