mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(transformer): arrow function transform: prevent stack getting out of sync (#5941)
Fix tiny bug in arrow function transform. Similar to #5828 - cannot assume that other transforms haven't altered AST between `enter_*` and `exit_*`. In this case, pushing/popping to the stack cannot depend on `func.body.is_some()`, as that could change in between `enter_function` and `exit_function`.
This commit is contained in:
parent
155d7fcba3
commit
87323c6220
1 changed files with 4 additions and 8 deletions
|
|
@ -122,10 +122,8 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
|
fn enter_function(&mut self, _func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||||
if func.body.is_some() {
|
self.this_var_stack.push(None);
|
||||||
self.this_var_stack.push(None);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ```ts
|
/// ```ts
|
||||||
|
|
@ -140,12 +138,10 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
|
||||||
/// ```
|
/// ```
|
||||||
/// Insert the var _this = this; statement outside the arrow function
|
/// Insert the var _this = this; statement outside the arrow function
|
||||||
fn exit_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
|
fn exit_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||||
let Some(body) = func.body.as_mut() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let this_var = self.this_var_stack.pop().unwrap();
|
let this_var = self.this_var_stack.pop().unwrap();
|
||||||
if let Some(this_var) = this_var {
|
if let Some(this_var) = this_var {
|
||||||
|
let Some(body) = &mut func.body else { unreachable!() };
|
||||||
|
|
||||||
self.insert_this_var_statement_at_the_top_of_statements(
|
self.insert_this_var_statement_at_the_top_of_statements(
|
||||||
&mut body.statements,
|
&mut body.statements,
|
||||||
&this_var,
|
&this_var,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue