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 ignore nullish coalescing #6272

Merged
merged 3 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,2 +1,2 @@
//// [logicalAssignment1.ts]
a && (a = "foo"), b || (b = "foo"), c = "foo", d && (d = 42), e || (e = 42), f = 42, g && (g = 42), h || (h = 42), i = 42;
a && (a = "foo"), b || (b = "foo"), c ?? (c = "foo"), d && (d = 42), e || (e = 42), f ?? (f = 42), g && (g = 42), h || (h = 42), i ?? (i = 42);
@@ -1,5 +1,5 @@
//// [logicalAssignment10.ts]
var _obj, _ref, _ref1, _ref2, count = 0, obj = {};
(_obj = obj)[_ref = ++count], _obj[_ref] = ++count, (_ref1 = ({
(_obj = obj)[_ref = ++count] ?? (_obj[_ref] = ++count), (_ref1 = ({
obj
}).obj)[_ref2 = ++count], _ref1[_ref2] = ++count;
}).obj)[_ref2 = ++count] ?? (_ref1[_ref2] = ++count);
@@ -1,3 +1,3 @@
//// [logicalAssignment11.ts]
let x;
(x ?? "x").length, (x ?? "x").length;
let x, d, e;
d ?? (d = x ?? "x"), d.length, e ?? (e = x ?? "x"), e.length;
@@ -1,3 +1,3 @@
//// [logicalAssignment11.ts]
let x, e;
(x ?? "x").length, (e ??= x ?? "x").length;
let x, d, e;
d ?? (d = x ?? "x"), d.length, (e ??= x ?? "x").length;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,3 +1,3 @@
//// [logicalAssignment3.ts]
var _a, _b, _c;
(_a = a).baz && (_a.baz = result.baz), (_b = b).baz || (_b.baz = result.baz), (_c = c).baz, _c.baz = result.baz;
(_a = a).baz && (_a.baz = result.baz), (_b = b).baz || (_b.baz = result.baz), (_c = c).baz ?? (_c.baz = result.baz);
5 changes: 1 addition & 4 deletions crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Expand Up @@ -683,10 +683,7 @@ where
match e {
Expr::Lit(..) | Expr::Ident(..) => true,

Expr::Bin(BinExpr {
op: op!("&&") | op!("||") | op!("??"),
..
}) => false,
Expr::Bin(BinExpr { op, .. }) if op.may_short_circuit() => false,

Expr::Bin(e) => {
self.is_return_arg_simple_enough_for_iife_eval(&e.left)
Expand Down
9 changes: 3 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Expand Up @@ -664,11 +664,8 @@ where
Expr::Paren(e) => return self.ignore_return_value(&mut e.expr),

Expr::Bin(BinExpr {
op: op!("&&") | op!("||"),
left,
right,
..
}) => {
op, left, right, ..
}) if op.may_short_circuit() => {
let ctx = Ctx {
dont_use_negated_iife: self.ctx.dont_use_negated_iife
|| self.options.side_effects,
Expand Down Expand Up @@ -1472,7 +1469,7 @@ where
fn visit_mut_bin_expr(&mut self, n: &mut BinExpr) {
{
let ctx = Ctx {
in_cond: self.ctx.in_cond || matches!(n.op, op!("&&") | op!("||") | op!("??")),
in_cond: self.ctx.in_cond || n.op.may_short_circuit(),
..self.ctx
};

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/ops.rs
Expand Up @@ -84,7 +84,7 @@ where
pub(super) fn remove_bin_paren(&mut self, n: &mut BinExpr) {
if let Expr::Bin(right) = &mut *n.right {
if right.op == n.op {
if matches!(n.op, op!("&&") | op!("||") | op!("??"))
if n.op.may_short_circuit()
|| (right.left.is_str() && right.op == op!(bin, "+"))
|| (n.left.is_str() && right.right.is_str())
{
Expand Down
5 changes: 2 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Expand Up @@ -1570,9 +1570,8 @@ where
return Ok(false);
}

match *op {
op!("&&") | op!("||") | op!("??") => return Ok(false),
_ => {}
if op.may_short_circuit() {
return Ok(false);
}

trace_op!("seq: Try right of bin");
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/tests/fixture/pr/6272/input.js
@@ -0,0 +1 @@
a ?? (a = b);
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/tests/fixture/pr/6272/output.js
@@ -0,0 +1 @@
a ?? (a = b);