feat(transformer): remove typescript symbols after transform (#8069)

part of #7460
This commit is contained in:
Boshen 2024-12-23 08:24:54 +00:00
parent 8b54d898b5
commit e632a7b158
8 changed files with 2364 additions and 11393 deletions

View file

@ -8,9 +8,11 @@ use oxc_syntax::{
node::NodeId,
reference::ReferenceId,
scope::{ScopeFlags, ScopeId},
symbol::SymbolId,
symbol::{SymbolFlags, SymbolId},
};
use crate::SymbolTable;
pub(crate) type Bindings<'a> = hashbrown::HashMap<&'a str, SymbolId, FxBuildHasher, &'a Allocator>;
pub type UnresolvedReferences<'a> =
hashbrown::HashMap<&'a str, ArenaVec<'a, ReferenceId>, FxBuildHasher, &'a Allocator>;
@ -462,4 +464,19 @@ impl ScopeTree {
});
}
}
pub fn delete_typescript_bindings(&mut self, symbol_table: &SymbolTable) {
self.cell.with_dependent_mut(|_allocator, inner| {
for bindings in &mut inner.bindings {
bindings.retain(|_name, symbol_id| {
let flags = symbol_table.get_flags(*symbol_id);
!flags.intersects(
SymbolFlags::TypeAlias
| SymbolFlags::Interface
| SymbolFlags::TypeParameter,
)
});
}
});
}
}

View file

