Skip to content

Commit caed1a3

Browse files
committed
disambiguate between call expressions with subscript type parameters and binary expressions between subscript expressions
1 parent 2ac62b3 commit caed1a3

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

common/corpus/functions.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Function calls with optional chaining and type arguments
7979
==================================
8080

8181
A?.<B>();
82+
A<B[]>();
8283

8384
---
8485

@@ -87,6 +88,11 @@ A?.<B>();
8788
(call_expression
8889
(identifier)
8990
(type_arguments (type_identifier))
91+
(arguments)))
92+
(expression_statement
93+
(call_expression
94+
(identifier)
95+
(type_arguments (array_type (type_identifier)))
9096
(arguments))))
9197

9298
==================================

common/define-grammar.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ module.exports = function defineGrammar(dialect) {
4141
]),
4242

4343
conflicts: ($, previous) => previous.concat([
44+
[$._expression, $.call_expression],
45+
[$._expression, $.arrow_function, $.call_expression],
46+
[$._property_name, $.arrow_function, $.call_expression],
47+
[$._property_name, $._expression, $.arrow_function, $.call_expression],
48+
[$._property_name, $.call_expression],
49+
[$._expression, $._property_name, $.call_expression],
50+
[$._expression, $.call_expression, $.new_expression, $.member_expression, $.subscript_expression],
51+
[$._expression, $.call_expression, $.generic_type],
52+
53+
[$._expression, $.call_expression, $.new_expression],
54+
[$._expression, $.new_expression],
55+
[$._expression, $.arrow_function, $.call_expression, $.new_expression],
56+
[$.call_expression, $.new_expression],
57+
[$.arrow_function, $.new_expression, $.call_expression],
58+
4459
[$.call_expression, $.binary_expression],
4560
[$.call_expression, $.binary_expression, $.unary_expression],
4661

@@ -104,25 +119,14 @@ module.exports = function defineGrammar(dialect) {
104119
optional($._initializer)
105120
),
106121

107-
call_expression: ($, previous) => choice(
108-
prec(PREC.CALL, seq(
109-
field('function', $._expression),
110-
field('type_arguments', optional($.type_arguments)),
111-
field('arguments', choice($.arguments, $.template_string))
112-
)),
113-
prec(PREC.MEMBER, seq(
114-
field('function', $._primary_expression),
115-
'?.',
116-
field('type_arguments', optional($.type_arguments)),
117-
field('arguments', $.arguments)
118-
))
122+
call_expression: $ => choice(
123+
callExpression($)(PREC.CALL),
124+
callExpression($)(PREC.MEMBER, '?.'),
119125
),
120126

121-
new_expression: $ => prec.right(PREC.NEW, seq(
127+
new_expression: $ => prec.dynamic(PREC.NEW, seq(
122128
'new',
123-
field('constructor', $._primary_expression),
124-
field('type_arguments', optional($.type_arguments)),
125-
field('arguments', optional($.arguments))
129+
callExpression($)(PREC.NEW)
126130
)),
127131

128132
_augmented_assignment_lhs: ($, previous) => choice(previous, $.non_null_expression),
@@ -792,6 +796,19 @@ module.exports = function defineGrammar(dialect) {
792796
});
793797
}
794798

799+
function callExpression ($) {
800+
return function (precedence, punctuation) {
801+
return seq(
802+
...[
803+
field('function', $._primary_expression),
804+
...(punctuation ? [punctuation] : []),
805+
field('type_arguments', prec.dynamic(precedence, optional($.type_arguments))),
806+
field('arguments', choice($.arguments, $.template_string))
807+
]
808+
)
809+
}
810+
}
811+
795812
function commaSep1 (rule) {
796813
return sepBy1(',', rule);
797814
}

0 commit comments

Comments
 (0)