Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Don't inline invalid LHS into an update argument #6680

Merged
merged 6 commits into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Expand Up @@ -733,6 +733,11 @@ where
})
.cloned()
{
if !matches!(&*value, Expr::Ident(..) | Expr::Member(..)) && self.ctx.is_update_arg
{
return;
}

self.changed = true;
report_change!("inline: Replacing a variable `{}` with cheap expression", i);

Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Expand Up @@ -1536,6 +1536,7 @@ where
},
is_lhs_of_assign: false,
is_exact_lhs_of_assign: false,
is_update_arg: false,
..self.ctx
};
e.callee.visit_mut_with(&mut *self.with_ctx(ctx));
Expand Down Expand Up @@ -1566,6 +1567,7 @@ where
is_this_aware_callee: false,
is_lhs_of_assign: false,
is_exact_lhs_of_assign: false,
is_update_arg: false,
..self.ctx
};
// TODO: Prevent inline if callee is unknown.
Expand All @@ -1583,6 +1585,7 @@ where
{
let ctx = Ctx {
dont_invoke_iife: true,
is_update_arg: false,
..self.ctx
};
n.super_class.visit_mut_with(&mut *self.with_ctx(ctx));
Expand All @@ -1591,6 +1594,7 @@ where
{
let ctx = Ctx {
in_strict: true,
is_update_arg: false,
..self.ctx
};
n.body.visit_mut_with(&mut *self.with_ctx(ctx));
Expand Down Expand Up @@ -2184,6 +2188,7 @@ where
let ctx = Ctx {
in_obj_of_non_computed_member: !n.prop.is_computed(),
is_exact_lhs_of_assign: false,
is_update_arg: false,
..self.ctx
};
n.obj.visit_mut_with(&mut *self.with_ctx(ctx));
Expand All @@ -2192,6 +2197,7 @@ where
let ctx = Ctx {
is_exact_lhs_of_assign: false,
is_lhs_of_assign: false,
is_update_arg: false,
..self.ctx
};
c.visit_mut_with(&mut *self.with_ctx(ctx));
Expand Down
13 changes: 13 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Expand Up @@ -10394,3 +10394,16 @@ fn issue_6528() {
"###,
)
}

#[test]
fn issue_6641() {
run_default_exec_test(
r###"
const iota = (i => () => 1 << ++i)(-1);

const a = iota(), b = iota();

console.log(a, b);
"###,
)
}