Skip to content

Commit 0b188cc

Browse files
authoredDec 29, 2023
fix(es/parser): Disallowing await as an identifier in class static block (#8450)
1 parent b76dd46 commit 0b188cc

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed
 

‎crates/swc_ecma_parser/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ pub struct Context {
394394
/// keyword.
395395
in_generator: bool,
396396

397+
/// If true, await is treated as a keyword.
398+
in_static_block: bool,
399+
397400
is_continue_allowed: bool,
398401
is_break_allowed: bool,
399402

‎crates/swc_ecma_parser/src/parser/class_and_fn.rs

+2
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ impl<I: Tokens> Parser<I> {
579579
fn parse_static_block(&mut self, start: BytePos) -> PResult<ClassMember> {
580580
let body = self
581581
.with_ctx(Context {
582+
in_static_block: true,
582583
in_class_field: true,
583584
allow_using_decl: true,
584585
..self.ctx()
@@ -1386,6 +1387,7 @@ impl<I: Tokens> Parser<I> {
13861387
true
13871388
},
13881389
in_function: true,
1390+
in_static_block: false,
13891391
is_break_allowed: false,
13901392
is_continue_allowed: false,
13911393
..self.ctx()

‎crates/swc_ecma_parser/src/parser/ident.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl<I: Tokens> Parser<I> {
147147
match w {
148148
Word::Keyword(Keyword::Await) if p.ctx().in_declare => Ok(atom!("await")),
149149

150+
Word::Keyword(Keyword::Await) if p.ctx().in_static_block => {
151+
syntax_error!(p, p.input.prev_span(), SyntaxError::ExpectedIdent)
152+
}
153+
150154
// It is a Syntax Error if the goal symbol of the syntactic grammar is Module
151155
// and the StringValue of IdentifierName is "await".
152156
Word::Keyword(Keyword::Await) if p.ctx().module | p.ctx().in_async => {

‎crates/swc_ecma_parser/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<I: Tokens> Parser<I> {
3535
if ident.is_reserved_in_strict_bind() {
3636
self.emit_strict_mode_err(ident.span, SyntaxError::EvalAndArgumentsInStrict);
3737
}
38-
if self.ctx().in_async && ident.sym == "await" {
38+
if (self.ctx().in_async || self.ctx().in_static_block) && ident.sym == "await" {
3939
self.emit_err(ident.span, SyntaxError::ExpectedIdent);
4040
}
4141
if self.ctx().in_generator && ident.sym == "yield" {

‎crates/swc_ecma_parser/src/parser/stmt.rs

+40
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,46 @@ export default function waitUntil(callback, options = {}) {
23962396
});
23972397
}
23982398

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+
23992439
#[test]
24002440
#[should_panic(expected = "Modifiers cannot appear here")]
24012441
fn class_static_blocks_in_ts_with_invalid_modifier_01() {

‎crates/swc_ecma_parser/src/parser/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ impl Context {
55
pub(crate) fn is_reserved(self, word: &Word) -> bool {
66
match *word {
77
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,
99
Word::Keyword(Keyword::Yield) => self.in_generator || self.strict,
1010

1111
Word::Null
@@ -70,7 +70,7 @@ impl Context {
7070
// let await = 1;
7171
// }
7272
// ```
73-
"await" => self.in_async || self.module,
73+
"await" => self.in_async || self.in_static_block || self.module,
7474
"yield" => self.in_generator || self.strict,
7575

7676
"null" | "true" | "false" | "break" | "case" | "catch" | "continue" | "debugger"

0 commit comments

Comments
 (0)
Please sign in to comment.