Skip to content

Commit

Permalink
fix corner cases in booleans & conditionals (#5695)
Browse files Browse the repository at this point in the history
fixes #5694
  • Loading branch information
alexlamsl authored Oct 3, 2022
1 parent 140e4e0 commit dabcc39
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
16 changes: 10 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ Compressor.prototype.compress = function(node) {

var identifier_atom = makePredicate("Infinity NaN undefined");
function is_lhs_read_only(lhs, compressor) {
if (lhs instanceof AST_Atom) return true;
if (lhs instanceof AST_ObjectIdentity) return true;
if (lhs instanceof AST_PropAccess) {
if (lhs.property === "__proto__") return true;
Expand Down Expand Up @@ -11589,10 +11590,12 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_Unary) return true;
}

function extract_lhs(node) {
if (node instanceof AST_Assign) return node.left;
function extract_lhs(node, compressor) {
if (node instanceof AST_Assign) return is_lhs_read_only(node.left, compressor) ? node : node.left;
if (node instanceof AST_Sequence) return extract_lhs(node.tail_node());
if (node instanceof AST_UnaryPrefix && UNARY_POSTFIX[node.operator]) return node.expression;
if (node instanceof AST_UnaryPrefix && UNARY_POSTFIX[node.operator]) {
return is_lhs_read_only(node.expression, compressor) ? node : node.expression;
}
return node;
}

Expand All @@ -11601,6 +11604,7 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_Sub) {
return repeatable(compressor, node.expression) && repeatable(compressor, node.property);
}
if (node instanceof AST_Symbol) return true;
return !node.has_side_effects(compressor);
}

Expand Down Expand Up @@ -11637,7 +11641,7 @@ Compressor.prototype.compress = function(node) {
if (seq !== self) return seq.optimize(compressor);
}
if (compressor.option("assignments") && lazy_op[self.operator]) {
var lhs = extract_lhs(self.left);
var lhs = extract_lhs(self.left, compressor);
var right = self.right;
// a || (a = x) ---> a = a || x
// (a = x) && (a = y) ---> a = (a = x) && y
Expand Down Expand Up @@ -11737,7 +11741,7 @@ Compressor.prototype.compress = function(node) {
var in_bool = false;
var parent = compressor.parent();
if (compressor.option("booleans")) {
var lhs = extract_lhs(self.left);
var lhs = extract_lhs(self.left, compressor);
if (lazy_op[self.operator] && !lhs.has_side_effects(compressor)) {
// a || a ---> a
// (a = x) && a --> a = x
Expand Down Expand Up @@ -12898,7 +12902,7 @@ Compressor.prototype.compress = function(node) {
}
var consequent = self.consequent;
var alternative = self.alternative;
var cond_lhs = extract_lhs(condition);
var cond_lhs = extract_lhs(condition, compressor);
if (repeatable(compressor, cond_lhs)) {
// x ? x : y ---> x || y
if (cond_lhs.equals(consequent)) return make_node(AST_Binary, self, {
Expand Down
17 changes: 17 additions & 0 deletions test/compress/booleans.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,20 @@ issue_5469: {
}
expect_stdout: "undefined"
}

issue_5694: {
options = {
booleans: true,
conditionals: true,
}
input: {
var undefined;
// Node.js v0.12~6 (vm): 42
console.log((undefined = 42) && undefined);
}
expect: {
var undefined;
console.log((undefined = 42) && undefined);
}
expect_stdout: true
}
2 changes: 0 additions & 2 deletions test/compress/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ self_comparison_1: {
comparisons: true,
}
input: {
var a, b;
a === a;
a !== b;
b.c === a.c;
b.c !== b.c;
}
expect: {
var a, b;
a == a;
a !== b;
b.c === a.c;
Expand Down
18 changes: 18 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3033,3 +3033,21 @@ issue_5673_2: {
}
expect_stdout: "PASS"
}

issue_5694: {
options = {
conditionals: true,
}
input: {
FORCE_EXEC = "async()=>{}";
var a = "foo";
// Node.js v0.12~6 (vm): foo
console.log((NaN = a) ? NaN : 42);
}
expect: {
FORCE_EXEC = "async()=>{}";
var a = "foo";
console.log((NaN = a) ? NaN : 42);
}
expect_stdout: "NaN"
}
38 changes: 19 additions & 19 deletions test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,33 +976,33 @@ nested_if_return: {
if_return: true,
}
input: {
function f(a, b, c, d, e, f, g, h, i, j, k, l, m, n) {
if (a) {
if (b)
return b;
if (c)
return d;
if (e)
return f;
if (g)
return h;
if (i) {
if (j)
return k;
function f() {
if (A) {
if (B)
return B;
if (C)
return D;
if (E)
return F;
if (G)
return H;
if (I) {
if (J)
return K;
return;
}
if (l) {
if (m)
if (L) {
if (M)
return;
return n;
return N;
}
}
}
}
expect: {
function f(a, b, c, d, e, f, g, h, i, j, k, l, m, n) {
if (a)
return b || (c ? d : e ? f : g ? h : i ? j ? k : void 0 : l && !m ? n : void 0);
function f() {
if (A)
return B || (C ? D : E ? F : G ? H : I ? J ? K : void 0 : L && !M ? N : void 0);
}
}
}
Expand Down

0 comments on commit dabcc39

Please sign in to comment.