Skip to content

Commit

Permalink
fix(es/parser): Disallowing await as an identifier in class static bl…
Browse files Browse the repository at this point in the history
…ock (#8450)
  • Loading branch information
iantanwx authored Dec 29, 2023
1 parent b76dd46 commit 0b188cc
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ pub struct Context {
/// keyword.
in_generator: bool,

/// If true, await is treated as a keyword.
in_static_block: bool,

is_continue_allowed: bool,
is_break_allowed: bool,

Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ impl<I: Tokens> Parser<I> {
fn parse_static_block(&mut self, start: BytePos) -> PResult<ClassMember> {
let body = self
.with_ctx(Context {
in_static_block: true,
in_class_field: true,
allow_using_decl: true,
..self.ctx()
Expand Down Expand Up @@ -1386,6 +1387,7 @@ impl<I: Tokens> Parser<I> {
true
},
in_function: true,
in_static_block: false,
is_break_allowed: false,
is_continue_allowed: false,
..self.ctx()
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/parser/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ impl<I: Tokens> Parser<I> {
match w {
Word::Keyword(Keyword::Await) if p.ctx().in_declare => Ok(atom!("await")),

Word::Keyword(Keyword::Await) if p.ctx().in_static_block => {
syntax_error!(p, p.input.prev_span(), SyntaxError::ExpectedIdent)
}

// It is a Syntax Error if the goal symbol of the syntactic grammar is Module
// and the StringValue of IdentifierName is "await".
Word::Keyword(Keyword::Await) if p.ctx().module | p.ctx().in_async => {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<I: Tokens> Parser<I> {
if ident.is_reserved_in_strict_bind() {
self.emit_strict_mode_err(ident.span, SyntaxError::EvalAndArgumentsInStrict);
}
if self.ctx().in_async && ident.sym == "await" {
if (self.ctx().in_async || self.ctx().in_static_block) && ident.sym == "await" {
self.emit_err(ident.span, SyntaxError::ExpectedIdent);
}
if self.ctx().in_generator && ident.sym == "yield" {
Expand Down
40 changes: 40 additions & 0 deletions crates/swc_ecma_parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,46 @@ export default function waitUntil(callback, options = {}) {
});
}

#[test]
#[should_panic(expected = "Expected ident")]
fn class_static_blocks_with_await() {
let src = "class Foo{
static {
var await = 'bar';
}
}";
test_parser(src, Syntax::Es(Default::default()), |p| p.parse_expr());
}

#[test]
#[should_panic(expected = "Expected ident")]
fn class_static_blocks_with_await_in_nested_class() {
let src = "class Foo{
static {
function foo() {
class Foo {
static {
var await = 'bar';
}
}
}
}
}";
test_parser(src, Syntax::Es(Default::default()), |p| p.parse_expr());
}

#[test]
fn class_static_blocks_with_await_in_fn() {
let src = "class Foo{
static {
function foo() {
var await = 'bar';
}
}
}";
test_parser(src, Syntax::Es(Default::default()), |p| p.parse_expr());
}

#[test]
#[should_panic(expected = "Modifiers cannot appear here")]
fn class_static_blocks_in_ts_with_invalid_modifier_01() {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_parser/src/parser/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl Context {
pub(crate) fn is_reserved(self, word: &Word) -> bool {
match *word {
Word::Keyword(Keyword::Let) => self.strict,
Word::Keyword(Keyword::Await) => self.in_async || self.strict,
Word::Keyword(Keyword::Await) => self.in_async || self.in_static_block || self.strict,
Word::Keyword(Keyword::Yield) => self.in_generator || self.strict,

Word::Null
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Context {
// let await = 1;
// }
// ```
"await" => self.in_async || self.module,
"await" => self.in_async || self.in_static_block || self.module,
"yield" => self.in_generator || self.strict,

"null" | "true" | "false" | "break" | "case" | "catch" | "continue" | "debugger"
Expand Down

0 comments on commit 0b188cc

Please sign in to comment.