Skip to content

Commit a5af938

Browse files
committed
fix(semantic): allow reserved keywords in typescript ambient contexts
1 parent 4608549 commit a5af938

File tree

9 files changed

+35
-35
lines changed

9 files changed

+35
-35
lines changed

crates/oxc_semantic/src/checker/javascript.rs

Lines changed: 15 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+
// reserved 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_ambient_binding(ctx)
106+
{
104107
return;
105108
}
106109
if name == "await" {
@@ -120,6 +123,16 @@ pub fn check_identifier(name: &str, span: Span, ctx: &SemanticBuilder<'_>) {
120123
}
121124
}
122125

126+
fn is_current_node_ambient_binding(ctx: &SemanticBuilder<'_>) -> bool {
127+
if let AstKind::BindingIdentifier(id) = ctx.nodes.kind(ctx.current_node_id)
128+
&& let Some(symbol_id) = id.symbol_id.get()
129+
{
130+
ctx.scoping.symbol_flags(symbol_id).contains(SymbolFlags::Ambient)
131+
} else {
132+
false
133+
}
134+
}
135+
123136
fn unexpected_identifier_assign(x0: &str, span1: Span) -> OxcDiagnostic {
124137
OxcDiagnostic::error(format!("Cannot assign to '{x0}' in strict mode")).with_label(span1)
125138
}
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: 48244d89
22

33
parser_typescript Summary:
44
AST Parsed : 9816/9817 (99.99%)
5-
Positive Passed: 9804/9817 (99.87%)
5+
Positive Passed: 9805/9817 (99.88%)
66
Negative Passed: 1455/2545 (57.17%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts
88

@@ -2365,23 +2365,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/es6/moduleExp
23652365
65 │ var y = _;
23662366
╰────
23672367

2368-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts
2369-
2370-
× The keyword 'await' is reserved
2371-
╭─[typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts:3:15]
2372-
2 │ export {};
2373-
3 │ declare const await: any;
2374-
· ──────────
2375-
4 │ declare class C extends await {}
2376-
╰────
2377-
2378-
× The keyword 'await' is reserved
2379-
╭─[typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts:4:25]
2380-
3 │ declare const await: any;
2381-
4 │ declare class C extends await {}
2382-
· ─────
2383-
╰────
2384-
23852368
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName11.ts
23862369

23872370
× 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: 48244d89
22

33
semantic_typescript Summary:
44
AST Parsed : 6146/6146 (100.00%)
5-
Positive Passed: 2660/6146 (43.28%)
5+
Positive Passed: 2661/6146 (43.30%)
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)]
@@ -42819,10 +42819,6 @@ Scope children mismatch:
4281942819
after transform: ScopeId(0): [ScopeId(1)]
4282042820
rebuilt : ScopeId(0): []
4282142821

42822-
semantic Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/topLevelAwait.3.ts
42823-
The keyword 'await' is reserved
42824-
The keyword 'await' is reserved
42825-
4282642822
semantic Error: tasks/coverage/typescript/tests/cases/conformance/externalModules/typeAndNamespaceExportMerge.ts
4282742823
Scope children mismatch:
4282842824
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)