Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand Down
18 changes: 0 additions & 18 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 : <None>
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:
Expand All @@ -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 : <None>
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:
Expand Down
71 changes: 1 addition & 70 deletions tasks/coverage/snapshots/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down Expand Up @@ -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 : <None>
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:
Expand Down
31 changes: 30 additions & 1 deletion tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading