-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
function.rs
1417 lines (1252 loc) · 48.6 KB
/
function.rs
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::parser::ParsedSyntax;
use crate::prelude::*;
use crate::state::{EnterFunction, EnterParameters, SignatureFlags};
use crate::syntax::binding::{
is_at_identifier_binding, is_nth_at_identifier_binding, parse_binding, parse_binding_pattern,
};
use crate::syntax::class::{
empty_decorator_list, parse_initializer_clause, parse_parameter_decorators,
};
use crate::syntax::expr::{
is_nth_at_identifier, parse_assignment_expression_or_higher, ExpressionContext,
};
use crate::syntax::js_parse_error;
use crate::syntax::js_parse_error::{
decorators_not_allowed, expected_binding, expected_parameter, expected_parameters,
};
use crate::syntax::stmt::{is_semi, parse_block_impl, semi, StatementContext};
use crate::syntax::typescript::ts_parse_error::ts_only_syntax_error;
use crate::syntax::typescript::{
is_nth_at_type_parameter_modifier, parse_ts_return_type_annotation, parse_ts_type_annotation,
parse_ts_type_parameters, try_parse, TypeContext,
};
use crate::JsSyntaxFeature::TypeScript;
use crate::ParsedSyntax::{Absent, Present};
use crate::{JsParser, JsSyntaxFeature, ParseRecoveryTokenSet};
use biome_js_syntax::JsSyntaxKind::*;
use biome_js_syntax::{JsSyntaxKind, TextRange, T};
use biome_parser::ParserProgress;
use biome_rowan::SyntaxKind;
/// A function declaration, this could be async and or a generator. This takes a marker
/// because you need to first advance over async or start a marker and feed it in.
// test js function_decl
// function foo1() {}
// function *foo2() {}
// async function *foo3() {}
// async function foo4() {}
// function *foo5() {
// yield foo;
// }
//
// test js function_declaration_script
// // SCRIPT
// function test(await) {}
//
// test_err js function_decl_err
// function() {}
// function foo {}
// function {}
// function *() {}
// async function() {}
// async function *() {}
// function *foo2() {}
// yield foo3;
// function test2(): number {}
// function foo4(await) {}
// function foo5(yield) {}
//
// test_err js function_broken
// function foo())})}{{{ {}
//
// test ts ts_function_statement
// function test(a: string, b?: number, c="default") {}
// function test2<A, B extends A, C = A>(a: A, b: B, c: C) {}
pub(super) fn parse_function_declaration(
p: &mut JsParser,
context: StatementContext,
) -> ParsedSyntax {
if !is_at_function(p) {
return Absent;
}
let m = p.start();
let mut function = if p.state().in_ambient_context() {
parse_ambient_function(p, m, AmbientFunctionKind::Declaration)
} else {
parse_function(
p,
m,
FunctionKind::Declaration {
single_statement_context: context.is_single_statement(),
},
)
};
if context != StatementContext::StatementList && !function.kind(p).is_bogus() {
if JsSyntaxFeature::StrictMode.is_supported(p) {
// test_err js function_in_single_statement_context_strict
// if (true) function a() {}
// label1: function b() {}
// while (true) function c() {}
p.error(p.err_builder("In strict mode code, functions can only be declared at top level or inside a block", function.range(p)).with_hint( "wrap the function in a block statement"));
function.change_to_bogus(p);
} else if !matches!(context, StatementContext::If | StatementContext::Label) {
// test js function_in_if_or_labelled_stmt_loose_mode
// // SCRIPT
// label1: function a() {}
// if (true) function b() {} else function c() {}
// if (true) function d() {}
// if (true) "test"; else function e() {}
p.error(p.err_builder("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if or labelled statement", function.range(p)).with_hint( "wrap the function in a block statement"));
function.change_to_bogus(p);
}
}
Present(function)
}
pub(super) fn parse_function_expression(p: &mut JsParser) -> ParsedSyntax {
if !is_at_function(p) {
return Absent;
}
let m = p.start();
Present(parse_function(p, m, FunctionKind::Expression))
}
// test js export_default_function_clause
// export default function test(a, b) {}
//
// test ts ts_export_default_function_overload
// export default function test(a: string): string;
// export default function test(a: string | undefined): string { return "hello" }
//
// test ts ts_export_function_overload
// export function test(a: string): string;
// export function test(a: string | undefined): string { return "hello" }
pub(super) fn parse_function_export_default_declaration(p: &mut JsParser) -> ParsedSyntax {
if !is_at_function(p) {
return Absent;
}
let m = p.start();
Present(if p.state().in_ambient_context() {
parse_ambient_function(p, m, AmbientFunctionKind::ExportDefault)
} else {
parse_function(p, m, FunctionKind::ExportDefault)
})
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum AmbientFunctionKind {
Declaration,
ExportDefault,
}
impl AmbientFunctionKind {
const fn is_export_default(&self) -> bool {
matches!(self, AmbientFunctionKind::ExportDefault)
}
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum FunctionKind {
Declaration {
// https://tc39.es/ecma262/multipage/additional-ecmascript-features-for-web-browsers.html#sec-functiondeclarations-in-ifstatement-statement-clauses
single_statement_context: bool,
},
Expression,
ExportDefault,
}
impl FunctionKind {
const fn is_export_default(&self) -> bool {
matches!(self, FunctionKind::ExportDefault)
}
fn is_id_optional(&self) -> bool {
matches!(self, FunctionKind::Expression | FunctionKind::ExportDefault)
}
fn is_expression(&self) -> bool {
matches!(self, FunctionKind::Expression)
}
fn is_in_single_statement_context(&self) -> bool {
matches!(
self,
FunctionKind::Declaration {
single_statement_context: true
}
)
}
}
impl From<FunctionKind> for JsSyntaxKind {
fn from(kind: FunctionKind) -> Self {
match kind {
FunctionKind::Declaration { .. } => JS_FUNCTION_DECLARATION,
FunctionKind::Expression => JS_FUNCTION_EXPRESSION,
FunctionKind::ExportDefault => JS_FUNCTION_EXPORT_DEFAULT_DECLARATION,
}
}
}
fn is_at_function(p: &mut JsParser) -> bool {
p.at_ts(token_set![T![async], T![function]]) || is_at_async_function(p, LineBreak::DoNotCheck)
}
#[inline]
fn parse_function(p: &mut JsParser, m: Marker, kind: FunctionKind) -> CompletedMarker {
let mut flags = SignatureFlags::empty();
let in_async = is_at_async_function(p, LineBreak::DoNotCheck);
if in_async {
// test_err js function_escaped_async
// void \u0061sync function f(){}
p.eat(T![async]);
flags |= SignatureFlags::ASYNC;
}
p.expect(T![function]);
let generator_range = if p.at(T![*]) {
let range = p.cur_range();
p.bump(T![*]);
flags |= SignatureFlags::GENERATOR;
Some(range)
} else {
None
};
let id = parse_function_id(p, kind, flags);
if !kind.is_id_optional() {
id.or_add_diagnostic(p, |p, range| {
p.err_builder(
"expected a name for the function in a function declaration, but found none",
range,
)
});
}
TypeScript
.parse_exclusive_syntax(
p,
|p| parse_ts_type_parameters(p, TypeContext::default().and_allow_const_modifier(true)),
|p, marker| {
p.err_builder(
"type parameters can only be used in TypeScript files",
marker.range(p),
)
},
)
.ok();
let parameter_context = if !kind.is_expression() && TypeScript.is_supported(p) {
// It isn't known at this point if this is a function overload definition (body is missing)
// or a regular function implementation.
// Let's go with the laxer of the two. Ideally, these verifications should be part of
// a second compiler pass.
ParameterContext::Declaration
} else {
ParameterContext::Implementation
};
parse_parameter_list(p, parameter_context, TypeContext::default(), flags)
.or_add_diagnostic(p, js_parse_error::expected_parameters);
TypeScript
.parse_exclusive_syntax(
p,
|p| parse_ts_return_type_annotation(p, TypeContext::default()),
|p, marker| {
p.err_builder(
"return types can only be used in TypeScript files",
marker.range(p),
)
},
)
.ok();
let body = parse_function_body(p, flags);
// test ts ts_function_overload
// function test(a: string): void;
// function test(a: string | undefined): void {}
// function no_semi(a: string)
// function no_semi(a: string) {}
// async function async_overload(a: string)
// async function async_overload(a: string) {}
if body.is_absent()
&& TypeScript.is_supported(p)
&& is_semi(p, 0)
&& !kind.is_in_single_statement_context()
&& !kind.is_expression()
{
p.eat(T![;]);
// test_err ts ts_function_overload_generator
// function* test(a: string);
// function* test(a: string) {}
if let Some(generator_range) = generator_range {
p.error(p.err_builder(
"An overload signature cannot be declared as a generator.",
generator_range,
));
}
if kind.is_export_default() {
m.complete(p, TS_DECLARE_FUNCTION_EXPORT_DEFAULT_DECLARATION)
} else {
m.complete(p, TS_DECLARE_FUNCTION_DECLARATION)
}
} else {
body.or_add_diagnostic(p, js_parse_error::expected_function_body);
let mut function = m.complete(p, kind.into());
// test_err js async_or_generator_in_single_statement_context
// if (true) async function t() {}
// if (true) function* t() {}
if kind.is_in_single_statement_context() && (in_async || generator_range.is_some()) {
p.error(p.err_builder("`async` and generator functions can only be declared at top level or inside a block", function.range(p) ));
function.change_to_bogus(p);
}
function
}
}
// test_err js break_in_nested_function
// while (true) {
// function helper() {
// break;
// }
// }
pub(super) fn parse_function_body(p: &mut JsParser, flags: SignatureFlags) -> ParsedSyntax {
p.with_state(EnterFunction(flags), |p| {
parse_block_impl(p, JS_FUNCTION_BODY)
})
}
fn parse_function_id(p: &mut JsParser, kind: FunctionKind, flags: SignatureFlags) -> ParsedSyntax {
match kind {
// Takes the async and generator restriction from the expression
FunctionKind::Expression => {
// test js function_expression_id
// // SCRIPT
// (function await() {});
// (function yield() {});
// (async function yield() {});
// (function* await() {})
//
// test_err js function_expression_id_err
// (async function await() {});
// (function* yield() {});
// function* test() { function yield() {} }
p.with_state(EnterFunction(flags), parse_binding)
}
// Inherits the async and generator from the parent
_ => {
// test js function_id
// // SCRIPT
// function test() {}
// function await(test) {}
// async function await(test) {}
// function yield(test) {}
// function* yield(test) {}
//
//
// test_err js function_id_err
// function* test() {
// function yield(test) {}
// }
parse_binding(p)
}
}
}
// test ts ts_declare_function
// declare function test<A, B, R>(a: A, b: B): R;
// declare function test2({ a }?: { a: "string" })
// declare
// function not_a_declaration() {}
//
// test_err ts ts_declare_function_with_body
// declare function test<A>(a: A): string { return "ambient function with a body"; }
//
// test ts ts_ambient_function
// declare module a {
// function test(): string;
// }
fn parse_ambient_function(
p: &mut JsParser,
m: Marker,
kind: AmbientFunctionKind,
) -> CompletedMarker {
let stmt_start = p.cur_range().start();
// test_err ts ts_declare_async_function
// declare async function test();
let is_async = p.at(T![async]);
if is_async {
p.error(p.err_builder(
"'async' modifier cannot be used in an ambient context.",
p.cur_range(),
));
p.bump(T![async]);
}
p.expect(T![function]);
let is_generator = p.at(T![*]);
if is_generator {
// test_err ts ts_declare_generator_function
// declare function* test(): void;
// declare module 'x' {
// export default function* test(): void
// }
p.error(p.err_builder(
"Generators are not allowed in an ambient context.",
p.cur_range(),
));
p.bump(T![*]);
}
let binding = parse_binding(p);
let binding_range = p.cur_range();
parse_ts_type_parameters(p, TypeContext::default().and_allow_const_modifier(true)).ok();
parse_parameter_list(
p,
ParameterContext::Declaration,
TypeContext::default(),
SignatureFlags::empty(),
)
.or_add_diagnostic(p, expected_parameters);
parse_ts_return_type_annotation(p, TypeContext::default()).ok();
if let Present(body) = parse_function_body(p, SignatureFlags::empty()) {
p.error(
p.err_builder(
"A 'declare' function cannot have a function body",
body.range(p),
)
.with_hint("remove this body"),
);
}
semi(p, TextRange::new(stmt_start, p.cur_range().start()));
if is_async {
m.complete(p, JS_BOGUS_STATEMENT)
} else if kind.is_export_default() {
// test ts ts_declare_function_export_default_declaration
// declare module 'x' {
// export default function(option: any): void
// }
// declare module 'y' {
// export default function test(option: any): void
// }
m.complete(p, TS_DECLARE_FUNCTION_EXPORT_DEFAULT_DECLARATION)
} else {
// test_err ts ts_declare_function_export_declaration_missing_id
// declare module 'x' {
// export function(option: any): void
// }
if binding.is_absent() {
p.error(expected_binding(p, binding_range));
}
// test ts ts_declare_function_export_declaration
// declare module 'x' {
// export function test(option: any): void
// }
m.complete(p, TS_DECLARE_FUNCTION_DECLARATION)
}
}
pub(crate) fn parse_ts_type_annotation_or_error(p: &mut JsParser) -> ParsedSyntax {
TypeScript.parse_exclusive_syntax(
p,
|p| parse_ts_type_annotation(p, TypeContext::default()),
|p, annotation| {
p.err_builder(
"return types can only be used in TypeScript files",
annotation.range(p),
)
.with_hint("remove this type annotation")
},
)
}
/// Tells [is_at_async_function] if it needs to check line breaks
#[derive(PartialEq, Eq)]
pub(crate) enum LineBreak {
// check line breaks
DoCheck,
// do not check line break
DoNotCheck,
}
#[inline]
/// Checks if the parser is inside a "async function"
pub(super) fn is_at_async_function(p: &mut JsParser, should_check_line_break: LineBreak) -> bool {
let async_function_tokens = p.at(T![async]) && p.nth_at(1, T![function]);
if should_check_line_break == LineBreak::DoCheck {
async_function_tokens && !p.has_nth_preceding_line_break(1)
} else {
async_function_tokens
}
}
/// There are cases where the parser must speculatively parse a syntax. For example,
/// parsing `<string>(test)` very much looks like an arrow expression *except* that it isn't followed
/// by a `=>`. This enum tells a parse function if ambiguity should be tolerated or if it should stop if it is not.
#[derive(Debug, Copy, Clone)]
pub(crate) enum Ambiguity {
/// Ambiguity is allowed. A parse method should continue even if an expected character is missing.
Allowed,
/// Ambiguity isn't allowed. A parse method should stop parsing if an expected character is missing
/// and let the caller decide what to do in this case.
Disallowed,
}
impl Ambiguity {
fn is_disallowed(&self) -> bool {
matches!(self, Ambiguity::Disallowed)
}
}
pub(crate) fn parse_arrow_function_expression(p: &mut JsParser) -> ParsedSyntax {
parse_parenthesized_arrow_function_expression(p)
.or_else(|| parse_arrow_function_with_single_parameter(p))
}
/// Tries to parse the header of a parenthesized arrow function expression.
///
/// The header is everything coming before the (or everything up and including the `=>` token):
/// `async (a) =>`.
///
/// Returns the [Marker] for the parsed arrow function header that must be completed by the caller.
///
/// ## Errors
///
/// Returns `Err` if `ambiguity` is [Ambiguity::Disallowed] and the syntax
/// is ambiguous. The `Err` contains the [Marker] of the syntax parsed to this point. It's up
/// to the caller to abandon or complete the returned marker.
///
/// For example, the parser speculatively tries to parse `<string>(test)` as an arrow
/// function because the start very much looks like one, except that the `=>` token is missing
/// (it's a TypeScript `<string>` cast followed by a parenthesized expression).
fn try_parse_parenthesized_arrow_function_head(
p: &mut JsParser,
ambiguity: Ambiguity,
) -> Result<(Marker, SignatureFlags), Marker> {
let m = p.start();
// test_err js arrow_escaped_async
// \u0061sync () => {}
let flags = if p.eat(T![async]) {
SignatureFlags::ASYNC
} else {
SignatureFlags::empty()
};
if p.at(T![<]) {
parse_ts_type_parameters(p, TypeContext::default().and_allow_const_modifier(true)).ok();
if ambiguity.is_disallowed() && p.last() != Some(T![>]) {
return Err(m);
}
}
if !p.at(T!['(']) && ambiguity.is_disallowed() {
return Err(m);
}
// test_err ts ts_decorator_on_arrow_function { "parse_class_parameter_decorators": true }
// const method = (@dec x, second, @dec third = 'default') => {};
// const method = (@dec.fn() x, second, @dec.fn() third = 'default') => {};
// const method = (@dec() x, second, @dec() third = 'default') => {};
parse_parameter_list(
p,
ParameterContext::Arrow,
TypeContext::default(),
arrow_function_parameter_flags(p, flags),
)
.or_add_diagnostic(p, expected_parameters);
if p.last() != Some(T![')']) && ambiguity.is_disallowed() {
return Err(m);
}
TypeScript
.parse_exclusive_syntax(
p,
|p| parse_ts_return_type_annotation(p, TypeContext::default()),
|p, annotation| ts_only_syntax_error(p, "return type annotation", annotation.range(p)),
)
.ok();
if p.has_preceding_line_break() {
p.error(p.err_builder("Line terminator not permitted before arrow.", p.cur_range()));
}
if !p.expect(T![=>]) && ambiguity.is_disallowed() {
return Err(m);
}
Ok((m, flags))
}
// test ts ts_arrow_function_type_parameters
// let a = <A, B extends A, C = string>(a: A, b: B, c: C) => "hello";
// let b = async <A, B>(a: A, b: B): Promise<string> => "hello";
fn parse_possible_parenthesized_arrow_function_expression(p: &mut JsParser) -> ParsedSyntax {
let start_pos = p.cur_range().start();
// Test if we already tried to parse this position as an arrow function and failed.
// If so, bail out immediately.
if p.state().not_parenthesized_arrow.contains(&start_pos) {
return Absent;
}
match try_parse(p, |p| {
try_parse_parenthesized_arrow_function_head(p, Ambiguity::Disallowed)
}) {
Ok((m, flags)) => {
parse_arrow_body(p, flags).or_add_diagnostic(p, js_parse_error::expected_arrow_body);
Present(m.complete(p, JS_ARROW_FUNCTION_EXPRESSION))
}
Err(m) => {
// SAFETY: Abandoning the marker here is safe because `try_parse` rewinds if
// the callback returns `Err` (which is the case that this branch is handling).
m.abandon(p);
p.state_mut().not_parenthesized_arrow.insert(start_pos);
Absent
}
}
}
fn parse_parenthesized_arrow_function_expression(p: &mut JsParser) -> ParsedSyntax {
let is_parenthesized = is_parenthesized_arrow_function_expression(p);
match is_parenthesized {
IsParenthesizedArrowFunctionExpression::True => {
let (m, flags) = try_parse_parenthesized_arrow_function_head(p, Ambiguity::Allowed).expect("'CompletedMarker' because function should never return 'Err' if called with 'Ambiguity::Allowed'.");
parse_arrow_body(p, flags).or_add_diagnostic(p, js_parse_error::expected_arrow_body);
Present(m.complete(p, JS_ARROW_FUNCTION_EXPRESSION))
}
IsParenthesizedArrowFunctionExpression::Unknown => {
parse_possible_parenthesized_arrow_function_expression(p)
}
IsParenthesizedArrowFunctionExpression::False => Absent,
}
}
#[derive(Debug, Copy, Clone)]
enum IsParenthesizedArrowFunctionExpression {
True,
False,
Unknown,
}
// test js paren_or_arrow_expr
// (foo);
// (foo) => {};
// (5 + 5);
// ({foo, bar, b: [f, ...baz]}) => {};
// (foo, ...bar) => {}
// test_err js paren_or_arrow_expr_invalid_params
// (5 + 5) => {}
// (a, ,b) => {}
// (a, b) =>;
// (a: string;
// (a, b)
// => {}
fn is_parenthesized_arrow_function_expression(
p: &mut JsParser,
) -> IsParenthesizedArrowFunctionExpression {
match p.cur() {
// These could be the start of a parenthesized arrow function expression but needs further verification
T!['('] | T![<] => {
is_parenthesized_arrow_function_expression_impl(p, SignatureFlags::empty())
}
T![async] => {
// test js async_arrow_expr
// let a = async foo => {}
// let b = async (bar) => {}
// async (foo, bar, ...baz) => foo
if p.has_nth_preceding_line_break(1) {
IsParenthesizedArrowFunctionExpression::False
} else if matches!(p.nth(1), T!['('] | T![<]) {
is_parenthesized_arrow_function_expression_impl(p, SignatureFlags::ASYNC)
} else {
IsParenthesizedArrowFunctionExpression::False
}
}
// Not entirely correct but that's probably what the user intended
T![=>] => IsParenthesizedArrowFunctionExpression::True,
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
// Tests if the parser is at an arrow function expression
fn is_parenthesized_arrow_function_expression_impl(
p: &mut JsParser,
flags: SignatureFlags,
) -> IsParenthesizedArrowFunctionExpression {
let n = usize::from(flags.contains(SignatureFlags::ASYNC));
match p.nth(n) {
T!['('] => {
match p.nth(n + 1) {
T![')'] => {
// '()' is an arrow expression if followed by an '=>', a type annotation or body.
// Otherwise, a parenthesized expression with a missing inner expression
match p.nth(n + 2) {
T![=>] | T![:] | T!['{'] => IsParenthesizedArrowFunctionExpression::True,
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
// Rest parameter '(...a' is certainly not a parenthesized expression
T![...] => IsParenthesizedArrowFunctionExpression::True,
// '([ ...', '({ ... } can either be a parenthesized object or array expression or a destructing parameter
T!['['] | T!['{'] => IsParenthesizedArrowFunctionExpression::Unknown,
// '(@' can be a decorator or a parenthesized arrow function
T![@] => IsParenthesizedArrowFunctionExpression::Unknown,
// '(a...'
_ if is_nth_at_identifier_binding(p, n + 1) || p.nth_at(n + 1, T![this]) => {
match p.nth(n + 2) {
// '(a: ' must be a type annotation
T![:] => IsParenthesizedArrowFunctionExpression::True,
// Unclear because it could either be
// * '(a = ': an initializer or a parenthesized assignment expression
// * '(a, ': separator to next parameter or a parenthesized sequence expression
// * '(a)': a single parameter OR a parenthesized expression
T![=] | T![,] | T![')'] => IsParenthesizedArrowFunctionExpression::Unknown,
T![?] => {
// Disambiguate between an optional parameter and a parenthesized conditional expression
match p.nth(n + 3) {
// '(a?:' | '(a?,' | '(a?=' | '(a?)'
T![:] | T![,] | T![=] | T![')'] => {
IsParenthesizedArrowFunctionExpression::True
}
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
// potential start of type parameters
T![<] => {
// test jsx jsx_type_arguments
// // These may look like a valid arrows but are JSX
// <A extends>() =</A>;
// <A extends="B">() =</A>;
// <A extends ok>() =</A>;
// <const A>() =</const>;
// <const A extends/>;
// <A extends/>;
// test tsx tsx_type_arguments
// // These are valid type arguments
// <A,>() => {};
// <const A,>() => {};
// <A extends B>() => {};
// <A=string>() => {};
// <A, B>() => {};
// <A extends B<C>>() => {};
if JsSyntaxFeature::Jsx.is_supported(p) {
// Disambiguate between JSX and type parameters
// Type parameters of arrow functions accept only the `const` modifier.
let n = if p.nth_at(n + 1, T![const]) { n + 1 } else { n };
if !is_nth_at_identifier(p, n + 1) {
// <5...
return IsParenthesizedArrowFunctionExpression::False;
};
match p.nth(n + 2) {
T![extends] => {
// `<a extends=` OR `<a extends/>` OR `<a extends>` is a JSX element
// and a `extends` type refinement: `<A extends string>`
if matches!(p.nth(n + 3), T![=] | T![/] | T![>]) {
IsParenthesizedArrowFunctionExpression::False
}
// `<A extends B>` Could be either
else if is_nth_at_identifier(p, n + 3) {
IsParenthesizedArrowFunctionExpression::Unknown
} else {
// <A extends B> must be type arguments
IsParenthesizedArrowFunctionExpression::True
}
}
// `<A=` or `<A,` or always type arguments and never JSX tags
T![=] | T![,] => IsParenthesizedArrowFunctionExpression::True,
_ => IsParenthesizedArrowFunctionExpression::False,
}
} else if is_nth_at_type_parameter_modifier(p, n + 1) {
// <const T>...
IsParenthesizedArrowFunctionExpression::True
} else if !is_nth_at_identifier(p, n + 1) {
// <5...
IsParenthesizedArrowFunctionExpression::False
} else {
// <a...
IsParenthesizedArrowFunctionExpression::Unknown
}
}
_ => IsParenthesizedArrowFunctionExpression::False,
}
}
/// Computes the signature flags for parsing the parameters of an arrow expression. These
/// have different semantics from parsing the body
fn arrow_function_parameter_flags(p: &JsParser, mut flags: SignatureFlags) -> SignatureFlags {
if p.state().in_generator() {
// Arrow functions inherit whatever yield is a valid identifier name from the parent.
flags |= SignatureFlags::GENERATOR;
}
// The arrow function is in an async context if the outer function is in an async context or itself is
// declared async
if p.state().in_async() {
flags |= SignatureFlags::ASYNC;
}
flags
}
// test js arrow_expr_single_param
// // SCRIPT
// foo => {}
// yield => {}
// await => {}
// baz =>
// {}
fn parse_arrow_function_with_single_parameter(p: &mut JsParser) -> ParsedSyntax {
if !is_arrow_function_with_single_parameter(p) {
return Absent;
}
let m = p.start();
let is_async = p.at(T![async]) && is_nth_at_identifier_binding(p, 1);
let flags = if is_async {
p.eat(T![async]);
SignatureFlags::ASYNC
} else {
SignatureFlags::empty()
};
// test_err js async_arrow_expr_await_parameter
// let a = async await => {}
// async() => { (a = await) => {} };
// async() => { (a = await 10) => {} };
p.with_state(EnterParameters(arrow_function_parameter_flags(p, flags)), parse_binding)
.expect("Expected function parameter to be present as guaranteed by is_arrow_function_with_simple_parameter");
p.bump(T![=>]);
parse_arrow_body(p, flags).or_add_diagnostic(p, js_parse_error::expected_arrow_body);
Present(m.complete(p, JS_ARROW_FUNCTION_EXPRESSION))
}
fn is_arrow_function_with_single_parameter(p: &mut JsParser) -> bool {
// a => ...
if p.nth_at(1, T![=>]) {
// test js single_parameter_arrow_function_with_parameter_named_async
// let id = async => async;
is_at_identifier_binding(p) && !p.has_nth_preceding_line_break(1)
}
// async ident => ...
else {
p.at(T![async])
&& !p.has_nth_preceding_line_break(1)
&& is_nth_at_identifier_binding(p, 1)
&& !p.has_nth_preceding_line_break(2)
&& p.nth_at(2, T![=>])
}
}
fn parse_arrow_body(p: &mut JsParser, mut flags: SignatureFlags) -> ParsedSyntax {
// test js arrow_in_constructor
// class A {
// constructor() {
// () => { super() };
// () => super();
// }
// }
if p.state().in_constructor() {
flags |= SignatureFlags::CONSTRUCTOR
}
if p.at(T!['{']) {
parse_function_body(p, flags)
} else {
p.with_state(EnterFunction(flags), |p| {
parse_assignment_expression_or_higher(p, ExpressionContext::default())
})
}
}
pub(crate) fn parse_any_parameter(
p: &mut JsParser,
decorator_list: ParsedSyntax,
parameter_context: ParameterContext,
expression_context: ExpressionContext,
type_context: TypeContext,
) -> ParsedSyntax {
let parameter = match p.cur() {
T![...] => parse_rest_parameter(p, decorator_list, expression_context, type_context),
T![this] => {
// test_err ts ts_decorator_this_parameter_option { "parse_class_parameter_decorators": true }
// class A {
// method(@dec this) {}
// method(@dec(val) this) {}
// method(@dec.fn(val) this) {}
// }
decorator_list
.add_diagnostic_if_present(p, decorators_not_allowed)
.map(|mut decorator_list| {
decorator_list.change_to_bogus(p);
decorator_list
});
parse_ts_this_parameter(p, type_context)
}
_ => parse_formal_parameter(
p,
decorator_list,
parameter_context,
expression_context,
type_context,
),
};
parameter.map(|mut parameter| {
if parameter.kind(p) == TS_THIS_PARAMETER {
if TypeScript.is_unsupported(p) {
parameter.change_to_bogus(p);
p.error(ts_only_syntax_error(
p,
"this parameter",
parameter.range(p),
));
} else if parameter_context.is_arrow_function() {
// test_err ts ts_arrow_function_this_parameter
// let a = (this: string) => {}
parameter.change_to_bogus(p);
p.error(p.err_builder(
"An arrow function cannot have a 'this' parameter.",
parameter.range(p),
));
}
}
parameter
})
}
pub(crate) fn parse_rest_parameter(
p: &mut JsParser,
decorator_list: ParsedSyntax,
expression_context: ExpressionContext,
type_context: TypeContext,
) -> ParsedSyntax {
if !p.at(T![...]) {
return Absent;
}
let m = decorator_list
.or_else(|| empty_decorator_list(p))
.precede(p);
p.bump(T![...]);
parse_binding_pattern(p, expression_context).or_add_diagnostic(p, expected_binding);
let mut valid = true;
if p.eat(T![?]) {
let err = p.err_builder("rest patterns cannot be optional", p.cur_range());
p.error(err);
valid = false;
}
// type annotation `...foo: number[]`
TypeScript
.parse_exclusive_syntax(
p,
|p| parse_ts_type_annotation(p, type_context),
|p, annotation| ts_only_syntax_error(p, "type annotation", annotation.range(p)),
)
.ok();
if let Present(initializer) = parse_initializer_clause(p, ExpressionContext::default()) {
// test_err js arrow_rest_in_expr_in_initializer
// for ((...a = "b" in {}) => {};;) {}