@ -79,6 +79,7 @@ impl<'a, 'ctx> Traverse<'a> for TypeScript<'a, 'ctx> {
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.annotations.exit_program(program, ctx);
self.module.exit_program(program, ctx);
ctx.scoping.delete_typescript_bindings();
}
fn enter_arrow_function_expression(

View file

@ -435,6 +435,10 @@ impl TraverseScoping {
.map(CompactStr::from)
.collect()
}
pub fn delete_typescript_bindings(&mut self) {
self.scopes.delete_typescript_bindings(&self.symbols);
}
}
/// Create base for UID name based on provided `name`.

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
semantic_misc Summary:
AST Parsed : 30/30 (100.00%)
Positive Passed: 18/30 (60.00%)
Positive Passed: 19/30 (63.33%)
tasks/coverage/misc/pass/oxc-1288.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["from"]
@ -12,37 +12,22 @@ after transform: ScopeId(0): ["infer", "target", "type"]
rebuilt : ScopeId(0): []
tasks/coverage/misc/pass/oxc-2087.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["Helpers"]
rebuilt : ScopeId(0): []
Scope children mismatch:
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
tasks/coverage/misc/pass/oxc-2394.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []
Scope children mismatch:
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
tasks/coverage/misc/pass/oxc-2592.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["Foo"]
rebuilt : ScopeId(0): []
Scope children mismatch:
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
tasks/coverage/misc/pass/oxc-3443.tsx
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["T"]
rebuilt : ScopeId(1): []
Bindings mismatch:
after transform: ScopeId(2): ["T"]
rebuilt : ScopeId(2): []
Unresolved references mismatch:
semantic error: Unresolved references mismatch:
after transform: ["F"]
rebuilt : []
@ -176,23 +161,12 @@ after transform: SymbolId(14): SymbolFlags(RegularEnum)
rebuilt : SymbolId(14): SymbolFlags(FunctionScopedVariable)
tasks/coverage/misc/pass/oxc-5177.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["Bang", "Foo"]
rebuilt : ScopeId(0): ["Bang"]
Scope children mismatch:
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
tasks/coverage/misc/pass/oxc-5955.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["B", "C", "a"]
rebuilt : ScopeId(0): ["B", "a"]
Scope children mismatch:
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
tasks/coverage/misc/pass/swc-7187.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["K"]
rebuilt : ScopeId(0): []

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
commit: 54a8389f
Passed: 602/927
Passed: 604/927
# All Passed:
* babel-plugin-transform-class-static-block
@ -1111,7 +1111,7 @@ x Output mismatch
x Output mismatch
# babel-preset-typescript (10/17)
# babel-preset-typescript (11/17)
* jsx-compat/ts-invalid/input.ts
x Expected `>` but found `/`
@ -1130,11 +1130,6 @@ Unresolved references mismatch:
after transform: ["T", "x"]
rebuilt : ["x"]
* node-extensions/type-param-arrow-in-ts/input.ts
Bindings mismatch:
after transform: ScopeId(1): ["T"]
rebuilt : ScopeId(1): []
* opts/optimizeConstEnums/input.ts
x Output mismatch
@ -1145,7 +1140,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-typescript (46/156)
# babel-plugin-transform-typescript (47/156)
* cast/as-expression/input.ts
Unresolved references mismatch:
after transform: ["T", "x"]
@ -1181,9 +1176,6 @@ rebuilt : ["x"]
* class/head/input.ts
Bindings mismatch:
after transform: ScopeId(1): ["T"]
rebuilt : ScopeId(1): []
Unresolved references mismatch:
after transform: ["D", "I"]
rebuilt : ["D"]
@ -1208,7 +1200,7 @@ rebuilt : ScopeId(0): []
* declarations/erased/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["E", "I", "M", "N", "T", "m", "x"]
after transform: ScopeId(0): ["E", "M", "N", "m", "x"]
rebuilt : ScopeId(0): []
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(7), ScopeId(8)]
@ -1509,9 +1501,6 @@ after transform: ScopeId(0): [SymbolId(0), SymbolId(1)]
rebuilt : ScopeId(0): [SymbolId(0), SymbolId(1)]
* exports/export-type/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
@ -1530,9 +1519,6 @@ rebuilt : ScopeId(0): ["C"]
x Output mismatch
* exports/issue-9916-1/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["PromiseRejectCb", "PromiseResolveCb", "a"]
rebuilt : ScopeId(0): ["a"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): []
@ -1541,9 +1527,6 @@ after transform: ["PromiseLike"]
rebuilt : []
* exports/issue-9916-2/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["PromiseRejectCb", "PromiseResolveCb"]
rebuilt : ScopeId(0): []
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): []
@ -1552,9 +1535,6 @@ after transform: ["PromiseLike"]
rebuilt : []
* exports/issue-9916-3/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["PromiseRejectCb", "PromiseResolveCb", "a"]
rebuilt : ScopeId(0): ["a"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): []
@ -1577,11 +1557,6 @@ Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
* function/parameters/input.ts
Bindings mismatch:
after transform: ScopeId(1): ["T", "x", "y"]
rebuilt : ScopeId(1): ["x", "y"]
* imports/elide-preact/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["FooBar", "Fragment", "h", "x"]
@ -1626,7 +1601,7 @@ rebuilt : ScopeId(0): ["A", "B"]
* imports/elision-locations/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["A", "B", "C", "Class", "D", "E", "F", "G", "H", "Iface", "x", "y"]
after transform: ScopeId(0): ["A", "B", "C", "Class", "D", "E", "F", "G", "H", "x", "y"]
rebuilt : ScopeId(0): ["A", "Class", "x", "y"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
@ -1750,9 +1725,6 @@ rebuilt : ScopeId(0): []
x Output mismatch
* lvalues/TSTypeParameterInstantiation/input.ts
Bindings mismatch:
after transform: ScopeId(1): ["M"]
rebuilt : ScopeId(1): []
Symbol reference IDs mismatch for "AbstractClass":
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(3)]
rebuilt : SymbolId(0): [ReferenceId(0)]

View file

@ -131,7 +131,7 @@ Missing ReferenceId: "_Name"
Missing ReferenceId: "Name"
Missing ReferenceId: "Name"
Bindings mismatch:
after transform: ScopeId(0): ["Baq", "Bar", "Baz", "Foo", "Func", "Im", "Name", "Ok", "T"]
after transform: ScopeId(0): ["Bar", "Foo", "Func", "Im", "Name", "Ok"]
rebuilt : ScopeId(0): ["Bar", "Foo", "Func", "Im", "Name", "Ok", "T"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(7)]
@ -159,14 +159,14 @@ after transform: SymbolId(7) "Name"
rebuilt : SymbolId(5) "Name"
* exports/type-and-non-type/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["ToastProps", "ToastViewport"]
rebuilt : ScopeId(0): ["ToastViewport"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
* redeclarations/input.ts
Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): ["A", "B", "T"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): []