Skip to content

Commit 8106351

Browse files
committed
fix: allow type queries in type annotations and type arguments in
special scenarios
1 parent a4d3032 commit 8106351

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

common/define-grammar.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = function defineGrammar(dialect) {
4848
[$.nested_type_identifier, $.generic_type, $._primary_type, $.lookup_type, $.index_type_query, $._type],
4949
[$.as_expression, $.satisfies_expression, $._primary_type],
5050
[$._type_query_member_expression, $.member_expression],
51+
[$.member_expression, $._type_query_member_expression_in_type_annotation],
5152
[$._type_query_member_expression, $.primary_expression],
5253
[$._type_query_subscript_expression, $.subscript_expression],
5354
[$._type_query_subscript_expression, $.primary_expression],
@@ -58,10 +59,11 @@ module.exports = function defineGrammar(dialect) {
5859
[$.decorator_call_expression, $.decorator],
5960
[$.literal_type, $.pattern],
6061
[$.predefined_type, $.pattern],
61-
[$._primary_type, $._type_query_subscript_expression],
62-
[$.nested_type_identifier, $._type_query_member_expression],
63-
[$.nested_identifier, $._type_query_member_expression],
64-
[$.generic_type, $._type_query_instantiation_expression],
62+
[$.call_expression, $._type_query_call_expression],
63+
[$.call_expression, $._type_query_call_expression_in_type_annotation],
64+
[$.new_expression, $.primary_expression],
65+
[$.meta_property, $.primary_expression],
66+
[$.construct_signature, $._property_name],
6567
]),
6668

6769
conflicts: ($, previous) => previous.concat([
@@ -635,13 +637,37 @@ module.exports = function defineGrammar(dialect) {
635637
':',
636638
choice(
637639
$._type,
638-
alias($._type_query_subscript_expression, $.subscript_expression),
639-
alias($._type_query_member_expression, $.member_expression),
640-
alias($._type_query_call_expression, $.call_expression),
641-
alias($._type_query_instantiation_expression, $.instantiation_expression),
640+
alias($._type_query_member_expression_in_type_annotation, $.member_expression),
641+
alias($._type_query_call_expression_in_type_annotation, $.call_expression),
642642
)
643643
),
644644

645+
// Oh boy
646+
// The issue is these special type queries need a lower relative precedence than the normal ones,
647+
// since these are used in type annotations whereas the other ones are used where `typeof` is
648+
// required beforehand. This allows for parsing of annotations such as
649+
// foo: import('x').y.z;
650+
// but was a nightmare to get working.
651+
_type_query_member_expression_in_type_annotation: $ => seq(
652+
field('object', choice(
653+
$.import,
654+
alias($._type_query_member_expression_in_type_annotation, $.member_expression),
655+
alias($._type_query_call_expression_in_type_annotation, $.call_expression)
656+
)),
657+
'.',
658+
field('property', choice(
659+
$.private_property_identifier,
660+
alias($.identifier, $.property_identifier)
661+
))
662+
),
663+
_type_query_call_expression_in_type_annotation: $ => seq(
664+
field('function', choice(
665+
$.import,
666+
alias($._type_query_member_expression_in_type_annotation, $.member_expression),
667+
)),
668+
field('arguments', $.arguments)
669+
),
670+
645671
asserts: $ => seq(
646672
'asserts',
647673
choice($.type_predicate, $.identifier, $.this)
@@ -880,7 +906,14 @@ module.exports = function defineGrammar(dialect) {
880906
),
881907

882908
type_arguments: $ => seq(
883-
'<', commaSep1($._type), optional(','), '>'
909+
'<',
910+
commaSep1(choice(
911+
$._type,
912+
alias($._type_query_member_expression_in_type_annotation, $.member_expression),
913+
alias($._type_query_call_expression_in_type_annotation, $.call_expression)
914+
)),
915+
optional(','),
916+
'>'
884917
),
885918

886919
object_type: $ => seq(

0 commit comments

Comments
 (0)