diff --git a/crates/oxc_transformer/src/typescript/annotations.rs b/crates/oxc_transformer/src/typescript/annotations.rs index 31075b35e4ec5..ef8313c4fd387 100644 --- a/crates/oxc_transformer/src/typescript/annotations.rs +++ b/crates/oxc_transformer/src/typescript/annotations.rs @@ -78,20 +78,20 @@ impl<'a> Traverse<'a> for TypeScriptAnnotations<'a, '_> { // Keep the export declaration if there are no export specifiers true } else { - decl.specifiers.retain(|specifier| { - !(specifier.export_kind.is_type() - || self.type_identifier_names.contains(&specifier.exported.name()) - || matches!( - &specifier.local, ModuleExportName::IdentifierReference(ident) - if ctx.scoping().get_reference(ident.reference_id()).is_type() - )) - }); + decl.specifiers + .retain(|specifier| self.can_retain_export_specifier(specifier, ctx)); // Keep the export declaration if there are still specifiers after removing type exports !decl.specifiers.is_empty() } } Statement::ExportAllDeclaration(decl) => !decl.export_kind.is_type(), - Statement::ExportDefaultDeclaration(decl) => !decl.is_typescript_syntax(), + Statement::ExportDefaultDeclaration(decl) => { + !decl.is_typescript_syntax() + && !matches!( + &decl.declaration, + ExportDefaultDeclarationKind::Identifier(ident) if Self::is_refers_to_type(ident, ctx) + ) + } Statement::ImportDeclaration(decl) => { if decl.import_kind.is_type() { false @@ -614,6 +614,30 @@ impl<'a> TypeScriptAnnotations<'a, '_> { self.is_jsx_imports(&id.name) } + + fn can_retain_export_specifier( + &self, + specifier: &ExportSpecifier<'a>, + ctx: &TraverseCtx<'a>, + ) -> bool { + if specifier.export_kind.is_type() + || self.type_identifier_names.contains(&specifier.exported.name()) + { + return false; + } + !matches!(&specifier.local, ModuleExportName::IdentifierReference(ident) if Self::is_refers_to_type(ident, ctx)) + } + + fn is_refers_to_type(ident: &IdentifierReference<'a>, ctx: &TraverseCtx<'a>) -> bool { + let scoping = ctx.scoping(); + let reference = scoping.get_reference(ident.reference_id()); + + reference.symbol_id().is_some_and(|symbol_id| { + reference.is_type() + || scoping.symbol_flags(symbol_id).is_ambient() + && scoping.symbol_redeclarations(symbol_id).iter().all(|r| r.flags.is_ambient()) + }) + } } struct Assignment<'a> { diff --git a/tasks/coverage/snapshots/semantic_babel.snap b/tasks/coverage/snapshots/semantic_babel.snap index 3e0b4469922c3..9cb7752ff06fd 100644 --- a/tasks/coverage/snapshots/semantic_babel.snap +++ b/tasks/coverage/snapshots/semantic_babel.snap @@ -1498,15 +1498,6 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "foo": -after transform: SymbolId(0) "foo" -rebuilt : -Reference flags mismatch for "foo": -after transform: ReferenceId(0): ReferenceFlags(Read) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["foo"] tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/input.ts semantic error: Bindings mismatch: @@ -1515,15 +1506,6 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "foo": -after transform: SymbolId(0) "foo" -rebuilt : -Reference flags mismatch for "foo": -after transform: ReferenceId(0): ReferenceFlags(Read) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["foo"] tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/input.ts semantic error: Scope flags mismatch: diff --git a/tasks/coverage/snapshots/semantic_typescript.snap b/tasks/coverage/snapshots/semantic_typescript.snap index d0f7404bb25bf..c9fb0c4046086 100644 --- a/tasks/coverage/snapshots/semantic_typescript.snap +++ b/tasks/coverage/snapshots/semantic_typescript.snap @@ -361,15 +361,6 @@ tasks/coverage/typescript/tests/cases/compiler/allowSyntheticDefaultImportsCanPa semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "Color": -after transform: SymbolId(0) "Color" -rebuilt : -Reference flags mismatch for "Color": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["Color"] tasks/coverage/typescript/tests/cases/compiler/ambientClassDeclarationWithExtends.ts semantic error: Bindings mismatch: @@ -8163,29 +8154,11 @@ tasks/coverage/typescript/tests/cases/compiler/declarationEmitToDeclarationDirWi semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "Foo": -after transform: SymbolId(0) "Foo" -rebuilt : -Reference flags mismatch for "Foo": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitToDeclarationDirWithDeclarationOption.ts semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "Foo": -after transform: SymbolId(0) "Foo" -rebuilt : -Reference flags mismatch for "Foo": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitTopLevelNodeFromCrossFile.ts semantic error: Scope children mismatch: @@ -8291,15 +8264,6 @@ tasks/coverage/typescript/tests/cases/compiler/declarationEmitWithComposite.ts semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "Foo": -after transform: SymbolId(0) "Foo" -rebuilt : -Reference flags mismatch for "Foo": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts semantic error: Bindings mismatch: @@ -13152,15 +13116,6 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "m": -after transform: SymbolId(0) "m" -rebuilt : -Reference flags mismatch for "m": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["m"] tasks/coverage/typescript/tests/cases/compiler/exportDefaultImportedType.ts semantic error: Scope children mismatch: @@ -20643,15 +20598,6 @@ tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationOfAlias.ts semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "I": -after transform: SymbolId(0) "I" -rebuilt : -Reference flags mismatch for "I": -after transform: ReferenceId(0): ReferenceFlags(Type) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["I"] tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsBundledOutput1.ts semantic error: Scope children mismatch: @@ -28735,15 +28681,9 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(4), ScopeId(6), ScopeId(7), ScopeId(8), ScopeId(14), ScopeId(16), ScopeId(19)] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "spected": -after transform: SymbolId(0) "spected" -rebuilt : -Reference flags mismatch for "spected": -after transform: ReferenceId(73): ReferenceFlags(Read) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) Unresolved references mismatch: after transform: ["Array", "Partial", "ReadonlyArray"] -rebuilt : ["spected"] +rebuilt : [] tasks/coverage/typescript/tests/cases/compiler/specializationError.ts semantic error: Scope children mismatch: @@ -31954,15 +31894,6 @@ tasks/coverage/typescript/tests/cases/compiler/uniqueSymbolPropertyDeclarationEm semantic error: Bindings mismatch: after transform: ScopeId(0): ["Op"] rebuilt : ScopeId(0): [] -Reference symbol mismatch for "Op": -after transform: SymbolId(0) "Op" -rebuilt : -Reference flags mismatch for "Op": -after transform: ReferenceId(0): ReferenceFlags(Read) -rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) -Unresolved references mismatch: -after transform: [] -rebuilt : ["Op"] tasks/coverage/typescript/tests/cases/compiler/unknownLikeUnionObjectFlagsNotPropagated.ts semantic error: Scope children mismatch: diff --git a/tasks/transform_conformance/snapshots/babel.snap.md b/tasks/transform_conformance/snapshots/babel.snap.md index d304ae57a6845..71d5028edf4d5 100644 --- a/tasks/transform_conformance/snapshots/babel.snap.md +++ b/tasks/transform_conformance/snapshots/babel.snap.md @@ -1727,7 +1727,36 @@ after transform: SymbolId(3): [Span { start: 147, end: 154 }, Span { start: 225, rebuilt : SymbolId(2): [] * exports/declared-types/input.ts -x Output mismatch +Bindings mismatch: +after transform: ScopeId(0): ["BB", "BB2", "C", "C2", "E", "M", "N", "f", "foo", "x"] +rebuilt : ScopeId(0): ["BB", "BB2", "C2", "foo"] +Scope children mismatch: +after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(7), ScopeId(8), ScopeId(9), ScopeId(10), ScopeId(11), ScopeId(12), ScopeId(13), ScopeId(14), ScopeId(15), ScopeId(16), ScopeId(17), ScopeId(18)] +rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5)] +Bindings mismatch: +after transform: ScopeId(12): ["BB", "K"] +rebuilt : ScopeId(2): ["BB"] +Scope flags mismatch: +after transform: ScopeId(12): ScopeFlags(StrictMode) +rebuilt : ScopeId(2): ScopeFlags(StrictMode | Function) +Bindings mismatch: +after transform: ScopeId(13): ["BB", "L"] +rebuilt : ScopeId(3): ["BB"] +Scope flags mismatch: +after transform: ScopeId(13): ScopeFlags(StrictMode) +rebuilt : ScopeId(3): ScopeFlags(StrictMode | Function) +Scope flags mismatch: +after transform: ScopeId(16): ScopeFlags(StrictMode) +rebuilt : ScopeId(4): ScopeFlags(StrictMode | Function) +Symbol flags mismatch for "BB": +after transform: SymbolId(11): SymbolFlags(RegularEnum) +rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable) +Symbol redeclarations mismatch for "BB": +after transform: SymbolId(11): [Span { start: 479, end: 481 }, Span { start: 495, end: 497 }] +rebuilt : SymbolId(1): [] +Symbol flags mismatch for "BB2": +after transform: SymbolId(16): SymbolFlags(RegularEnum) +rebuilt : SymbolId(4): SymbolFlags(FunctionScopedVariable) * exports/default-function/input.ts Scope children mismatch: