Skip to content

Commit

Permalink
fix(transformer/typescript): correct the semantic for TSImportEqualsD…
Browse files Browse the repository at this point in the history
…eclaration transformation
  • Loading branch information
Dunqing authored and Boshen committed Jan 14, 2025
1 parent 2a400d6 commit 53dec04
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 1,905 deletions.
23 changes: 17 additions & 6 deletions crates/oxc_transformer/src/typescript/module.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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));
}
Expand Down
61 changes: 4 additions & 57 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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"]
Expand All @@ -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"]
Expand Down Expand Up @@ -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)]
Expand Down
Loading

0 comments on commit 53dec04

Please sign in to comment.