Skip to content

Commit 0fb9c45

Browse files
committed
fix(semantic): allow reserved keywords in typescript ambient contexts
1 parent c8c7da7 commit 0fb9c45

File tree

9 files changed

+38
-35
lines changed

9 files changed

+38
-35
lines changed

crates/oxc_semantic/src/checker/javascript.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ pub const STRICT_MODE_NAMES: Set<&'static str> = phf_set! {
9999
};
100100

101101
pub fn check_identifier(name: &str, span: Span, ctx: &SemanticBuilder<'_>) {
102-
// ts module block allows revered keywords
103-
if ctx.current_scope_flags().is_ts_module_block() {
102+
// revered keywords are allowed in ambient contexts
103+
if ctx.source_type.is_typescript_definition()
104+
|| ctx.current_scope_flags().is_ts_module_block()
105+
|| is_current_node_declare_binding(ctx)
106+
{
104107
return;
105108
}
106109
if name == "await" {
@@ -120,6 +123,19 @@ pub fn check_identifier(name: &str, span: Span, ctx: &SemanticBuilder<'_>) {
120123
}
121124
}
122125

126+
fn is_current_node_declare_binding(ctx: &SemanticBuilder<'_>) -> bool {
127+
ctx.nodes
128+
.ancestor_kinds(ctx.current_node_id)
129+
.find_map(|ancestor| {
130+
if let AstKind::VariableDeclaration(decl) = ancestor {
131+
Some(decl.declare)
132+
} else {
133+
None
134+
}
135+
})
136+
.unwrap_or_default()
137+
}
138+
123139
fn unexpected_identifier_assign(x0: &str, span1: Span) -> OxcDiagnostic {
124140
OxcDiagnostic::error(format!("Cannot assign to '{x0}' in strict mode")).with_label(span1)
125141
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare let private: number;
2+
3+
export {};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
codegen_misc Summary:
2-
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 49/49 (100.00%)
2+
AST Parsed : 50/50 (100.00%)
3+
Positive Passed: 50/50 (100.00%)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
formatter_misc Summary:
2-
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 49/49 (100.00%)
2+
AST Parsed : 50/50 (100.00%)
3+
Positive Passed: 50/50 (100.00%)

tasks/coverage/snapshots/parser_misc.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
parser_misc Summary:
2-
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 49/49 (100.00%)
2+
AST Parsed : 50/50 (100.00%)
3+
Positive Passed: 50/50 (100.00%)
44
Negative Passed: 108/108 (100.00%)
55

66
× Cannot assign to 'arguments' in strict mode

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 8ea03f88
22

33
parser_typescript Summary:
44
AST Parsed : 9811/9812 (99.99%)
5-
Positive Passed: 9801/9812 (99.89%)
5+
Positive Passed: 9802/9812 (99.90%)
66
Negative Passed: 1455/2545 (57.17%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts
88

@@ -2246,23 +2246,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExp
22462246
65 │ var y = _;
22472247
╰────
22482248

2249-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts
2250-
2251-
× The keyword 'await' is reserved
2252-
╭─[typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts:3:15]
2253-
2 │ export {};
2254-
3 │ declare const await: any;
2255-
· ──────────
2256-
4 │ declare class C extends await {}
2257-
╰────
2258-
2259-
× The keyword 'await' is reserved
2260-
╭─[typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts:4:25]
2261-
3 │ declare const await: any;
2262-
4 │ declare class C extends await {}
2263-
· ─────
2264-
╰────
2265-
22662249
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName11.ts
22672250

22682251
× Function implementation is missing or not immediately following the declaration.

tasks/coverage/snapshots/semantic_misc.snap

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
semantic_misc Summary:
2-
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 31/49 (63.27%)
2+
AST Parsed : 50/50 (100.00%)
3+
Positive Passed: 31/50 (62.00%)
4+
semantic Error: tasks/coverage/misc/pass/declare-let-private.ts
5+
Bindings mismatch:
6+
after transform: ScopeId(0): ["private"]
7+
rebuilt : ScopeId(0): []
8+
49
semantic Error: tasks/coverage/misc/pass/oxc-11593.ts
510
Scope children mismatch:
611
after transform: ScopeId(0): [ScopeId(1)]

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 8ea03f88
22

33
semantic_typescript Summary:
44
AST Parsed : 6142/6142 (100.00%)
5-
Positive Passed: 2661/6142 (43.32%)
5+
Positive Passed: 2662/6142 (43.34%)
66
semantic Error: tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts
77
Symbol reference IDs mismatch for "Cell":
88
after transform: SymbolId(0): [ReferenceId(1)]
@@ -42760,10 +42760,6 @@ Scope children mismatch:
4276042760
after transform: ScopeId(0): [ScopeId(1)]
4276142761
rebuilt : ScopeId(0): []
4276242762

42763-
semantic Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts
42764-
The keyword 'await' is reserved
42765-
The keyword 'await' is reserved
42766-
4276742763
semantic Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/typeAndNamespaceExportMerge.ts
4276842764
Scope children mismatch:
4276942765
after transform: ScopeId(0): [ScopeId(1)]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
transformer_misc Summary:
2-
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 49/49 (100.00%)
2+
AST Parsed : 50/50 (100.00%)
3+
Positive Passed: 50/50 (100.00%)

0 commit comments

Comments
 (0)