Skip to content

Commit df49d09

Browse files
DunqingBoshen
authored andcommitted
refactor(transformer/typescript): remove unnecessary specific handling of TSModuledDeclaration transformation
1 parent 16606b1 commit df49d09

File tree

6 files changed

+11
-96
lines changed

6 files changed

+11
-96
lines changed

crates/oxc_transformer/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,6 @@ impl<'a> Traverse<'a> for TransformerImpl<'a, '_> {
307307
self.x2_es2022.exit_static_block(block, ctx);
308308
}
309309

310-
fn enter_ts_module_declaration(
311-
&mut self,
312-
decl: &mut TSModuleDeclaration<'a>,
313-
ctx: &mut TraverseCtx<'a>,
314-
) {
315-
if let Some(typescript) = self.x0_typescript.as_mut() {
316-
typescript.enter_ts_module_declaration(decl, ctx);
317-
}
318-
}
319-
320310
#[inline]
321311
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
322312
self.common.enter_expression(expr, ctx);

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc_hash::FxHashSet;
2-
31
use oxc_allocator::{TakeIn, Vec as ArenaVec};
42
use oxc_ast::ast::*;
53
use oxc_diagnostics::OxcDiagnostic;
@@ -29,7 +27,6 @@ pub struct TypeScriptAnnotations<'a, 'ctx> {
2927
has_jsx_fragment: bool,
3028
jsx_element_import_name: String,
3129
jsx_fragment_import_name: String,
32-
type_identifier_names: FxHashSet<Atom<'a>>,
3330
}
3431

