feat(transformer/arrow-function-converter): move scope to changed scope for this_var if scope have changed (#7125)

The scope can be moved or deleted, we need to check whether the scope id is the same as the container's scope id, if not, we need to move the `this_var` to the target scope id.
This commit is contained in:
Dunqing 2024-11-06 03:02:36 +00:00
parent df772411ea
commit b6a575009a
2 changed files with 17 additions and 48 deletions

View file

@ -146,6 +146,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
if let Some(this_var) = self.this_var_stack.take_last() {
self.insert_this_var_statement_at_the_top_of_statements(
&mut program.body,
program.scope_id.get().unwrap(),
&this_var,
ctx,
);
@ -183,6 +184,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
self.insert_this_var_statement_at_the_top_of_statements(
&mut body.statements,
func.scope_id.get().unwrap(),
&this_var,
ctx,
);
@ -205,6 +207,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
if let Some(this_var) = self.this_var_stack.pop() {
self.insert_this_var_statement_at_the_top_of_statements(
&mut block.body,
block.scope_id.get().unwrap(),
&this_var,
ctx,
);
@ -448,9 +451,19 @@ impl<'a> ArrowFunctionConverter<'a> {
fn insert_this_var_statement_at_the_top_of_statements(
&mut self,
statements: &mut ArenaVec<'a, Statement<'a>>,
target_scope_id: ScopeId,
this_var: &BoundIdentifier<'a>,
ctx: &TraverseCtx<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let symbol_id = this_var.symbol_id;
let scope_id = ctx.symbols().get_scope_id(symbol_id);
// Because scope can be moved or deleted, we need to check the scope id again,
// if it's different, we need to move the binding to the target scope.
if target_scope_id != scope_id {
ctx.scopes_mut().move_binding(scope_id, target_scope_id, &this_var.name);
ctx.symbols_mut().set_scope_id(symbol_id, target_scope_id);
}
let variable_declarator = ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,

View file

@ -1,6 +1,6 @@
commit: d20b314c
Passed: 377/1078
Passed: 381/1078
# All Passed:
* babel-plugin-transform-class-static-block
@ -1450,40 +1450,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-async-generator-functions (15/19)
* async-generators/class-method/input.js
Bindings mismatch:
after transform: ScopeId(0): ["C", "_this"]
rebuilt : ScopeId(0): ["C"]
Bindings mismatch:
after transform: ScopeId(3): []
rebuilt : ScopeId(2): ["_this"]
Symbol scope ID mismatch for "_this":
after transform: SymbolId(1): ScopeId(0)
rebuilt : SymbolId(1): ScopeId(2)
* async-generators/object-method/input.js
Bindings mismatch:
after transform: ScopeId(0): ["_this"]
rebuilt : ScopeId(0): []
Bindings mismatch:
after transform: ScopeId(2): []
rebuilt : ScopeId(1): ["_this"]
Symbol scope ID mismatch for "_this":
after transform: SymbolId(0): ScopeId(0)
rebuilt : SymbolId(0): ScopeId(1)
* async-generators/static-method/input.js
Bindings mismatch:
after transform: ScopeId(0): ["C", "_this"]
rebuilt : ScopeId(0): ["C"]
Bindings mismatch:
after transform: ScopeId(3): []
rebuilt : ScopeId(2): ["_this"]
Symbol scope ID mismatch for "_this":
after transform: SymbolId(1): ScopeId(0)
rebuilt : SymbolId(1): ScopeId(2)
# babel-plugin-transform-async-generator-functions (18/19)
* nested/arrows-in-declaration/input.js
x Output mismatch
@ -1652,7 +1619,7 @@ x Output mismatch
x Output mismatch
# babel-plugin-transform-async-to-generator (9/24)
# babel-plugin-transform-async-to-generator (10/24)
* assumption-ignoreFunctionLength-true/basic/input.mjs
x Output mismatch
@ -1695,17 +1662,6 @@ x Output mismatch
* regression/in-uncompiled-class-fields/input.js
x Output mismatch
* regression/regression-2765/input.js
Bindings mismatch:
after transform: ScopeId(10): []
rebuilt : ScopeId(6): ["_this2"]
Bindings mismatch:
after transform: ScopeId(4): ["_this2", "c"]
rebuilt : ScopeId(7): ["c"]
Symbol scope ID mismatch for "_this2":
after transform: SymbolId(8): ScopeId(4)
rebuilt : SymbolId(6): ScopeId(6)
# babel-plugin-transform-exponentiation-operator (3/4)
* regression/4349/input.js