Skip to content

Commit 9b6f2a1

Browse files
authored
fix(es/parser): Mark static as reserved in strict mode (#10861)
Fixes #10851
1 parent 97a5c79 commit 9b6f2a1

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
x Unexpected escape sequence in reserved word: static
2+
,-[$DIR/tests/fixture/issues-2xxx/2844/input/index.js:1:1]
3+
1 | class X { st\u0061tic y() { } }
4+
: ^^^^^^^^^^^
5+
`----
6+
x Unexpected token `<lexing error: Error { error: (11..22, EscapeInReservedWord { word: "static" }) }>`. Expected identifier, string literal, numeric literal or [ for the computed key
7+
,-[$DIR/tests/fixture/issues-2xxx/2844/input/index.js:1:1]
8+
1 | class X { st\u0061tic y() { } }
9+
: ^^^^^^^^^^^
10+
`----

crates/swc/tests/tsc-references/parserStrictMode1.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [parserStrictMode1.ts]
2-
//! x `static` cannot be used as an identifier in strict mode
2+
//! x Expression expected
33
//! ,-[4:1]
44
//! 1 | foo1();
55
//! 2 | foo1();

crates/swc/tests/tsc-references/parserStrictMode1.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [parserStrictMode1.ts]
2-
//! x `static` cannot be used as an identifier in strict mode
2+
//! x Expression expected
33
//! ,-[4:1]
44
//! 1 | foo1();
55
//! 2 | foo1();

crates/swc_ecma_lexer/src/common/parser/expr.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,12 +2446,6 @@ pub fn parse_primary_expr_rest<'a, P: Parser<'a>>(
24462446
.into())
24472447
} else if p.is_ident_ref() {
24482448
let cur = p.bump();
2449-
if cur.is_static() {
2450-
p.emit_strict_mode_err(
2451-
p.input().prev_span(),
2452-
SyntaxError::InvalidIdentInStrict(cur.clone().take_word(p.input()).unwrap()),
2453-
);
2454-
}
24552449
let Some(word) = cur.take_word(p.input_mut()) else {
24562450
unreachable!()
24572451
};

crates/swc_ecma_lexer/src/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::token::*;
66
impl Context {
77
pub fn is_reserved(self, word: &Word) -> bool {
88
match *word {
9-
Word::Keyword(Keyword::Let) => self.contains(Context::Strict),
9+
Word::Keyword(Keyword::Let) | Word::Ident(IdentLike::Known(known_ident!("static"))) => {
10+
self.contains(Context::Strict)
11+
}
1012
Word::Keyword(Keyword::Await) => {
1113
self.contains(Context::InAsync)
1214
|| self.contains(Context::InStaticBlock)

crates/swc_ecma_parser/src/lexer/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ impl std::fmt::Debug for Token {
904904
impl Token {
905905
pub(crate) fn is_reserved(&self, ctx: Context) -> bool {
906906
match self {
907-
Token::Let => ctx.contains(Context::Strict),
907+
Token::Let | Token::Static => ctx.contains(Context::Strict),
908908
Token::Await => {
909909
ctx.contains(Context::InAsync)
910910
|| ctx.contains(Context::InStaticBlock)

0 commit comments

Comments
 (0)