3532
impl<'a, 'ctx> TypeScriptAnnotations<'a, 'ctx> {
@@ -55,7 +52,6 @@ impl<'a, 'ctx> TypeScriptAnnotations<'a, 'ctx> {
5552
has_jsx_fragment: false,
5653
jsx_element_import_name,
5754
jsx_fragment_import_name,
58-
type_identifier_names: FxHashSet::default(),
5955
}
6056
}
6157
}
@@ -79,7 +75,7 @@ impl<'a> Traverse<'a> for TypeScriptAnnotations<'a, '_> {
7975
true
8076
} else {
8177
decl.specifiers
82-
.retain(|specifier| self.can_retain_export_specifier(specifier, ctx));
78+
.retain(|specifier| Self::can_retain_export_specifier(specifier, ctx));
8379
// Keep the export declaration if there are still specifiers after removing type exports
8480
!decl.specifiers.is_empty()
8581
}
@@ -456,8 +452,6 @@ impl<'a> Traverse<'a> for TypeScriptAnnotations<'a, '_> {
456452
// Remove TS specific statements
457453
stmts.retain(|stmt| match stmt {
458454
Statement::ExpressionStatement(s) => !s.expression.is_typescript_syntax(),
459-
// Any namespaces left after namespace transform are type only, so remove them
460-
Statement::TSModuleDeclaration(_) => false,
461455
match_declaration!(Statement) => !stmt.to_declaration().is_typescript_syntax(),
462456
// Ignore ModuleDeclaration as it's handled in the program
463457
_ => true,
@@ -548,17 +542,6 @@ impl<'a> Traverse<'a> for TypeScriptAnnotations<'a, '_> {
548542
fn enter_jsx_fragment(&mut self, _elem: &mut JSXFragment<'a>, _ctx: &mut TraverseCtx<'a>) {
549543
self.has_jsx_fragment = true;
550544
}
551-
552-
fn enter_ts_module_declaration(
553-
&mut self,
554-
decl: &mut TSModuleDeclaration<'a>,
555-
_ctx: &mut TraverseCtx<'a>,
556-
) {
557-
// NB: Namespace transform happens in `enter_program` visitor, and replaces retained
558-
// namespaces with functions. This visitor is called after, by which time any remaining
559-
// namespaces need to be deleted.
560-
self.type_identifier_names.insert(decl.id.name());
561-
}
562545
}
563546

564547
impl<'a> TypeScriptAnnotations<'a, '_> {
@@ -615,14 +598,8 @@ impl<'a> TypeScriptAnnotations<'a, '_> {
615598
self.is_jsx_imports(&id.name)
616599
}
617600

618-
fn can_retain_export_specifier(
619-
&self,
620-
specifier: &ExportSpecifier<'a>,
621-
ctx: &TraverseCtx<'a>,
622-
) -> bool {
623-
if specifier.export_kind.is_type()
624-
|| self.type_identifier_names.contains(&specifier.exported.name())
625-
{
601+
fn can_retain_export_specifier(specifier: &ExportSpecifier<'a>, ctx: &TraverseCtx<'a>) -> bool {
602+
if specifier.export_kind.is_type() {
626603
return false;
627604
}
628605
!matches!(&specifier.local, ModuleExportName::IdentifierReference(ident) if Self::is_refers_to_type(ident, ctx))

crates/oxc_transformer/src/typescript/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ impl<'a> Traverse<'a> for TypeScript<'a, '_> {
118118
self.annotations.enter_class_body(body, ctx);
119119
}
120120

121-
fn enter_ts_module_declaration(
122-
&mut self,
123-
decl: &mut TSModuleDeclaration<'a>,
124-
ctx: &mut TraverseCtx<'a>,
125-
) {
126-
self.annotations.enter_ts_module_declaration(decl, ctx);
127-
}
128-
129121
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
130122
self.annotations.enter_expression(expr, ctx);
131123
}

crates/oxc_transformer/src/typescript/namespace.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use oxc_allocator::{Box as ArenaBox, TakeIn, Vec as ArenaVec};
22
use oxc_ast::{NONE, ast::*};
33
use oxc_ecmascript::BoundNames;
4-
use oxc_semantic::Reference;
54
use oxc_span::SPAN;
65
use oxc_syntax::{
76
operator::{AssignmentOperator, LogicalOperator},
@@ -22,24 +21,18 @@ pub struct TypeScriptNamespace<'a, 'ctx> {
2221

2322
// Options
2423
allow_namespaces: bool,
25-
only_remove_type_imports: bool,
2624
}
2725

2826
impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
2927
pub fn new(options: &TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
30-
Self {
31-
ctx,
32-
allow_namespaces: options.allow_namespaces,
33-
only_remove_type_imports: options.only_remove_type_imports,
34-
}
28+
Self { ctx, allow_namespaces: options.allow_namespaces }
3529
}
3630
}
3731

3832
impl<'a> Traverse<'a> for TypeScriptNamespace<'a, '_> {
3933
// `namespace Foo { }` -> `let Foo; (function (_Foo) { })(Foo || (Foo = {}));`
4034
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
4135
// namespace declaration is only allowed at the top level
42-
4336
if !has_namespace(program.body.as_slice()) {
4437
return;
4538
}
@@ -59,14 +52,12 @@ impl<'a> Traverse<'a> for TypeScriptNamespace<'a, '_> {
5952
self.handle_nested(decl, /* is_export */ false, &mut new_stmts, None, ctx);
6053
continue;
6154
}
62-
Statement::ExportNamedDeclaration(export_decl) => {
63-
if export_decl.declaration.as_ref().is_none_or(|decl| {
64-
decl.declare() || !matches!(decl, Declaration::TSModuleDeclaration(_))
65-
}) {
66-
new_stmts.push(Statement::ExportNamedDeclaration(export_decl));
67-
continue;
68-
}
69-
55+
Statement::ExportNamedDeclaration(export_decl)
56+
if export_decl.declaration.as_ref().is_some_and(|declaration| {
57+
!declaration.declare()
58+
&& matches!(declaration, Declaration::TSModuleDeclaration(_))
59+
}) =>
60+
{
7061
let Some(Declaration::TSModuleDeclaration(decl)) =
7162
export_decl.unbox().declaration
7263
else {
@@ -156,7 +147,6 @@ impl<'a> TypeScriptNamespace<'a, '_> {
156147
match stmt {
157148
Statement::TSModuleDeclaration(decl) => {
158149
self.handle_nested(decl, /* is_export */ false, &mut new_stmts, None, ctx);
159-
continue;
160150
}
161151
Statement::ExportNamedDeclaration(export_decl) => {
162152
// NB: `ExportNamedDeclaration` with no declaration (e.g. `export {x}`) is not
@@ -219,25 +209,9 @@ impl<'a> TypeScriptNamespace<'a, '_> {
219209
_ => {}
220210
}
221211
}
222-
continue;
223-
}
224-
// Retain when `only_remove_type_imports` is true or there are value references
225-
// The behavior is the same as `TypeScriptModule::transform_ts_import_equals`
226-
Statement::TSImportEqualsDeclaration(decl)
227-
if !self.only_remove_type_imports
228-
&& ctx
229-
.scoping()
230-
.get_resolved_references(decl.id.symbol_id())
231-
.all(Reference::is_type) =>
232-
{
233-
continue;
234212
}
235-
Statement::TSTypeAliasDeclaration(_) | Statement::TSInterfaceDeclaration(_) => {
236-
continue;
237-
}
238-
_ => {}
213+
_ => new_stmts.push(stmt),
239214
}
240-
new_stmts.push(stmt);
241215
}
242216

243217
if !Self::is_redeclaration_namespace(&ident, ctx) {

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,9 +2095,6 @@ rebuilt : ScopeId(0): ["M2"]
20952095
Scope children mismatch:
20962096
after transform: ScopeId(0): [ScopeId(1), ScopeId(4)]
20972097
rebuilt : ScopeId(0): [ScopeId(1)]
2098-
Bindings mismatch:
2099-
after transform: ScopeId(4): ["C", "T", "_M"]
2100-
rebuilt : ScopeId(1): ["C", "_M"]
21012098
Scope flags mismatch:
21022099
after transform: ScopeId(4): ScopeFlags(StrictMode | Function)
21032100
rebuilt : ScopeId(1): ScopeFlags(Function)
@@ -17964,9 +17961,6 @@ rebuilt : ScopeId(0): ["B"]
1796417961
Scope children mismatch:
1796517962
after transform: ScopeId(0): [ScopeId(1), ScopeId(3)]
1796617963
rebuilt : ScopeId(0): [ScopeId(1)]
17967-
Bindings mismatch:
17968-
after transform: ScopeId(3): ["A", "Y", "_B"]
17969-
rebuilt : ScopeId(1): ["A", "_B"]
1797017964
Scope flags mismatch:
1797117965
after transform: ScopeId(3): ScopeFlags(StrictMode | Function)
1797217966
rebuilt : ScopeId(1): ScopeFlags(Function)
@@ -20060,9 +20054,6 @@ rebuilt : ScopeId(1): ScopeFlags(Function)
2006020054
Scope children mismatch:
2006120055
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
2006220056
rebuilt : ScopeId(1): [ScopeId(2)]
20063-
Bindings mismatch:
20064-
after transform: ScopeId(4): ["Bug", "_editor", "i", "modes"]
20065-
rebuilt : ScopeId(3): ["Bug", "_editor", "i"]
2006620057
Scope flags mismatch:
2006720058
after transform: ScopeId(4): ScopeFlags(StrictMode | Function)
2006820059
rebuilt : ScopeId(3): ScopeFlags(Function)
@@ -20078,9 +20069,6 @@ rebuilt : ScopeId(14): ScopeFlags(Function)
2007820069
Scope children mismatch:
2007920070
after transform: ScopeId(15): [ScopeId(16), ScopeId(17)]
2008020071
rebuilt : ScopeId(14): [ScopeId(15)]
20081-
Bindings mismatch:
20082-
after transform: ScopeId(18): ["A1Alias1", "_B", "c", "i"]
20083-
rebuilt : ScopeId(16): ["_B", "c", "i"]
2008420072
Scope flags mismatch:
2008520073
after transform: ScopeId(18): ScopeFlags(StrictMode | Function)
2008620074
rebuilt : ScopeId(16): ScopeFlags(Function)
@@ -20926,9 +20914,6 @@ rebuilt : ScopeId(2): ScopeFlags(Function)
2092620914
Scope flags mismatch:
2092720915
after transform: ScopeId(4): ScopeFlags(StrictMode | Function)
2092820916
rebuilt : ScopeId(4): ScopeFlags(Function)
20929-
Bindings mismatch:
20930-
after transform: ScopeId(5): ["M", "_M2", "bar"]
20931-
rebuilt : ScopeId(5): ["_M2", "bar"]
2093220917
Scope flags mismatch:
2093320918
after transform: ScopeId(5): ScopeFlags(StrictMode | Function)
2093420919
rebuilt : ScopeId(5): ScopeFlags(Function)

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ after transform: SymbolId(1): Span { start: 31, end: 33 }
209209
rebuilt : SymbolId(1): Span { start: 0, end: 0 }
210210

211211
* namespace/import-=/input.ts
212-
Bindings mismatch:
213-
after transform: ScopeId(1): ["V", "X", "_N"]
214-
rebuilt : ScopeId(1): ["V", "_N"]
215212
Scope flags mismatch:
216213
after transform: ScopeId(1): ScopeFlags(StrictMode | Function)
217214
rebuilt : ScopeId(1): ScopeFlags(Function)

0 commit comments

Comments
 (0)