File tree 6 files changed +52
-3
lines changed
crates/swc_ecma_parser/src
6 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -394,6 +394,9 @@ pub struct Context {
394
394
/// keyword.
395
395
in_generator : bool ,
396
396
397
+ /// If true, await is treated as a keyword.
398
+ in_static_block : bool ,
399
+
397
400
is_continue_allowed : bool ,
398
401
is_break_allowed : bool ,
399
402
Original file line number Diff line number Diff line change @@ -579,6 +579,7 @@ impl<I: Tokens> Parser<I> {
579
579
fn parse_static_block ( & mut self , start : BytePos ) -> PResult < ClassMember > {
580
580
let body = self
581
581
. with_ctx ( Context {
582
+ in_static_block : true ,
582
583
in_class_field : true ,
583
584
allow_using_decl : true ,
584
585
..self . ctx ( )
@@ -1386,6 +1387,7 @@ impl<I: Tokens> Parser<I> {
1386
1387
true
1387
1388
} ,
1388
1389
in_function : true ,
1390
+ in_static_block : false ,
1389
1391
is_break_allowed : false ,
1390
1392
is_continue_allowed : false ,
1391
1393
..self . ctx ( )
Original file line number Diff line number Diff line change @@ -147,6 +147,10 @@ impl<I: Tokens> Parser<I> {
147
147
match w {
148
148
Word :: Keyword ( Keyword :: Await ) if p. ctx ( ) . in_declare => Ok ( atom ! ( "await" ) ) ,
149
149
150
+ Word :: Keyword ( Keyword :: Await ) if p. ctx ( ) . in_static_block => {
151
+ syntax_error ! ( p, p. input. prev_span( ) , SyntaxError :: ExpectedIdent )
152
+ }
153
+
150
154
// It is a Syntax Error if the goal symbol of the syntactic grammar is Module
151
155
// and the StringValue of IdentifierName is "await".
152
156
Word :: Keyword ( Keyword :: Await ) if p. ctx ( ) . module | p. ctx ( ) . in_async => {
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ impl<I: Tokens> Parser<I> {
35
35
if ident. is_reserved_in_strict_bind ( ) {
36
36
self . emit_strict_mode_err ( ident. span , SyntaxError :: EvalAndArgumentsInStrict ) ;
37
37
}
38
- if self . ctx ( ) . in_async && ident. sym == "await" {
38
+ if ( self . ctx ( ) . in_async || self . ctx ( ) . in_static_block ) && ident. sym == "await" {
39
39
self . emit_err ( ident. span , SyntaxError :: ExpectedIdent ) ;
40
40
}
41
41
if self . ctx ( ) . in_generator && ident. sym == "yield" {
Original file line number Diff line number Diff line change @@ -2396,6 +2396,46 @@ export default function waitUntil(callback, options = {}) {
2396
2396
} ) ;
2397
2397
}
2398
2398
2399
+ #[ test]
2400
+ #[ should_panic( expected = "Expected ident" ) ]
2401
+ fn class_static_blocks_with_await ( ) {
2402
+ let src = "class Foo{
2403
+ static {
2404
+ var await = 'bar';
2405
+ }
2406
+ }" ;
2407
+ test_parser ( src, Syntax :: Es ( Default :: default ( ) ) , |p| p. parse_expr ( ) ) ;
2408
+ }
2409
+
2410
+ #[ test]
2411
+ #[ should_panic( expected = "Expected ident" ) ]
2412
+ fn class_static_blocks_with_await_in_nested_class ( ) {
2413
+ let src = "class Foo{
2414
+ static {
2415
+ function foo() {
2416
+ class Foo {
2417
+ static {
2418
+ var await = 'bar';
2419
+ }
2420
+ }
2421
+ }
2422
+ }
2423
+ }" ;
2424
+ test_parser ( src, Syntax :: Es ( Default :: default ( ) ) , |p| p. parse_expr ( ) ) ;
2425
+ }
2426
+
2427
+ #[ test]
2428
+ fn class_static_blocks_with_await_in_fn ( ) {
2429
+ let src = "class Foo{
2430
+ static {
2431
+ function foo() {
2432
+ var await = 'bar';
2433
+ }
2434
+ }
2435
+ }" ;
2436
+ test_parser ( src, Syntax :: Es ( Default :: default ( ) ) , |p| p. parse_expr ( ) ) ;
2437
+ }
2438
+
2399
2439
#[ test]
2400
2440
#[ should_panic( expected = "Modifiers cannot appear here" ) ]
2401
2441
fn class_static_blocks_in_ts_with_invalid_modifier_01 ( ) {
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ impl Context {
5
5
pub ( crate ) fn is_reserved ( self , word : & Word ) -> bool {
6
6
match * word {
7
7
Word :: Keyword ( Keyword :: Let ) => self . strict ,
8
- Word :: Keyword ( Keyword :: Await ) => self . in_async || self . strict ,
8
+ Word :: Keyword ( Keyword :: Await ) => self . in_async || self . in_static_block || self . strict ,
9
9
Word :: Keyword ( Keyword :: Yield ) => self . in_generator || self . strict ,
10
10
11
11
Word :: Null
@@ -70,7 +70,7 @@ impl Context {
70
70
// let await = 1;
71
71
// }
72
72
// ```
73
- "await" => self . in_async || self . module ,
73
+ "await" => self . in_async || self . in_static_block || self . module ,
74
74
"yield" => self . in_generator || self . strict ,
75
75
76
76
"null" | "true" | "false" | "break" | "case" | "catch" | "continue" | "debugger"
You can’t perform that action at this time.
0 commit comments