Skip to content

Commit

Permalink
fix corner case in collapse_vars (#3521)
Browse files Browse the repository at this point in the history
fixes #3520
  • Loading branch information
alexlamsl committed Oct 23, 2019
1 parent 370c8e0 commit 0f7aa41
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/compress.js
Expand Up @@ -1128,6 +1128,8 @@ merge(Compressor.prototype, {
if (!stop_if_hit && in_conditional(node, parent)) {
stop_if_hit = parent;
}
// Skip transient nodes caused by single-use variable replacement
if (node.single_use && parent instanceof AST_VarDef && parent.value === node) return node;
// Replace variable with assignment when found
var hit_rhs;
if (can_replace
Expand Down Expand Up @@ -3737,9 +3739,7 @@ merge(Compressor.prototype, {
def.value = null;
head.push(def);
} else {
var value = def.value
&& !def.value.single_use
&& def.value.drop_side_effect_free(compressor);
var value = def.value && def.value.drop_side_effect_free(compressor);
if (value) {
AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
side_effects.push(value);
Expand Down
38 changes: 38 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -6258,3 +6258,41 @@ cond_sequence_return: {
}
expect_stdout: "2"
}

issue_3520: {
options = {
collapse_vars: true,
reduce_vars: true,
unused: true,
}
input: {
var a = 0;
var b = function(c) {
for (var i = 2; --i >= 0;) {
(function f() {
c = 0;
var i = void 0;
var f = f && f[i];
})();
a += b;
c && b++;
}
}(b = 1);
console.log(a);
}
expect: {
var a = 0;
var b = function(c) {
for (var i = 2; --i >= 0;) {
(function() {
c = 0;
var f = f && f[void 0];
})();
a += b;
c && b++;
}
}(b = 1);
console.log(a);
}
expect_stdout: "2"
}
3 changes: 2 additions & 1 deletion test/compress/drop-unused.js
Expand Up @@ -2121,7 +2121,8 @@ issue_3515_1: {
expect: {
var c = 0;
(function() {
for (var key20 in !(this[c++] = 0));
this[c++] = 0;
for (var key20 in !0);
})();
console.log(c);
}
Expand Down

0 comments on commit 0f7aa41

Please sign in to comment.