Skip to content

Commit

Permalink
fix corner case in dead_code (#5031)
Browse files Browse the repository at this point in the history
fixes #5030
  • Loading branch information
alexlamsl committed Jun 23, 2021
1 parent 980dbde commit 7cbcd11
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
42 changes: 28 additions & 14 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9858,7 +9858,7 @@ merge(Compressor.prototype, {
OPT(AST_Sequence, function(self, compressor) {
var expressions = filter_for_side_effects();
var end = expressions.length - 1;
merge_conditional_assignments();
merge_assignments();
trim_right_for_undefined();
if (end == 0) {
self = maintain_this_binding(compressor, compressor.parent(), compressor.self(), expressions[0]);
Expand Down Expand Up @@ -9895,19 +9895,33 @@ merge(Compressor.prototype, {
}
}

function merge_conditional_assignments() {
if (!compressor.option("conditionals")) return;
function is_simple_assign(node) {
return node instanceof AST_Assign
&& node.operator == "="
&& node.left instanceof AST_SymbolRef
&& node.left.definition();
}

function merge_assignments() {
for (var i = 1; i < end; i++) {
var assign = expressions[i - 1];
if (!(assign instanceof AST_Assign)) continue;
if (assign.operator != "=") continue;
if (!(assign.left instanceof AST_SymbolRef)) continue;
var def = assign.left.definition();
var cond = to_conditional_assignment(compressor, def, assign.right, expressions[i]);
if (!cond) continue;
assign.right = cond;
expressions.splice(i, 1);
end--;
var prev = expressions[i - 1];
var def = is_simple_assign(prev);
if (!def) continue;
var expr = expressions[i];
if (compressor.option("conditionals")) {
var cond = to_conditional_assignment(compressor, def, prev.right, expr);
if (cond) {
prev.right = cond;
expressions.splice(i--, 1);
end--;
continue;
}
}
if (compressor.option("dead_code")
&& is_simple_assign(expr) === def
&& expr.right.is_constant_expression(def.scope.resolve())) {
expressions[--i] = prev.right;
}
}
}
});
Expand Down Expand Up @@ -11143,7 +11157,7 @@ merge(Compressor.prototype, {
var scan_scope = new TreeWalker(function(node) {
if (reachable) return true;
if (node instanceof AST_Lambda && node !== self) {
if (!(is_async(node) || is_generator(node))) {
if (!(node.name || is_async(node) || is_generator(node))) {
var parent = scan_scope.parent();
if (parent instanceof AST_Call && parent.expression === node) return;
}
Expand Down
49 changes: 49 additions & 0 deletions test/compress/dead-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,21 @@ last_assign_finally: {
expect_stdout: "PASS"
}

consecutive_assignments: {
options = {
dead_code: true,
}
input: {
while (a = void 0, a = "PASS", console.log(a));
var a;
}
expect: {
while (void 0, a = "PASS", console.log(a));
var a;
}
expect_stdout: "PASS"
}

issue_3578: {
options = {
dead_code: true,
Expand Down Expand Up @@ -1584,3 +1599,37 @@ issue_4570: {
}
expect_stdout: "NaN"
}

issue_5030: {
options = {
dead_code: true,
}
input: {
(function(a, b) {
a = function f() {
if (a)
if (b--)
setImmediate(f);
else
console.log("FAIL");
else
console.log("PASS");
}();
})(42, 1);
}
expect: {
(function(a, b) {
a = function f() {
if (a)
if (b--)
setImmediate(f);
else
console.log("FAIL");
else
console.log("PASS");
}();
})(42, 1);
}
expect_stdout: "PASS"
node_version: ">=0.12"
}
11 changes: 7 additions & 4 deletions test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ inline_loop_1: {
inline_loop_2: {
options = {
inline: true,
sequences: true,
toplevel: true,
unused: true,
}
input: {
while (function(a = [ "PASS" ]) {
Expand All @@ -432,10 +434,11 @@ inline_loop_2: {
}());
}
expect: {
while (a = [ "PASS" ], a = function f(b) {
console.log(a[b]);
}(0), void 0) ;
var a;
while (a = [ "PASS" ],
b = void 0,
b = 0,
void (a = void console.log(a[b])));
var a, b;
}
expect_stdout: "PASS"
node_version: ">=6"
Expand Down

0 comments on commit 7cbcd11

Please sign in to comment.