tree-sitter / tree-sitter-typescript

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

Fix typeof according to TypeScript rules #183

Closed resolritter closed 2 years ago

resolritter commented 2 years ago

type_query is right now defined as follows:

https://github.com/tree-sitter/tree-sitter-typescript/blob/74a2748896434c13cac7697c4d182b43ec170d30/common/define-grammar.js#L660

typeof generic_type is not valid TypeScript syntax and does not make sense.

Using primary_expression causes the parsing errors described in https://github.com/tree-sitter/tree-sitter-typescript/issues/171. I found that it's possible to work around that by defining stricter variants of call_expression, subscript_expression and member_expression according to what I tried in the TypeScript playground. This is more restrictive than allowing all primary expressions like done right now, but it seems to work, so I'm submitting the PR here in case it's useful.


Initial measurements of parser.c against 74a2748896434c13cac7697c4d182b43ec170d30

./typescript/src/parser.c

-#define STATE_COUNT 6031
-#define LARGE_STATE_COUNT 1270
+#define STATE_COUNT 4209
+#define LARGE_STATE_COUNT 708

./tsx/src/parser.c

-#define STATE_COUNT 6336
-#define LARGE_STATE_COUNT 1271
+#define STATE_COUNT 4498
+#define LARGE_STATE_COUNT 722

Checklist:

closes https://github.com/tree-sitter/tree-sitter-typescript/issues/171

mjambon commented 2 years ago

wow, that's a major reduction in parser.c:

145,406 additions, 282,644 deletions

mjambon commented 2 years ago

looks great to me, merging.