fix(transformer/typescript): correct the semantic for TSImportEqualsDeclaration transformation (#8463)

This commit is contained in:
Dunqing 2025-01-14 11:26:50 +00:00
parent 2a400d66d4
commit 270245fd6e
5 changed files with 251 additions and 1899 deletions

View file

@ -1,5 +1,5 @@
use oxc_ast::{ast::*, NONE};
use oxc_semantic::Reference;
use oxc_semantic::{Reference, SymbolFlags};
use oxc_span::SPAN;
use oxc_syntax::reference::ReferenceFlags;
use oxc_traverse::{Traverse, TraverseCtx};
@ -113,20 +113,31 @@ impl<'a> TypeScriptModule<'a, '_> {
}
TSModuleReference::ExternalModuleReference(_) => {}
}
let scope_id = ctx.current_scope_id();
ctx.scopes_mut().remove_binding(scope_id, &decl.id.name);
return None;
}
let binding_pattern_kind =
ctx.ast.binding_pattern_kind_binding_identifier(SPAN, &decl.id.name);
BindingPatternKind::BindingIdentifier(ctx.ast.alloc(decl.id.clone()));
let binding = ctx.ast.binding_pattern(binding_pattern_kind, NONE, false);
let decl_span = decl.span;
let flags = ctx.symbols_mut().get_flags_mut(decl.id.symbol_id());
flags.remove(SymbolFlags::Import);
let (kind, init) = match &mut decl.module_reference {
type_name @ match_ts_type_name!(TSModuleReference) => (
VariableDeclarationKind::Var,
self.transform_ts_type_name(&mut *type_name.to_ts_type_name_mut(), ctx),
),
type_name @ match_ts_type_name!(TSModuleReference) => {
flags.insert(SymbolFlags::FunctionScopedVariable);
(
VariableDeclarationKind::Var,
self.transform_ts_type_name(&mut *type_name.to_ts_type_name_mut(), ctx),
)
}
TSModuleReference::ExternalModuleReference(reference) => {
flags.insert(SymbolFlags::BlockScopedVariable | SymbolFlags::ConstVariable);
if self.ctx.module.is_esm() {
self.ctx.error(diagnostics::import_equals_cannot_be_used_in_esm(decl_span));
}

View file

@ -2,7 +2,7 @@ commit: 54a8389f
semantic_babel Summary:
AST Parsed : 2218/2218 (100.00%)
Positive Passed: 1885/2218 (84.99%)
Positive Passed: 1893/2218 (85.35%)
tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.3-function-in-if-body/input.js
semantic error: Symbol scope ID mismatch for "f":
after transform: SymbolId(0): ScopeId(4294967294)
@ -129,11 +129,6 @@ semantic error: Unresolved references mismatch:
after transform: ["Y", "foo"]
rebuilt : []
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/import-require/input.js
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["x"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(7)]
@ -717,63 +712,20 @@ after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []
Unresolved references mismatch:
semantic error: Unresolved references mismatch:
after transform: ["B"]
rebuilt : []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-in-unambiguous/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []
Unresolved references mismatch:
semantic error: Unresolved references mismatch:
after transform: ["B"]
rebuilt : []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
semantic error: Missing SymbolId: "A"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-require/input.ts
semantic error: Missing SymbolId: "a"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-as-identifier/input.ts
semantic error: Missing SymbolId: "type"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-require/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-named-import-require/input.ts
semantic error: Missing SymbolId: "a"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Reference symbol mismatch for "a":
after transform: SymbolId(0) "a"
rebuilt : SymbolId(0) "a"
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["bar", "type"]
@ -794,11 +746,6 @@ semantic error: Bindings mismatch:
after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-type-as-identifier/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["type"]
rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/internal-comments/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["A", "C", "D", "foo"]
@ -1418,7 +1365,7 @@ rebuilt : ScopeId(0): []
tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-equals-var/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["M", "a"]
after transform: ScopeId(0): ["M"]
rebuilt : ScopeId(0): ["a"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
commit: 54a8389f
Passed: 686/1154
Passed: 688/1154
# All Passed:
* babel-plugin-transform-logical-assignment-operators
@ -1619,7 +1619,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-typescript (47/157)
# babel-plugin-transform-typescript (49/157)
* cast/as-expression/input.ts
Unresolved references mismatch:
after transform: ["T", "x"]
@ -1976,12 +1976,6 @@ Symbol reference IDs mismatch for "None":
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2)]
rebuilt : SymbolId(0): [ReferenceId(1)]
* exports/export-import=/input.ts
Missing SymbolId: "JGraph"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0), SymbolId(1)]
rebuilt : ScopeId(0): [SymbolId(0), SymbolId(1)]
* exports/export-type/input.ts
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
@ -2061,7 +2055,7 @@ rebuilt : ScopeId(0): ["x"]
* imports/elide-type-referenced-in-imports-equal-no/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["bar", "foo", "nsa", "nsb"]
after transform: ScopeId(0): ["nsa", "nsb"]
rebuilt : ScopeId(0): []
* imports/elide-typeof/input.ts
@ -2157,15 +2151,6 @@ Bindings mismatch:
after transform: ScopeId(0): ["A", "B"]
rebuilt : ScopeId(0): []
* imports/import=-module-to-cjs/input.ts
Missing SymbolId: "lib"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Reference symbol mismatch for "lib":
after transform: SymbolId(0) "lib"
rebuilt : SymbolId(0) "lib"
* imports/only-remove-type-imports/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["H", "I", "I2", "J", "K1", "K2", "L1", "L2", "L3", "a", "b", "c2", "d", "d2", "e", "e4"]
@ -2213,19 +2198,15 @@ after transform: SymbolId(2): [ReferenceId(4), ReferenceId(6)]
rebuilt : SymbolId(1): [ReferenceId(1)]
* namespace/alias/input.ts
Missing SymbolId: "AliasModule"
Bindings mismatch:
after transform: ScopeId(0): ["AliasModule", "LongNameModule", "b", "babel", "bar", "baz", "node", "some", "str"]
after transform: ScopeId(0): ["AliasModule", "LongNameModule", "babel", "bar", "baz", "node", "some", "str"]
rebuilt : ScopeId(0): ["AliasModule", "bar", "baz", "node", "some", "str"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Reference symbol mismatch for "AliasModule":
after transform: SymbolId(8) "AliasModule"
rebuilt : SymbolId(0) "AliasModule"
Reference symbol mismatch for "AliasModule":
after transform: SymbolId(8) "AliasModule"
rebuilt : SymbolId(0) "AliasModule"
Symbol reference IDs mismatch for "AliasModule":
after transform: SymbolId(8): [ReferenceId(2), ReferenceId(3), ReferenceId(4)]
rebuilt : SymbolId(0): [ReferenceId(1), ReferenceId(2)]
Unresolved reference IDs mismatch for "LongNameModule":
after transform: [ReferenceId(1), ReferenceId(5)]
rebuilt : [ReferenceId(0)]

View file

@ -1,10 +1,6 @@
commit: 54a8389f
<<<<<<< HEAD
Passed: 129/150
=======
Passed: 126/148
>>>>>>> 92283dbb1 (fix(transformer/typescript): retain TSImportEqualsDeclaration when it is exported)
Passed: 129/151
# All Passed:
* babel-plugin-transform-class-static-block
@ -159,10 +155,6 @@ after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
* namespace/export-import-=/input.ts
Missing SymbolId: "X"
Binding symbols mismatch:
after transform: ScopeId(1): [SymbolId(2), SymbolId(3)]
rebuilt : ScopeId(1): [SymbolId(2), SymbolId(3)]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode | Function)
rebuilt : ScopeId(1): ScopeFlags(Function)
@ -172,21 +164,14 @@ rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable)
Symbol span mismatch for "N1":
after transform: SymbolId(1): Span { start: 31, end: 33 }
rebuilt : SymbolId(1): Span { start: 0, end: 0 }
Reference symbol mismatch for "X":
after transform: SymbolId(2) "X"
rebuilt : SymbolId(3) "X"
* namespace/import-=/input.ts
Missing SymbolId: "X"
Bindings mismatch:
after transform: ScopeId(1): ["V", "X", "_N"]
rebuilt : ScopeId(1): ["V", "_N"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode | Function)
rebuilt : ScopeId(1): ScopeFlags(Function)
Binding symbols mismatch:
after transform: ScopeId(2): [SymbolId(5), SymbolId(6), SymbolId(8)]
rebuilt : ScopeId(2): [SymbolId(5), SymbolId(6), SymbolId(7)]
Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode | Function)
rebuilt : ScopeId(2): ScopeFlags(Function)
@ -205,22 +190,11 @@ rebuilt : SymbolId(4): SymbolFlags(BlockScopedVariable)
Symbol span mismatch for "N2":
after transform: SymbolId(4): Span { start: 130, end: 132 }
rebuilt : SymbolId(4): Span { start: 0, end: 0 }
Reference symbol mismatch for "X":
after transform: SymbolId(5) "X"
rebuilt : SymbolId(6) "X"
* namespace/preserve-import-=/input.ts
Missing SymbolId: "Foo"
Missing SymbolId: "Foo"
Binding symbols mismatch:
after transform: ScopeId(1): [SymbolId(2), SymbolId(3), SymbolId(7)]
rebuilt : ScopeId(1): [SymbolId(2), SymbolId(3), SymbolId(4)]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode | Function)
rebuilt : ScopeId(1): ScopeFlags(Function)
Binding symbols mismatch:
after transform: ScopeId(2): [SymbolId(5), SymbolId(6), SymbolId(8)]
rebuilt : ScopeId(2): [SymbolId(6), SymbolId(7), SymbolId(8)]
Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode | Function)
rebuilt : ScopeId(2): ScopeFlags(Function)
@ -236,12 +210,14 @@ rebuilt : SymbolId(5): SymbolFlags(BlockScopedVariable)
Symbol span mismatch for "N2":
after transform: SymbolId(4): Span { start: 145, end: 147 }
rebuilt : SymbolId(5): Span { start: 0, end: 0 }
Symbol reference IDs mismatch for "Foo":
after transform: SymbolId(5): [ReferenceId(2)]
rebuilt : SymbolId(7): []
* preserve-import-=/input.js
Missing SymbolId: "Foo"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(2)]
rebuilt : ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(2)]
Symbol reference IDs mismatch for "Foo":
after transform: SymbolId(1): [ReferenceId(1)]
rebuilt : SymbolId(1): []
* redeclarations/input.ts
Bindings mismatch: