From 3b4fe0edfd4cf0fbe4bf6a7d41b911066263947b Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 6 Jan 2024 12:56:27 +0800 Subject: [PATCH] feat(semantic): allow reserved keyword defined in ts module block (#1907) https://www.typescriptlang.org/play?target=99#code/PTAEAEBcEMCcHMCmkBcpEGcB2iAekAoECAWwHsATAVwBtE1MAmABkcYBpMd8CiwAHKgCNQAYzJYMkUAGUAKgCUAkgGE5AfQCyAeQAiAUXUA5AIKb9MtDOQAeAGQByKdEgBLUaCmwAfKAC8oPwAFgBm6hjIAISgAN58oAkJAESuJPx0JIhYkBhJ7PGJoCnZiLAh0KKIeQWJSXSQ1cSFSfwVANbQSI1gzfywrgBuLlX5TbV9ZJCIolMU3YVFgkI07vPNzm6ia7UAnq6INHOjYAC+ANy8ePxksNIx5wQU0zRwiKBY0JkYrZWgIWRkWKgAgJcSSaSpdKITLZDBoaBYHZnYGgiRSQLCFaieGI5EgsRoiElMoVeigBFIlGgIawUD1HGU-Fg9E-DpIBl41HgwL9IZTDlMwk8ybTWYCrnojbucUE7l7A4UAUnAhAA --- crates/oxc_semantic/src/checker/javascript.rs | 4 ++++ crates/oxc_syntax/src/scope.rs | 4 ++++ tasks/coverage/parser_typescript.snap | 16 ---------------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/crates/oxc_semantic/src/checker/javascript.rs b/crates/oxc_semantic/src/checker/javascript.rs index ae4e224ce..723e9a7c4 100644 --- a/crates/oxc_semantic/src/checker/javascript.rs +++ b/crates/oxc_semantic/src/checker/javascript.rs @@ -179,6 +179,10 @@ pub const STRICT_MODE_NAMES: Set<&'static str> = phf_set! { }; fn check_identifier<'a>(name: &Atom, span: Span, node: &AstNode<'a>, ctx: &SemanticBuilder<'a>) { + // ts module block allows revered keywords + if ctx.current_scope_flags().is_ts_module_block() { + return; + } if *name == "await" { // It is a Syntax Error if the goal symbol of the syntactic grammar is Module and the StringValue of IdentifierName is "await". if ctx.source_type.is_module() { diff --git a/crates/oxc_syntax/src/scope.rs b/crates/oxc_syntax/src/scope.rs index d7c118435..545a91ed6 100644 --- a/crates/oxc_syntax/src/scope.rs +++ b/crates/oxc_syntax/src/scope.rs @@ -52,6 +52,10 @@ impl ScopeFlags { self.contains(Self::ClassStaticBlock) } + pub fn is_ts_module_block(&self) -> bool { + self.contains(Self::TsModuleBlock) + } + pub fn is_var(&self) -> bool { self.intersects(Self::Var) } diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index 3c60c3ee1..9fcf4516b 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -3955,14 +3955,6 @@ Expect to Parse: "conformance/externalModules/topLevelAwait.2.ts" · ───── ╰──── - × The keyword 'await' is reserved - ╭─[conformance/externalModules/topLevelAwait.2.ts:3:1] - 3 │ - 4 │ declare namespace foo { const await: any; } - · ───── - 5 │ - ╰──── - × The keyword 'await' is reserved ╭─[conformance/externalModules/topLevelAwait.2.ts:6:1] 6 │ // await allowed in import=namespace when not a module @@ -15634,14 +15626,6 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" · ───── ╰──── - × The keyword 'await' is reserved - ╭─[conformance/externalModules/topLevelAwaitErrors.12.ts:4:1] - 4 │ export {}; - 5 │ declare namespace foo { const await: any; } - · ───── - 6 │ - ╰──── - × The keyword 'await' is reserved ╭─[conformance/externalModules/topLevelAwaitErrors.12.ts:7:1] 7 │ // await disallowed in import=namespace when in a module