1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use oxc_allocator::Box;
use oxc_ast::{NONE, ast::*};
use oxc_span::{FileExtension, GetSpan};
use oxc_syntax::precedence::Precedence;
use super::{FunctionKind, Tristate};
use crate::{Context, ParserConfig as Config, ParserImpl, diagnostics, lexer::Kind};
struct ArrowFunctionHead<'a> {
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
params: Box<'a, FormalParameters<'a>>,
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
r#async: bool,
span: u32,
}
impl<'a, C: Config> ParserImpl<'a, C> {
pub(super) fn try_parse_parenthesized_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
match self.is_parenthesized_arrow_function_expression() {
Tristate::False => None,
Tristate::True => Some(self.parse_parenthesized_arrow_function_expression(
allow_return_type_in_arrow_function,
)),
Tristate::Maybe => self.parse_possible_parenthesized_arrow_function_expression(
allow_return_type_in_arrow_function,
),
}
}
pub(super) fn try_parse_async_simple_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
if self.at(Kind::Async) && self.is_un_parenthesized_async_arrow_function_worker() {
let span = self.start_span();
self.bump_any(); // bump `async`
let expr = self.parse_binary_expression_or_higher(Precedence::Comma);
let Expression::Identifier(ident) = &expr else {
return self.unexpected();
};
// It is a Syntax Error if ArrowParameters Contains AwaitExpression is true.
if ident.name == "await" {
self.error(diagnostics::identifier_async("await", ident.span));
}
return Some(self.parse_simple_arrow_function_expression(
span,
ident,
/* async */ true,
allow_return_type_in_arrow_function,
));
}
None
}
fn is_parenthesized_arrow_function_expression(&mut self) -> Tristate {
match self.cur_kind() {
Kind::LParen | Kind::LAngle | Kind::Async => {
self.lookahead(Self::is_parenthesized_arrow_function_expression_worker)
}
_ => Tristate::False,
}
}
fn is_parenthesized_arrow_function_expression_worker(&mut self) -> Tristate {
if self.eat(Kind::Async) {
if self.cur_token().is_on_new_line() {
return Tristate::False;
}
let kind = self.cur_kind();
if kind != Kind::LParen && kind != Kind::LAngle {
return Tristate::False;
}
}
let first = self.cur_kind();
self.bump_any();
let second = self.cur_kind();
match first {
Kind::LParen => {
match second {
// Simple cases: "() =>", "(): ", and "() {".
// This is an arrow function with no parameters.
// The last one is not actually an arrow function,
// but this is probably what the user intended.
Kind::RParen => {
self.bump_any();
let third = self.cur_kind();
match third {
Kind::Colon if self.is_ts => Tristate::Maybe,
Kind::Arrow | Kind::LCurly => Tristate::True,
_ => Tristate::False,
}
}
// If encounter "([" or "({", this could be the start of a binding pattern.
// Examples:
// ([ x ]) => { }
// ({ x }) => { }
// ([ x ])
// ({ x })
Kind::LBrack | Kind::LCurly => Tristate::Maybe,
// Simple case: "(..."
// This is an arrow function with a rest parameter.
Kind::Dot3 => {
self.bump_any();
let third = self.cur_kind();
match third {
// '(...ident' is a lambda
Kind::Ident => Tristate::True,
// '(...null' is not a lambda
kind if kind.is_literal() => Tristate::False,
_ => Tristate::Maybe,
}
}
_ => {
self.bump_any();
let third = self.cur_kind();
// Check for "(xxx yyy", where xxx is a modifier and yyy is an identifier. This
// isn't actually allowed, but we want to treat it as a lambda so we can provide
// a good error message.
if second.is_modifier_kind()
&& second != Kind::Async
&& third.is_binding_identifier()
{
if third == Kind::As {
return Tristate::False; // https://github.com/microsoft/TypeScript/issues/44466
}
return Tristate::True;
}
// If we had "(" followed by something that's not an identifier,
// then this definitely doesn't look like a lambda. "this" is not
// valid, but we want to parse it and then give a semantic error.
if !second.is_binding_identifier() && second != Kind::This {
return Tristate::False;
}
match third {
// If we have something like "(a:", then we must have a
// type-annotated parameter in an arrow function expression.
Kind::Colon => Tristate::True,
// If we have "(a?:" or "(a?," or "(a?=" or "(a?)" then it is definitely a lambda.
Kind::Question => {
self.bump_any();
let fourth = self.cur_kind();
if matches!(
fourth,
Kind::Colon | Kind::Comma | Kind::Eq | Kind::RParen
) {
return Tristate::True;
}
Tristate::False
}
// If we have "(a," or "(a=" or "(a)" this *could* be an arrow function
Kind::Comma | Kind::Eq | Kind::RParen => Tristate::Maybe,
// It is definitely not an arrow function
_ => Tristate::False,
}
}
}
}
Kind::LAngle => {
// If we have "<" not followed by an identifier,
// then this definitely is not an arrow function.
if !second.is_binding_identifier() && second != Kind::Const {
return Tristate::False;
}
// JSX overrides
if self.source_type.is_jsx() {
// <const Ident extends Ident>
// ^^^^^ Optional
self.bump(Kind::Const);
self.bump_any();
let third = self.cur_kind();
return match third {
Kind::Extends => {
self.bump_any();
let fourth = self.cur_kind();
if matches!(fourth, Kind::Eq | Kind::RAngle | Kind::Slash) {
Tristate::False
} else if fourth.is_binding_identifier() {
Tristate::Maybe
} else {
Tristate::True
}
}
Kind::Eq | Kind::Comma => Tristate::True,
_ => Tristate::False,
};
}
Tristate::Maybe
}
_ => unreachable!(),
}
}
fn is_un_parenthesized_async_arrow_function_worker(&mut self) -> bool {
// Use lookahead to avoid checkpoint/rewind
self.lookahead(|parser| {
parser.bump(Kind::Async);
// If the "async" is followed by "=>" token then it is not a beginning of an async arrow-function
// but instead a simple arrow-function which will be parsed inside "parseAssignmentExpressionOrHigher"
if !parser.cur_token().is_on_new_line() && parser.cur_kind().is_binding_identifier() {
// Arrow before newline is checked in `parse_simple_arrow_function_expression`
parser.bump_any();
parser.at(Kind::Arrow)
} else {
false
}
})
}
pub(crate) fn parse_simple_arrow_function_expression(
&mut self,
span: u32,
ident: &IdentifierReference<'a>,
r#async: bool,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let pattern = BindingPattern::BindingIdentifier(
self.ast.alloc_binding_identifier(ident.span, ident.name),
);
let formal_parameter = self.ast.plain_formal_parameter(ident.span, pattern);
let params = self.ast.alloc_formal_parameters(
ident.span,
FormalParameterKind::ArrowFormalParameters,
self.ast.vec1(formal_parameter),
NONE,
);
if self.cur_token().is_on_new_line() {
self.error(diagnostics::lineterminator_before_arrow(self.cur_token().span()));
}
self.expect(Kind::Arrow);
self.parse_arrow_function_expression_body(
ArrowFunctionHead { type_parameters: None, params, return_type: None, r#async, span },
allow_return_type_in_arrow_function,
)
}
fn parse_parenthesized_arrow_function_head(&mut self) -> ArrowFunctionHead<'a> {
let span = self.start_span();
let r#async = self.eat(Kind::Async);
let has_await = self.ctx.has_await();
self.ctx = self.ctx.union_await_if(r#async);
let (type_parameters, has_trailing_comma) =
self.parse_ts_type_parameters_with_trailing_comma();
if let Some(type_params) = &type_parameters
&& matches!(self.source_type.extension(), Some(FileExtension::Mts | FileExtension::Cts))
&& type_params.params.len() == 1
&& type_params.params[0].constraint.is_none()
&& !has_trailing_comma
{
self.error(diagnostics::jsx_type_parameter_in_mts_cts(type_params.params[0].name.span));
}
let (this_param, params) = self.parse_formal_parameters(
FunctionKind::Expression,
FormalParameterKind::ArrowFormalParameters,
);
if let Some(this_param) = this_param {
// const x = (this: number) => {};
self.error(diagnostics::ts_arrow_function_this_parameter(this_param.span));
}
let return_type = if self.is_ts { self.parse_ts_return_type_annotation() } else { None };
self.ctx = self.ctx.and_await(has_await);
if self.cur_token().is_on_new_line() {
self.error(diagnostics::lineterminator_before_arrow(self.cur_token().span()));
}
self.expect(Kind::Arrow);
ArrowFunctionHead { type_parameters, params, return_type, r#async, span }
}
/// [ConciseBody](https://tc39.es/ecma262/#prod-ConciseBody)
/// [lookahead ≠ {] `ExpressionBody`[?In, ~Await]
/// { `FunctionBody`[~Yield, ~Await] }
/// `ExpressionBody`[In, Await] :
/// `AssignmentExpression`[?In, ~Yield, ?Await]
fn parse_arrow_function_expression_body(
&mut self,
arrow_function_head: ArrowFunctionHead<'a>,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let ArrowFunctionHead { type_parameters, params, return_type, r#async, span } =
arrow_function_head;
let has_await = self.ctx.has_await();
let has_yield = self.ctx.has_yield();
self.ctx = self.ctx.and_await(r#async).and_yield(false);
let expression = !self.at(Kind::LCurly);
let body = if expression {
// Remove TopLevel context for arrow function expression body
let expr = self.context_remove(Context::TopLevel, |p| {
p.parse_assignment_expression_or_higher_impl(allow_return_type_in_arrow_function)
});
let span = expr.span();
let expr_stmt = self.ast.statement_expression(span, expr);
self.ast.alloc_function_body(span, self.ast.vec(), self.ast.vec1(expr_stmt))
} else {
self.parse_function_body()
};
self.ctx = self.ctx.and_await(has_await).and_yield(has_yield);
self.ast.expression_arrow_function(
self.end_span(span),
expression,
r#async,
type_parameters,
params,
return_type,
body,
)
}
/// Section [Arrow Function](https://tc39.es/ecma262/#sec-arrow-function-definitions)
/// `ArrowFunction`[In, Yield, Await] :
/// `ArrowParameters`[?Yield, ?Await] [no `LineTerminator` here] => `ConciseBody`[?In]
fn parse_parenthesized_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let head = self.parse_parenthesized_arrow_function_head();
self.parse_arrow_function_expression_body(head, allow_return_type_in_arrow_function)
}
fn parse_possible_parenthesized_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
let pos = self.cur_token().start();
if self.state.not_parenthesized_arrow.contains(&pos) {
return None;
}
let checkpoint = self.checkpoint_with_error_recovery();
let head = self.parse_parenthesized_arrow_function_head();
if self.has_fatal_error() {
self.state.not_parenthesized_arrow.insert(pos);
self.rewind(checkpoint);
return None;
}
let has_return_type = head.return_type.is_some();
let body =
self.parse_arrow_function_expression_body(head, allow_return_type_in_arrow_function);
// Given:
// x ? y => ({ y }) : z => ({ z })
// We try to parse the body of the first arrow function by looking at:
// ({ y }) : z => ({ z })
// This is a valid arrow function with "z" as the return type.
//
// But, if we're in the true side of a conditional expression, this colon
// terminates the expression, so we cannot allow a return type if we aren't
// certain whether or not the preceding text was parsed as a parameter list.
//
// For example,
// a() ? (b: number, c?: string): void => d() : e
// is determined by isParenthesizedArrowFunctionExpression to unambiguously
// be an arrow expression, so we allow a return type.
if !allow_return_type_in_arrow_function && has_return_type {
// However, if the arrow function we were able to parse is followed by another colon
// as in:
// a ? (x): string => x : null
// Then allow the arrow function, and treat the second colon as terminating
// the conditional expression. It's okay to do this because this code would
// be a syntax error in JavaScript (as the second colon shouldn't be there).
if !self.at(Kind::Colon) {
self.state.not_parenthesized_arrow.insert(pos);
self.rewind(checkpoint);
return None;
}
}
Some(body)
}
}