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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//! Arrow functions.
//!
//! `( … )` and `async ( … )` are parsed once with the cover grammars in [`super::cover`],
//! and refined to arrow function parameters if `=>` follows.
use std::{mem, num::NonZeroU32};
use oxc_allocator::{ArenaBox, ArenaVec};
use oxc_ast::ast::*;
use oxc_span::{FileExtension, Span};
use oxc_str::Ident;
use oxc_syntax::precedence::Precedence;
use super::{
FunctionKind, Tristate,
cover::{Cover, CoverElement, CoverIdentifier, CoverList},
operator::kind_to_precedence,
};
use crate::{
Context, ParserConfig as Config, ParserImpl, cursor::ParserCheckpoint, diagnostics, lexer::Kind,
};
struct ArrowFunctionHead<'a> {
type_parameters: Option<ArenaBox<'a, TSTypeParameterDeclaration<'a>>>,
params: ArenaBox<'a, FormalParameters<'a>>,
return_type: Option<ArenaBox<'a, TSTypeAnnotation<'a>>>,
r#async: bool,
start: u32,
}
impl<'a, C: Config> ParserImpl<'a, C> {
/// `CoverParenthesizedExpressionAndArrowParameterList`[Yield, Await] :
/// ( `Expression`[+In, ?Yield, ?Await] )
/// ( `Expression`[+In, ?Yield, ?Await] , )
/// ( )
/// ( ... `BindingIdentifier`[?Yield, ?Await] )
/// ( ... `BindingPattern`[?Yield, ?Await] )
/// ( `Expression`[+In, ?Yield, ?Await] , ... `BindingIdentifier`[?Yield, ?Await] )
/// ( `Expression`[+In, ?Yield, ?Await] , ... `BindingPattern`[?Yield, ?Await] )
///
/// Returns the expression, and whether it is an arrow function.
pub(super) fn parse_parenthesized_expression_or_arrow_function(
&mut self,
allow_return_type_in_arrow_function: bool,
no_side_effects_comments: Option<(u32, NonZeroU32)>,
) -> (Expression<'a>, bool) {
debug_assert!(self.at(Kind::LParen));
let start = self.cur_start();
self.bump_any(); // `(`
let first_start = self.cur_start();
let mut cover = Cover::new(
start,
start,
self.state.cover_elements.len(),
self.state.cover_parenthesized.len(),
/* is_async */ false,
);
let end = 'list: {
let kind = self.cur_kind();
let mut identifier = None;
if kind == Kind::Ident {
let ident = self.parse_cover_identifier();
// `(a)` and `(a) => …`
if self.at(Kind::RParen) {
match self.parse_parenthesized_identifier_or_arrow_function(
start,
ident,
allow_return_type_in_arrow_function,
) {
Ok(result) => return result,
Err(end) => break 'list end,
}
}
identifier = Some(ident);
}
// A first element that cannot be a `BindingElement` makes the list
// a parenthesized expression: `(a.b)`, `(a + b)`, `(!a)`, `(function () {})`,
// `(<div />)`.
let first_parsed = if identifier.is_some() {
Self::continues_identifier_expression(self.cur_kind())
} else {
Self::cannot_start_binding_element(kind)
};
if first_parsed {
match self.parse_parenthesized_expression_first_element(
cover,
identifier.take(),
no_side_effects_comments,
) {
Ok(expression) => return (expression, false),
Err(Some(end)) => break 'list end,
Err(None) => {}
}
}
match self.parse_cover_list(start, cover.base, false, first_parsed, identifier) {
CoverList::Parameters(params) => {
let arrow = self.parse_cover_parameters_arrow_function(
cover,
params,
allow_return_type_in_arrow_function,
);
return (arrow, true);
}
CoverList::Undecided { trailing_comma, end } => {
cover.trailing_comma = trailing_comma;
end
}
}
};
if self.fatal_error.is_some() {
return (self.abandon_cover(cover), false);
}
if let Some(arrow) =
self.try_parse_cover_arrow_function(cover, allow_return_type_in_arrow_function)
{
return (arrow, true);
}
let expression = self.cover_elements_to_parenthesized_expression(
cover,
first_start,
end,
no_side_effects_comments,
);
(expression, false)
}
/// Whether `kind` after an identifier continues an expression that is not a `BindingElement`.
#[inline]
fn continues_identifier_expression(kind: Kind) -> bool {
matches!(kind, Kind::Dot | Kind::QuestionDot | Kind::LParen | Kind::LBrack)
|| kind.is_template_start_of_tagged_template()
|| kind_to_precedence(kind).is_some()
}
/// Parse `(a)` or `(a) => …` after `a`.
///
/// Returns `Err` with the end of `a`, pushed as a cover element, if a TypeScript `:` follows,
/// which may start a return type or end the consequent of a conditional expression.
#[expect(clippy::inline_always)]
#[inline(always)]
fn parse_parenthesized_identifier_or_arrow_function(
&mut self,
start: u32,
identifier: CoverIdentifier<'a>,
allow_return_type_in_arrow_function: bool,
) -> Result<(Expression<'a>, bool), u32> {
let CoverIdentifier { span, name, .. } = identifier;
self.bump_any(); // `)`
match self.cur_kind() {
// `(a) => …`
Kind::Arrow => {
let pattern = BindingPattern::new_binding_identifier(span, name, self);
let param = FormalParameter::new_plain(span, pattern, self);
let params = FormalParameters::boxed(
self.end_span(start),
FormalParameterKind::ArrowFormalParameters,
ArenaVec::from_value_in(param, self),
None,
self,
);
let arrow = self.parse_arrow_function_rest(
start,
false,
params,
None,
allow_return_type_in_arrow_function,
);
Ok((arrow, true))
}
// `(a): T => …` or `c ? (a) : b`
Kind::Colon if self.is_ts => {
self.state.cover_elements.push(CoverElement::Identifier { span, name });
Err(span.end)
}
// `(a)`
_ => {
// An enclosing cover list cannot become a binding pattern with `(a)` in it.
if self.state.cover_depth > 0 {
self.state.cover_parenthesized.push(span.start);
}
let expression = Expression::new_identifier(span, name, self);
let expression = if self.options.preserve_parens {
Expression::new_parenthesized_expression(self.end_span(start), expression, self)
} else {
expression
};
Ok((expression, false))
}
}
}
/// Whether an element that starts with `kind` cannot be a `BindingElement`.
///
/// `<` starts a JSX element, a type assertion, or an arrow function with type parameters.
/// Most JSX elements which start an expression are directly inside parentheses.
#[inline]
fn cannot_start_binding_element(kind: Kind) -> bool {
kind.is_literal()
|| kind.is_unary_operator()
|| kind.is_update_operator()
|| kind.is_template_start_of_tagged_template()
|| matches!(
kind,
Kind::Function
| Kind::Class
| Kind::New
| Kind::LParen
| Kind::Slash
| Kind::LAngle
)
}
/// Parse the first element of a list that cannot be arrow function parameters, and
/// `( Expression )` if it is the only element.
///
/// Otherwise the element is pushed as a cover element, and returns `Err` with its end if
/// `)` follows it, or `Err(None)` if more elements follow.
fn parse_parenthesized_expression_first_element(
&mut self,
cover: Cover,
identifier: Option<CoverIdentifier<'a>>,
no_side_effects_comments: Option<(u32, NonZeroU32)>,
) -> Result<Expression<'a>, Option<u32>> {
let ctx = self.ctx;
self.ctx = ctx.and_in(true).and_decorator(false);
// Record parentheses in it, as in `((a)) => {}`.
self.state.cover_depth += 1;
let expression = match identifier {
Some(identifier) => self.parse_cover_identifier_expression(identifier),
None => self.parse_assignment_expression_or_higher(),
};
self.state.cover_depth -= 1;
self.ctx = ctx;
if !self.at(Kind::RParen) || self.fatal_error.is_some() {
self.state.cover_elements.push(CoverElement::Expression(expression));
return Err(None);
}
let end = self.prev_token_end;
self.bump_any(); // `)`
// `(1) => {}` is reported by refining the list to parameters.
if self.at(Kind::Arrow) {
self.state.cover_elements.push(CoverElement::Expression(expression));
return Err(Some(end));
}
Ok(self.finish_parenthesized_expression(
cover.start,
cover.parenthesized_base,
expression,
no_side_effects_comments,
))
}
/// `AsyncArrowFunction`[In, Yield, Await] :
/// async [no `LineTerminator` here] `AsyncArrowBindingIdentifier`[?Yield] [no `LineTerminator` here] => `AsyncConciseBody`[?In]
/// `CoverCallExpressionAndAsyncArrowHead`[?Yield, ?Await] [no `LineTerminator` here] => `AsyncConciseBody`[?In]
///
/// Returns the expression, and whether it is an arrow function. If it is not, it is the
/// call `async(…)`. Returns `None` with the cursor at `async` if `async` is an identifier.
pub(super) fn try_parse_async_arrow_function_or_call(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Option<(Expression<'a>, bool)> {
debug_assert!(self.at(Kind::Async));
if self.fatal_error.is_some() {
return None;
}
let start = self.cur_start();
let checkpoint = self.checkpoint();
// An escaped `async` is only an error in an arrow function.
let is_escaped = self.cur_token().escaped();
let (async_span, async_name) = self.parse_identifier_kind(Kind::Ident);
let token = self.cur_token();
if !token.is_on_new_line() {
let kind = token.kind();
if kind == Kind::LParen {
return self.parse_async_call_or_arrow_function(
checkpoint,
async_span,
async_name,
is_escaped,
allow_return_type_in_arrow_function,
);
}
if kind == Kind::Ident {
// `async a => …`
let ident = self.parse_plain_binding_identifier();
if self.at(Kind::Arrow) {
if is_escaped {
self.error(diagnostics::escaped_keyword(async_span));
}
let arrow = self.parse_simple_arrow_function_expression(
start,
ident,
/* async */ true,
allow_return_type_in_arrow_function,
);
return Some((arrow, true));
}
} else if kind == Kind::LAngle && self.is_ts {
// `async <T>(x) => ...`.
// `is_ts` check above because in JS/JSX, `async <` is a comparison
// (`f(async < b, c)`).
self.rewind(checkpoint);
return self
.try_parse_generic_arrow_function_expression(
allow_return_type_in_arrow_function,
)
.map(|arrow| (arrow, true));
} else if kind.is_binding_identifier() {
// `async yield => …`
self.bump_any();
if self.at(Kind::Arrow) {
self.rewind(checkpoint);
let arrow = self.parse_async_simple_arrow_function_expression(
allow_return_type_in_arrow_function,
);
return Some((arrow, true));
}
}
}
self.rewind(checkpoint);
None
}
/// `CoverCallExpressionAndAsyncArrowHead`[Yield, Await] :
/// `MemberExpression`[?Yield, ?Await] `Arguments`[?Yield, ?Await]
///
/// The cursor is at `(`, and `checkpoint` is at `async`.
fn parse_async_call_or_arrow_function(
&mut self,
checkpoint: ParserCheckpoint<'a>,
async_span: Span,
async_name: Ident<'a>,
is_escaped: bool,
allow_return_type_in_arrow_function: bool,
) -> Option<(Expression<'a>, bool)> {
let start = async_span.start;
// `await` is reserved in the parameters of an async arrow function, but not in the
// arguments of a call outside of async code: the list may have to be parsed again.
let may_parse_again = !self.ctx.has_await();
let had_module_syntax = self.module_record_builder.has_module_syntax();
let deferred_script_errors_len = self.deferred_script_errors.len();
let lparen = self.cur_start();
self.bump_any(); // `(`
let mut cover = Cover::new(
start,
lparen,
self.state.cover_elements.len(),
self.state.cover_parenthesized.len(),
/* is_async */ true,
);
let base = cover.base;
let outer_mentions_await = mem::replace(&mut self.state.mentions_await, false);
let list = self.parse_cover_list(lparen, base, true, false, None);
let mentions_await = self.state.mentions_await;
self.state.mentions_await |= outer_mentions_await;
if may_parse_again
&& mentions_await
&& (matches!(list, CoverList::Parameters(_))
|| self.fatal_error.is_some()
|| self.at(Kind::Arrow)
|| (self.is_ts && self.at(Kind::Colon)))
{
// Discard what `rewind` does not restore, and parse the list as parameters.
self.state.cover_elements.truncate(base);
self.state.cover_initialized_name.retain(|&start, _| start < lparen);
self.state.trailing_commas.retain(|&start, _| start < lparen);
self.deferred_script_errors.truncate(deferred_script_errors_len);
if !had_module_syntax {
self.module_record_builder.unset_module_syntax();
}
self.rewind(checkpoint);
return self
.parse_possible_parenthesized_arrow_function_expression(
allow_return_type_in_arrow_function,
)
.map(|arrow| (arrow, true));
}
match list {
CoverList::Parameters(params) => {
if is_escaped {
self.error(diagnostics::escaped_keyword(async_span));
}
let arrow = self.parse_cover_parameters_arrow_function(
cover,
params,
allow_return_type_in_arrow_function,
);
return Some((arrow, true));
}
CoverList::Undecided { trailing_comma, .. } => cover.trailing_comma = trailing_comma,
}
if self.fatal_error.is_some() {
return Some((self.abandon_cover(cover), false));
}
if let Some(arrow) =
self.try_parse_cover_arrow_function(cover, allow_return_type_in_arrow_function)
{
if is_escaped {
self.error(diagnostics::escaped_keyword(async_span));
}
return Some((arrow, true));
}
let callee = Expression::new_identifier(async_span, async_name, self);
Some((self.cover_elements_to_call_expression(cover, callee), false))
}
/// Parse the rest of an arrow function after an undecided cover list, if `=>` or
/// a TypeScript return type follows.
#[expect(clippy::inline_always)]
#[inline(always)]
fn try_parse_cover_arrow_function(
&mut self,
cover: Cover,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
let kind = self.cur_kind();
if kind == Kind::Arrow {
let params = self.cover_elements_to_parameters(cover, self.end_span(cover.lparen));
return Some(self.parse_arrow_function_rest(
cover.start,
cover.is_async,
params,
None,
allow_return_type_in_arrow_function,
));
}
if kind == Kind::Colon && self.is_ts {
return self.try_parse_cover_arrow_function_with_return_type(
cover,
allow_return_type_in_arrow_function,
);
}
None
}
/// Parse `: Type => body` after an undecided cover list, if it is not the `:` of
/// a conditional expression.
fn try_parse_cover_arrow_function_with_return_type(
&mut self,
cover: Cover,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
let params_span = self.end_span(cover.lparen);
if !cover.is_async && self.state.cover_elements.len() == cover.base {
// `(): T => …`: `()` is not an expression, so `:` starts a return type.
let params = self.cover_elements_to_parameters(cover, params_span);
let return_type = self.parse_ts_return_type_annotation();
return Some(self.parse_arrow_function_rest(
cover.start,
false,
params,
return_type,
allow_return_type_in_arrow_function,
));
}
// `case (a.b): c => d` and `a ? (b + 1) : c => d` are not arrow functions.
if self.state.not_parenthesized_arrow.contains(&cover.start)
|| !self.is_cover_parameter_list(cover)
{
return None;
}
let checkpoint = self.checkpoint();
let return_type = self.parse_cover_return_type(cover.is_async);
'reject: {
if self.fatal_error.is_some() || !self.at(Kind::Arrow) {
break 'reject;
}
self.expect_arrow();
if allow_return_type_in_arrow_function {
let params = self.cover_elements_to_parameters(cover, params_span);
let head = ArrowFunctionHead {
type_parameters: None,
params,
return_type,
r#async: cover.is_async,
start: cover.start,
};
return Some(self.parse_arrow_function_expression_body(
head,
allow_return_type_in_arrow_function,
));
}
// In the consequent of a conditional expression, a return type needs another `:`
// after the body, see `parse_possible_parenthesized_arrow_function_expression`.
let body =
self.parse_arrow_function_body(cover.is_async, allow_return_type_in_arrow_function);
if !self.at(Kind::Colon) {
break 'reject;
}
let params = self.cover_elements_to_parameters(cover, params_span);
return Some(Expression::new_arrow_function_expression(
self.end_span(cover.start),
cover.is_async,
None,
params,
return_type,
body,
self,
));
}
self.state.not_parenthesized_arrow.insert(cover.start);
self.rewind(checkpoint);
None
}
/// Parse the rest of an arrow function whose parameters were decided inside `( … )`.
fn parse_cover_parameters_arrow_function(
&mut self,
cover: Cover,
params: ArenaBox<'a, FormalParameters<'a>>,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
self.state.cover_parenthesized.truncate(cover.parenthesized_base);
let return_type =
if self.is_ts { self.parse_cover_return_type(cover.is_async) } else { None };
self.parse_arrow_function_rest(
cover.start,
cover.is_async,
params,
return_type,
allow_return_type_in_arrow_function,
)
}
/// Parse the return type of an arrow function at `:`.
fn parse_cover_return_type(
&mut self,
is_async: bool,
) -> Option<ArenaBox<'a, TSTypeAnnotation<'a>>> {
let has_await = self.ctx.has_await();
self.ctx = self.ctx.union_await_if(is_async);
let return_type = self.parse_ts_return_type_annotation();
self.ctx = self.ctx.and_await(has_await);
return_type
}
pub(crate) fn parse_simple_arrow_function_expression(
&mut self,
start: u32,
ident: BindingIdentifier<'a>,
r#async: bool,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let span = ident.span;
let pattern = BindingPattern::BindingIdentifier(self.alloc(ident));
let formal_parameter = FormalParameter::new_plain(span, pattern, self);
let params = FormalParameters::boxed(
span,
FormalParameterKind::ArrowFormalParameters,
[formal_parameter],
None,
self,
);
self.parse_arrow_function_rest(
start,
r#async,
params,
None,
allow_return_type_in_arrow_function,
)
}
fn parse_async_simple_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
let start = self.cur_start();
self.bump_any(); // bump `async`
// Lookahead established a single binding identifier followed by `=>`.
// Build the binding directly instead of allocating a temporary identifier reference.
let ident = if self.at(Kind::Ident) {
self.parse_plain_binding_identifier()
} else if self.cur_kind().is_identifier() {
self.parse_known_binding_identifier()
} else {
let expr = self.parse_binary_expression_or_higher(Precedence::Comma);
let Expression::Identifier(ident) = &expr else {
return self.unexpected();
};
BindingIdentifier::new(ident.span, ident.name, self)
};
// It is a Syntax Error if ArrowParameters Contains AwaitExpression is true.
if ident.name == "await" {
self.error(diagnostics::identifier_async("await", ident.span));
}
self.parse_simple_arrow_function_expression(
start,
ident,
/* async */ true,
allow_return_type_in_arrow_function,
)
}
/// Parse an arrow function with type parameters, if `<` starts one: `<T>(a: T) => a`.
///
/// The cursor is at `<`, or at an `async` that is followed by `<` on the same line.
///
/// In JS/JSX this always returns `None`, as type parameters are a TS-only thing.
/// This function is only called in a JS file for a `<` at the start of an expression,
/// which is a syntax error. Checking `is_ts` here, or before calling this function,
/// would add a branch to valid TS/TSX, just to bail out sooner on invalid JS (rare).
pub(super) fn try_parse_generic_arrow_function_expression(
&mut self,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
let kind = self.lookahead(|p| {
p.bump(Kind::Async);
p.is_generic_arrow_function_expression()
});
// Most `<` do not start type parameters. Decide that here, without a call.
if kind == Tristate::False {
return None;
}
self.parse_generic_arrow_function_expression(kind, allow_return_type_in_arrow_function)
}
/// Parse an arrow function with type parameters, given whether the tokens at the cursor
/// start one.
///
/// The cursor is at `<`, or at an `async` that is followed by `<` on the same line.
pub(crate) fn parse_generic_arrow_function_expression(
&mut self,
kind: Tristate,
allow_return_type_in_arrow_function: bool,
) -> Option<Expression<'a>> {
match kind {
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,
),
}
}
/// Whether the `<` at the cursor starts the type parameters of an arrow function.
///
/// Called in TS, in TSX only after `async`, and in JS only for invalid code.
/// Never called in JSX.
fn is_generic_arrow_function_expression(&mut self) -> Tristate {
debug_assert!(self.at(Kind::LAngle));
self.bump_any(); // `<`
let second = self.cur_kind();
// 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;
}
// TSX overrides. Only `async <` gets here in TSX.
if self.source_type.is_jsx() {
debug_assert!(self.is_ts);
// <const Ident extends Ident>
// ^^^^^ Optional
self.bump(Kind::Const);
self.bump_any(); // the name
return self.is_generic_arrow_function_expression_after_jsx_name();
}
// `<T>` may start an arrow function, which is then tried first. The exception is
// a type assertion that the next tokens give away, because trying to parse them as
// type parameters and parameters is certain to fail: the name of a type parameter is
// followed by `,`, `=`, `extends` or `>`, not by the rest of a type as in `<A.B>a`,
// `<A[]>a`, `<A | B>a` and `<A<B>>a`, and the `>` of type parameters is followed by
// `(`, not by an operand as in `<T>a` and `<any>{}`.
// JS only gets here for invalid code. `Maybe` gives it a better error for `<T,>`.
if !self.is_ts || second == Kind::Const {
return Tristate::Maybe;
}
self.bump_any(); // the name
match self.cur_kind() {
// `<T,` is not a type, so it can only start type parameters.
Kind::Comma => Tristate::True,
Kind::Dot | Kind::LBrack | Kind::Pipe | Kind::Amp | Kind::LAngle => Tristate::False,
Kind::RAngle => {
self.bump_any(); // `>`
let operand = self.cur_kind();
if operand.is_identifier_name()
|| operand.is_literal()
|| operand.is_template_start_of_tagged_template()
|| matches!(operand, Kind::LCurly | Kind::LBrack)
{
Tristate::False
} else {
Tristate::Maybe
}
}
_ => Tristate::Maybe,
}
}
/// Whether `<` and a name start the type parameters of an arrow function in a TSX file,
/// where they can also start a JSX element. The cursor is at the token after the name,
/// and moves past an `extends`.
///
/// ```text
/// TypeParameter : `const`? BindingIdentifier (`extends` Type)? (`=` Type)?
/// JSXOpeningElement : `<` JSXElementName TypeArguments? JSXAttributes? `/`? `>`
/// JSXElementName : JSXIdentifier ((`:` | `.`) JSXIdentifier)*
/// JSXAttributes : (JSXAttributeName (`=` JSXAttributeValue)? | `{` `...` Expression `}`)*
/// ```
///
/// The name of a type parameter is followed by `,`, `=`, `extends` or `>`. The name of
/// a JSX element is followed by `.`, `:`, `<`, `{`, `/`, `>` or an attribute name, which may
/// be `extends`. So `,` and `=` are type parameters, `extends` is decided by the token
/// after it, and everything else is a JSX element. That includes `<T>`, which is why
/// the type parameters of an arrow function are written `<T,>` in a TSX file.
#[expect(clippy::inline_always)]
#[inline(always)] // Keep `is_generic_arrow_function_expression` in one piece.
pub(crate) fn is_generic_arrow_function_expression_after_jsx_name(&mut self) -> Tristate {
debug_assert!(self.is_ts);
match self.cur_kind() {
Kind::Extends => {
self.bump_any();
let next = self.cur_kind();
if matches!(next, Kind::Eq | Kind::RAngle | Kind::Slash) {
// An attribute: `<T extends={U}>`, `<T extends>`, `<T extends />`.
Tristate::False
} else {
// A constraint: `<T extends U>(a) => a`. `<T extends U` could also start an
// element with attributes `extends` and `U`, but TypeScript always reads it
// as type parameters, so `<T extends U>(a)</T>` is a syntax error.
Tristate::True
}
}
Kind::Eq | Kind::Comma => Tristate::True,
_ => Tristate::False,
}
}
fn parse_parenthesized_arrow_function_head(&mut self) -> ArrowFunctionHead<'a> {
let start = self.cur_start();
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);
ArrowFunctionHead { type_parameters, params, return_type, r#async, start }
}
/// 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.expect_arrow();
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();
let head = self.parse_parenthesized_arrow_function_head();
// Rejected heads are rewound, so do not construct an error for the missing arrow.
if !self.at(Kind::Arrow) {
self.state.not_parenthesized_arrow.insert(pos);
self.rewind(checkpoint);
return None;
}
self.expect_arrow();
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)
}
fn expect_arrow(&mut self) {
if self.at(Kind::Arrow) && self.cur_token().is_on_new_line() {
self.error(diagnostics::lineterminator_before_arrow(self.cur_token().span()));
}
self.expect(Kind::Arrow);
}
/// [ConciseBody](https://tc39.es/ecma262/#prod-ConciseBody)
/// [lookahead ≠ {] `ExpressionBody`[?In, ~Await]
/// { `FunctionBody`[~Yield, ~Await] }
/// `ExpressionBody`[In, Await] :
/// `AssignmentExpression`[?In, ~Yield, ?Await]
#[inline]
fn parse_arrow_function_body(
&mut self,
r#async: bool,
allow_return_type_in_arrow_function: bool,
) -> ArrowFunctionBody<'a> {
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 body = if self.at(Kind::LCurly) {
ArrowFunctionBody::FunctionBody(
self.context_add(Context::In, Self::parse_function_body),
)
} else {
let expr = self.context_remove(Context::TopLevel, |p| {
p.parse_assignment_expression_or_higher_impl(allow_return_type_in_arrow_function)
});
ArrowFunctionBody::from(expr)
};
self.ctx = self.ctx.and_await(has_await).and_yield(has_yield);
body
}
/// Parse `=> ConciseBody` after the parameters and return type.
fn parse_arrow_function_rest(
&mut self,
start: u32,
r#async: bool,
params: ArenaBox<'a, FormalParameters<'a>>,
return_type: Option<ArenaBox<'a, TSTypeAnnotation<'a>>>,
allow_return_type_in_arrow_function: bool,
) -> Expression<'a> {
self.expect_arrow();
let head = ArrowFunctionHead { type_parameters: None, params, return_type, r#async, start };
self.parse_arrow_function_expression_body(head, allow_return_type_in_arrow_function)
}
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, start } =
arrow_function_head;
let body = self.parse_arrow_function_body(r#async, allow_return_type_in_arrow_function);
Expression::new_arrow_function_expression(
self.end_span(start),
r#async,
type_parameters,
params,
return_type,
body,
self,
)
}
}