mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 18:28:13 +03:00
fix(es/minifier): Prepend vars when dropping vars in a for loop initializer (#5930)
**Description:** The minifier appends initializer of for statements instead of prepending it. **Related issue:** - https://github.com/vercel/next.js/discussions/30237#discussioncomment-3704795
This commit is contained in:
parent
a2b3063784
commit
f2224132c1
@ -22,7 +22,6 @@ for(;;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(new C();;);
|
||||
for(new C();;);
|
||||
for(new D();;);
|
||||
@ -33,3 +32,4 @@ for(;;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(new M.A();;);
|
||||
for(;;);
|
||||
|
@ -28,7 +28,6 @@ for(!function(M) {
|
||||
}(M || (M = {}));;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
for(new C();;);
|
||||
for(new D();;);
|
||||
for(;;);
|
||||
@ -43,4 +42,4 @@ for(new C(), new C2(), new D();;);
|
||||
for(new D();;);
|
||||
for(;;);
|
||||
for(;;);
|
||||
M.A;
|
||||
for(M.A;;);
|
||||
|
@ -1358,6 +1358,20 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_with_prepend<N>(&mut self, n: &mut N)
|
||||
where
|
||||
N: VisitMutWith<Self>,
|
||||
{
|
||||
let mut old_prepend_stmts = self.prepend_stmts.take();
|
||||
let old_append_stmts = self.append_stmts.take();
|
||||
n.visit_mut_with(self);
|
||||
old_prepend_stmts.append(&mut *self.prepend_stmts);
|
||||
old_prepend_stmts.append(&mut *self.append_stmts);
|
||||
|
||||
self.prepend_stmts = old_prepend_stmts;
|
||||
self.append_stmts = old_append_stmts;
|
||||
}
|
||||
}
|
||||
|
||||
impl<M> VisitMut for Optimizer<'_, M>
|
||||
@ -1895,7 +1909,7 @@ where
|
||||
is_exact_lhs_of_assign: n.left.is_pat(),
|
||||
..self.ctx
|
||||
};
|
||||
n.left.visit_mut_with(&mut *self.with_ctx(ctx));
|
||||
self.with_ctx(ctx).visit_with_prepend(&mut n.left);
|
||||
}
|
||||
|
||||
{
|
||||
@ -1917,7 +1931,7 @@ where
|
||||
is_exact_lhs_of_assign: n.left.is_pat(),
|
||||
..self.ctx
|
||||
};
|
||||
n.left.visit_mut_with(&mut *self.with_ctx(ctx));
|
||||
self.with_ctx(ctx).visit_with_prepend(&mut n.left);
|
||||
}
|
||||
|
||||
{
|
||||
@ -1931,15 +1945,16 @@ where
|
||||
|
||||
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
|
||||
fn visit_mut_for_stmt(&mut self, s: &mut ForStmt) {
|
||||
self.visit_with_prepend(&mut s.init);
|
||||
|
||||
s.test.visit_mut_with(self);
|
||||
s.update.visit_mut_with(self);
|
||||
|
||||
let ctx = Ctx {
|
||||
executed_multiple_time: true,
|
||||
..self.ctx
|
||||
};
|
||||
|
||||
s.init.visit_mut_with(self);
|
||||
s.test.visit_mut_with(self);
|
||||
s.update.visit_mut_with(self);
|
||||
|
||||
s.body.visit_mut_with(&mut *self.with_ctx(ctx));
|
||||
|
||||
self.with_ctx(ctx).optimize_init_of_for_stmt(s);
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user