Skip to content

Commit

Permalink
perf(transformer/arrow-function): change to a correct scope_id when i…
Browse files Browse the repository at this point in the history
…nserting this
  • Loading branch information
Dunqing committed Oct 31, 2024
1 parent 3aacc67 commit 47e0e3a
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'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 @@ -196,6 +197,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {

self.insert_this_var_statement_at_the_top_of_statements(
&mut body.statements,
func.scope_id.get().unwrap(),
&this_var,
ctx,
);
Expand All @@ -210,6 +212,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'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 @@ -268,26 +271,15 @@ impl<'a> ArrowFunctions<'a> {
ctx: &mut TraverseCtx<'a>,
) -> Option<IdentifierReference<'a>> {
// Find arrow function we are currently in (if we are)
let arrow_scope_id = Self::get_arrow_function_scope(ctx)?;
Self::get_arrow_function_scope(ctx)?;

// TODO(improve-on-babel): We create a new UID for every scope. This is pointless, as only one
// `this` can be in scope at a time. We could create a single `_this` UID and reuse it in each
// scope. But this does not match output for some of Babel's test cases.
// <https://github.com/oxc-project/oxc/pull/5840>
let this_var = self.this_var_stack.last_or_init(|| {
let target_scope_id = ctx
.scopes()
.ancestors(arrow_scope_id)
// Skip arrow function scope
.skip(1)
.find(|&scope_id| {
let scope_flags = ctx.scopes().get_flags(scope_id);
scope_flags.intersects(
ScopeFlags::Function | ScopeFlags::Top | ScopeFlags::ClassStaticBlock,
) && !scope_flags.contains(ScopeFlags::Arrow)
})
.unwrap();
ctx.generate_uid("this", target_scope_id, SymbolFlags::FunctionScopedVariable)
let scope_id = ctx.scopes().get_parent_id(ctx.current_scope_id()).unwrap();
ctx.generate_uid("this", scope_id, SymbolFlags::FunctionScopedVariable)
});
Some(this_var.create_spanned_read_reference(span, ctx))
}
Expand Down Expand Up @@ -406,9 +398,16 @@ impl<'a> ArrowFunctions<'a> {
fn insert_this_var_statement_at_the_top_of_statements(
&mut self,
statements: &mut ArenaVec<'a, Statement<'a>>,
new_scope_id: ScopeId,
this_var: &BoundIdentifier<'a>,
ctx: &TraverseCtx<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let origin_scope_id = ctx.symbols().get_scope_id(this_var.symbol_id);
if origin_scope_id != new_scope_id {
ctx.symbols_mut().set_scope_id(this_var.symbol_id, new_scope_id);
ctx.scopes_mut().move_binding(origin_scope_id, new_scope_id, &this_var.name);
}

let variable_declarator = ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
Expand Down

0 comments on commit 47e0e3a

Please sign in to comment.