Skip to content

Commit

Permalink
feat(transformer/arrow-function-converter): move scope to changed sco…
Browse files Browse the repository at this point in the history
…pe for this_var if scope have changed
  • Loading branch information
Dunqing committed Nov 6, 2024
1 parent bfb9366 commit 7d1449a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 48 deletions.
15 changes: 14 additions & 1 deletion crates/oxc_transformer/src/common/arrow_function_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
Expand Down
50 changes: 3 additions & 47 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 377/1078
Passed: 381/1078

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d1449a

Please sign in to comment.