Skip to content

Commit c37f048

Browse files
committed
fix(semantic): do not declare a Symbol for an ModuleDeclaration where the id is not a BindingIdentifier (#10343)
In previous implementations, a symbol was declared for `ModuleDeclaration` but only inserted when `name` was a `BindingIdentifier`. This is incorrect and also causes a potential performance impact. See https://github.com/oxc-project/oxc/blob/bae53b9606b62fce58b4101497849997b0dc07b3/crates/oxc_semantic/src/stats.rs. So we should only declare a symbol when its name is a `BindingIdentifier` because only `BindingIdentifier` can be inserted with a symbol ID.
1 parent 4a6bb21 commit c37f048

File tree

6 files changed

+88
-295
lines changed

6 files changed

+88
-295
lines changed

crates/oxc_semantic/src/binder.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::ptr;
44

55
use oxc_ast::{AstKind, ast::*};
66
use oxc_ecmascript::{BoundNames, IsSimpleParameterList};
7-
use oxc_span::GetSpan;
87
use oxc_syntax::{
98
scope::{ScopeFlags, ScopeId},
109
symbol::SymbolFlags,
@@ -385,23 +384,22 @@ impl<'a> Binder<'a> for TSEnumMember<'a> {
385384
impl<'a> Binder<'a> for TSModuleDeclaration<'a> {
386385
fn bind(&self, builder: &mut SemanticBuilder) {
387386
// do not bind `global` for `declare global { ... }`
388-
if self.kind == TSModuleDeclarationKind::Global {
387+
if self.kind.is_global() {
389388
return;
390389
}
390+
let TSModuleDeclarationName::Identifier(id) = &self.id else { return };
391391

392392
// At declaration time a module has no value declaration it is only when a value declaration
393393
// is made inside a the scope of a module that the symbol is modified
394394
let ambient = if self.declare { SymbolFlags::Ambient } else { SymbolFlags::None };
395395
let symbol_id = builder.declare_symbol(
396-
self.id.span(),
397-
self.id.name().as_str(),
396+
id.span,
397+
&id.name,
398398
SymbolFlags::NameSpaceModule | ambient,
399399
SymbolFlags::None,
400400
);
401401

402-
if let TSModuleDeclarationName::Identifier(id) = &self.id {
403-
id.symbol_id.set(Some(symbol_id));
404-
}
402+
id.set_symbol_id(symbol_id);
405403
}
406404
}
407405

crates/oxc_semantic/tests/fixtures/typescript-eslint/ts-module/import.snap

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/ts-module/impor
1313
"symbols": [
1414
{
1515
"flags": "SymbolFlags(Import)",
16-
"id": 1,
16+
"id": 0,
1717
"name": "bar",
1818
"node": "ImportSpecifier(bar)",
1919
"references": []
@@ -24,14 +24,6 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/ts-module/impor
2424
"flags": "ScopeFlags(StrictMode | Top)",
2525
"id": 0,
2626
"node": "Program",
27-
"symbols": [
28-
{
29-
"flags": "SymbolFlags(NameSpaceModule | Ambient)",
30-
"id": 0,
31-
"name": "foo",
32-
"node": "TSModuleDeclaration(foo)",
33-
"references": []
34-
}
35-
]
27+
"symbols": []
3628
}
3729
]

crates/oxc_semantic/tests/integration/symbols.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,6 @@ fn test_module_like_declarations() {
423423
.contains_flags(SymbolFlags::NameSpaceModule)
424424
.test();
425425

426-
SemanticTester::ts(r#"module "A" { export const x = 1; }"#)
427-
.has_root_symbol("A")
428-
.contains_flags(SymbolFlags::NameSpaceModule)
429-
.test();
430-
431426
let test = SemanticTester::ts("declare global { interface Window { x: number; } }");
432427
let semantic = test.build();
433428
let global = semantic.scoping().symbol_names().find(|name| *name == "global");

tasks/coverage/snapshots/semantic_babel.snap

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,7 @@ after transform: SymbolId(0): SymbolFlags(RegularEnum)
858858
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
859859
860860
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/estree-compat/shorthand-ambient-module/input.ts
861-
semantic error: Bindings mismatch:
862-
after transform: ScopeId(0): ["hot-new-module"]
863-
rebuilt : ScopeId(0): []
864-
Scope children mismatch:
861+
semantic error: Scope children mismatch:
865862
after transform: ScopeId(0): [ScopeId(1)]
866863
rebuilt : ScopeId(0): []
867864
@@ -1038,10 +1035,7 @@ after transform: ScopeId(0): ["A", "C", "D", "foo"]
10381035
rebuilt : ScopeId(0): []
10391036
10401037
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/not-top-level/input.ts
1041-
semantic error: Bindings mismatch:
1042-
after transform: ScopeId(0): ["m"]
1043-
rebuilt : ScopeId(0): []
1044-
Scope children mismatch:
1038+
semantic error: Scope children mismatch:
10451039
after transform: ScopeId(0): [ScopeId(1)]
10461040
rebuilt : ScopeId(0): []
10471041
@@ -1314,26 +1308,17 @@ after transform: ScopeId(0): [ScopeId(1)]
13141308
rebuilt : ScopeId(0): []
13151309
13161310
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/declare-shorthand/input.ts
1317-
semantic error: Bindings mismatch:
1318-
after transform: ScopeId(0): ["m"]
1319-
rebuilt : ScopeId(0): []
1320-
Scope children mismatch:
1311+
semantic error: Scope children mismatch:
13211312
after transform: ScopeId(0): [ScopeId(1)]
13221313
rebuilt : ScopeId(0): []
13231314
13241315
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/global-in-module/input.ts
1325-
semantic error: Bindings mismatch:
1326-
after transform: ScopeId(0): ["m"]
1327-
rebuilt : ScopeId(0): []
1328-
Scope children mismatch:
1316+
semantic error: Scope children mismatch:
13291317
after transform: ScopeId(0): [ScopeId(1)]
13301318
rebuilt : ScopeId(0): []
13311319
13321320
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/global-in-module-babel-7/input.ts
1333-
semantic error: Bindings mismatch:
1334-
after transform: ScopeId(0): ["m"]
1335-
rebuilt : ScopeId(0): []
1336-
Scope children mismatch:
1321+
semantic error: Scope children mismatch:
13371322
after transform: ScopeId(0): [ScopeId(1)]
13381323
rebuilt : ScopeId(0): []
13391324
@@ -1345,15 +1330,15 @@ semantic error: Ambient modules cannot be nested in other modules or namespaces.
13451330
13461331
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head-declare/input.ts
13471332
semantic error: Bindings mismatch:
1348-
after transform: ScopeId(0): ["M", "N", "m"]
1333+
after transform: ScopeId(0): ["M", "N"]
13491334
rebuilt : ScopeId(0): []
13501335
Scope children mismatch:
13511336
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(4), ScopeId(5)]
13521337
rebuilt : ScopeId(0): []
13531338
13541339
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/module-namespace/head-declare-babel-7/input.ts
13551340
semantic error: Bindings mismatch:
1356-
after transform: ScopeId(0): ["M", "N", "m"]
1341+
after transform: ScopeId(0): ["M", "N"]
13571342
rebuilt : ScopeId(0): []
13581343
Scope children mismatch:
13591344
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(4), ScopeId(5)]
@@ -1573,21 +1558,15 @@ after transform: SymbolId(0): SymbolFlags(RegularEnum)
15731558
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
15741559
15751560
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-func-in-declare-module/input.ts
1576-
semantic error: Bindings mismatch:
1577-
after transform: ScopeId(0): ["m2"]
1578-
rebuilt : ScopeId(0): []
1579-
Scope children mismatch:
1561+
semantic error: Scope children mismatch:
15801562
after transform: ScopeId(0): [ScopeId(1), ScopeId(6)]
15811563
rebuilt : ScopeId(0): []
15821564
Unresolved references mismatch:
15831565
after transform: ["X"]
15841566
rebuilt : []
15851567
15861568
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-import-in-declare-module/input.ts
1587-
semantic error: Bindings mismatch:
1588-
after transform: ScopeId(0): ["~popsicle/dist/common"]
1589-
rebuilt : ScopeId(0): []
1590-
Scope children mismatch:
1569+
semantic error: Scope children mismatch:
15911570
after transform: ScopeId(0): [ScopeId(1)]
15921571
rebuilt : ScopeId(0): []
15931572
@@ -1646,7 +1625,7 @@ rebuilt : SymbolId(0): []
16461625
16471626
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var/input.js
16481627
semantic error: Bindings mismatch:
1649-
after transform: ScopeId(0): ["bar", "foo"]
1628+
after transform: ScopeId(0): ["foo"]
16501629
rebuilt : ScopeId(0): []
16511630
Scope children mismatch:
16521631
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
@@ -2125,16 +2104,13 @@ after transform: SymbolId(3): [Span { start: 81, end: 82 }, Span { start: 170, e
21252104
rebuilt : SymbolId(3): []
21262105
21272106
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-different-module/input.ts
2128-
semantic error: Bindings mismatch:
2129-
after transform: ScopeId(0): ["test", "test/submodule"]
2130-
rebuilt : ScopeId(0): []
2131-
Scope children mismatch:
2107+
semantic error: Scope children mismatch:
21322108
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
21332109
rebuilt : ScopeId(0): []
21342110
21352111
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-different-module-and-top-level/input.ts
21362112
semantic error: Bindings mismatch:
2137-
after transform: ScopeId(0): ["JSONSchema7", "foo", "fooBar", "test/submodule", "test/submodule2"]
2113+
after transform: ScopeId(0): ["JSONSchema7", "foo", "fooBar"]
21382114
rebuilt : ScopeId(0): ["foo", "fooBar"]
21392115
Scope children mismatch:
21402116
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
@@ -2144,10 +2120,7 @@ after transform: SymbolId(1): [ReferenceId(1), ReferenceId(4)]
21442120
rebuilt : SymbolId(0): [ReferenceId(0)]
21452121
21462122
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-nested-module/input.ts
2147-
semantic error: Bindings mismatch:
2148-
after transform: ScopeId(0): ["test/sub1"]
2149-
rebuilt : ScopeId(0): []
2150-
Scope children mismatch:
2123+
semantic error: Scope children mismatch:
21512124
after transform: ScopeId(0): [ScopeId(1)]
21522125
rebuilt : ScopeId(0): []
21532126

0 commit comments

Comments
 (0)