Skip to content

Commit 4f8844a

Browse files
committed
disambiguate between call expressions with subscript type parameters and binary expressions between subscript expressions
1 parent d7965b8 commit 4f8844a

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
@@ -36,6 +36,21 @@ module.exports = function defineGrammar(dialect) {
3636
]),
3737

3838
conflicts: ($, previous) => previous.concat([
39+
[$._expression, $.call_expression],
40+
[$._expression, $.arrow_function, $.call_expression],
41+
[$._property_name, $.arrow_function, $.call_expression],
42+
[$._property_name, $._expression, $.arrow_function, $.call_expression],
43+
[$._property_name, $.call_expression],
44+
[$._expression, $._property_name, $.call_expression],
45+
[$._expression, $.call_expression, $.new_expression, $.member_expression, $.subscript_expression],
46+
[$._expression, $.call_expression, $.generic_type],
47+
48+
[$._expression, $.call_expression, $.new_expression],
49+
[$._expression, $.new_expression],
50+
[$._expression, $.arrow_function, $.call_expression, $.new_expression],
51+
[$.call_expression, $.new_expression],
52+
[$.arrow_function, $.new_expression, $.call_expression],
53+
3954
[$.call_expression, $.binary_expression],
4055
[$.call_expression, $.binary_expression, $.unary_expression],
4156

@@ -93,25 +108,14 @@ module.exports = function defineGrammar(dialect) {
93108
optional($._initializer)
94109
),
95110

96-
call_expression: ($, previous) => choice(
97-
prec(PREC.CALL, seq(
98-
field('function', $._expression),
99-
field('type_arguments', optional($.type_arguments)),
100-
field('arguments', choice($.arguments, $.template_string))
101-
)),
102-
prec(PREC.MEMBER, seq(
103-
field('function', $._primary_expression),
104-
'?.',
105-
field('type_arguments', optional($.type_arguments)),
106-
field('arguments', $.arguments)
107-
))
111+
call_expression: $ => choice(
112+
callExpression($)(PREC.CALL),
113+
callExpression($)(PREC.MEMBER, '?.'),
108114
),
109115

110-
new_expression: $ => prec.right(PREC.NEW, seq(
116+
new_expression: $ => prec.dynamic(PREC.NEW, seq(
111117
'new',
112-
field('constructor', $._primary_expression),
113-
field('type_arguments', optional($.type_arguments)),
114-
field('arguments', optional($.arguments))
118+
callExpression($)(PREC.NEW)
115119
)),
116120

117121
// If the dialect is regular typescript, we exclude JSX expressions and
@@ -726,6 +730,19 @@ module.exports = function defineGrammar(dialect) {
726730
});
727731
}
728732

733+
function callExpression ($) {
734+
return function (precedence, punctuation) {
735+
return seq(
736+
...[
737+
field('function', $._primary_expression),
738+
...(punctuation ? [punctuation] : []),
739+
field('type_arguments', prec.dynamic(precedence, optional($.type_arguments))),
740+
field('arguments', choice($.arguments, $.template_string))
741+
]
742+
)
743+
}
744+
}
745+
729746
function commaSep1 (rule) {
730747
return sepBy1(',', rule);
731748
}

0 commit comments

Comments
 (0)