Skip to content

Commit 0a90858

Browse files
committed
Fix type_query precedence
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.
1 parent bd6912a commit 0a90858

File tree

6 files changed

+222770
-224137
lines changed

6 files changed

+222770
-224137
lines changed

common/corpus/types.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ type T = typeof array[number];
767767
(type_alias_declaration (type_identifier)
768768
(intersection_type (index_type_query (type_identifier)) (type_identifier)))
769769
(type_alias_declaration (type_identifier)
770-
(type_query (subscript_expression (identifier) (predefined_type)))))
770+
(lookup_type (type_query (identifier)) (predefined_type))))
771771

772772
=======================================
773773
Lookup types
@@ -1318,9 +1318,10 @@ type T = Foo<Bar<typeof bar["baz"]>>
13181318
(generic_type
13191319
(type_identifier)
13201320
(type_arguments
1321-
(type_query
1322-
(subscript_expression
1323-
(identifier)
1321+
(lookup_type
1322+
(type_query
1323+
(identifier))
1324+
(literal_type
13241325
(string
13251326
(string_fragment))))))))))
13261327

@@ -1422,11 +1423,12 @@ type X2 = (typeof Y)[keyof typeof Z];
14221423
(comment)
14231424
(type_alias_declaration
14241425
(type_identifier)
1425-
(type_query
1426-
(identifier))
1427-
(ERROR
1428-
(identifier)
1429-
(identifier)))
1426+
(lookup_type
1427+
(type_query
1428+
(identifier))
1429+
(index_type_query
1430+
(type_query
1431+
(identifier)))))
14301432
(type_alias_declaration
14311433
(type_identifier)
14321434
(lookup_type

common/define-grammar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = function defineGrammar(dialect) {
4545
[$.readonly_type, $.pattern],
4646
[$.readonly_type, $.primary_expression],
4747
[$.type_query, $.subscript_expression, $.expression],
48+
[$.type_query, $._type_query_subscript_expression],
4849
[$.nested_type_identifier, $.generic_type, $._primary_type, $.lookup_type, $.index_type_query, $._type],
4950
[$.as_expression, $._primary_type],
5051
[$._type_query_member_expression, $.member_expression],

tsx/src/grammar.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10814,6 +10814,16 @@
1081410814
"name": "expression"
1081510815
}
1081610816
],
10817+
[
10818+
{
10819+
"type": "SYMBOL",
10820+
"name": "type_query"
10821+
},
10822+
{
10823+
"type": "SYMBOL",
10824+
"name": "_type_query_subscript_expression"
10825+
}
10826+
],
1081710827
[
1081810828
{
1081910829
"type": "SYMBOL",

0 commit comments

Comments
 (0)