Skip to content

Commit

Permalink
fix(transformer/typescript): retain TSImportEqualsDeclaration in name…
Browse files Browse the repository at this point in the history
…space when its binding has been referenced or onlyRemoveTypeImports is true
  • Loading branch information
Dunqing committed Jan 13, 2025
1 parent 424c770 commit 6056996
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 6 deletions.
25 changes: 21 additions & 4 deletions crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use oxc_semantic::Reference;
use rustc_hash::FxHashSet;

use oxc_allocator::{Box as ArenaBox, Vec as ArenaVec};
Expand All @@ -23,11 +24,16 @@ pub struct TypeScriptNamespace<'a, 'ctx> {

// Options
allow_namespaces: bool,
only_remove_type_imports: bool,
}

impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
pub fn new(options: &TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { ctx, allow_namespaces: options.allow_namespaces }
Self {
ctx,
allow_namespaces: options.allow_namespaces,
only_remove_type_imports: options.only_remove_type_imports,
}
}
}

Expand Down Expand Up @@ -274,9 +280,20 @@ impl<'a> TypeScriptNamespace<'a, '_> {
| Statement::TSEnumDeclaration(_) => {
names.insert(stmt.to_declaration().id().as_ref().unwrap().name.clone());
}
Statement::TSTypeAliasDeclaration(_)
| Statement::TSInterfaceDeclaration(_)
| Statement::TSImportEqualsDeclaration(_) => continue,
// Retain when `only_remove_type_imports` is true or there are value references
// The behavior is the same as `TypeScriptModule::transform_ts_import_equals`
Statement::TSImportEqualsDeclaration(decl)
if !self.only_remove_type_imports
&& ctx
.symbols()
.get_resolved_references(decl.id.symbol_id())
.all(Reference::is_type) =>
{
continue;
}
Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {
continue
}
_ => {}
}
new_stmts.push(stmt);
Expand Down
63 changes: 61 additions & 2 deletions tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 54a8389f

Passed: 126/145
Passed: 126/147

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -47,7 +47,7 @@ after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), R
rebuilt : SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), ReferenceId(10)]


# babel-plugin-transform-typescript (2/11)
# babel-plugin-transform-typescript (2/13)
* class-property-definition/input.ts
Unresolved references mismatch:
after transform: ["const"]
Expand Down Expand Up @@ -159,6 +159,65 @@ Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []

* namespace/import-=/input.ts
Missing SymbolId: "N1"
Missing SymbolId: "_N"
Missing ReferenceId: "N1"
Missing ReferenceId: "N1"
Missing SymbolId: "N2"
Missing SymbolId: "_N2"
Missing SymbolId: "X"
Missing ReferenceId: "N2"
Missing ReferenceId: "N2"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(4)]
rebuilt : ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(4)]
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)
Symbol reference IDs mismatch for "A":
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1)]
rebuilt : SymbolId(0): [ReferenceId(2)]
Reference symbol mismatch for "X":
after transform: SymbolId(5) "X"
rebuilt : SymbolId(6) "X"

* namespace/preserve-import-=/input.ts
Missing SymbolId: "N1"
Missing SymbolId: "_N"
Missing SymbolId: "Foo"
Missing ReferenceId: "N1"
Missing ReferenceId: "N1"
Missing SymbolId: "N2"
Missing SymbolId: "_N2"
Missing SymbolId: "Foo"
Missing ReferenceId: "N2"
Missing ReferenceId: "N2"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(4)]
rebuilt : ScopeId(0): [SymbolId(0), SymbolId(1), SymbolId(5)]
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)

* preserve-import-=/input.js
Missing SymbolId: "Foo"
Binding symbols mismatch:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import A from 'mod';
namespace N1 {
// Remove because `X` has not been referenced
import X = A.B;
const V = 0;
}

namespace N2 {
// Retain because `X` has been referenced
import X = A.B;
const V = X;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import A from 'mod';
let N1;
(function (_N) {
const V = 0;
})(N1 || (N1 = {}));
let N2;
(function (_N2) {
// Retain because `X` has been referenced
var X = A.B;
const V = X;
})(N2 || (N2 = {}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import nsa from "mod";

namespace N1 {
// Retain because `onlyRemoveTypeImports` is true
import Foo = nsa.bar;
const foo = 0;
}

namespace N2 {
// Retain because `onlyRemoveTypeImports` is true
import Foo = nsa.bar;
const foo: Foo = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
[
"transform-typescript",
{
"onlyRemoveTypeImports": true
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import nsa from "mod";

let N1;
(function(_N) {
// Retain because `onlyRemoveTypeImports` is true
var Foo = nsa.bar;
const foo = 0;
})(N1 || (N1 = {}));

let N2;
(function(_N2) {
// Retain because `onlyRemoveTypeImports` is true
var Foo = nsa.bar;
const foo = 0;
})(N2 || (N2 = {}));

0 comments on commit 6056996

Please sign in to comment.