Skip to content

Commit

Permalink
fix corner cases with collapse_vars, inline & reduce_vars (#2637)
Browse files Browse the repository at this point in the history
fixes #2630
  • Loading branch information
alexlamsl committed Dec 23, 2017
1 parent c07ea17 commit 202f90e
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/compress.js
Expand Up @@ -1089,6 +1089,7 @@ merge(Compressor.prototype, {
for (var i = stat_index; !abort && i < statements.length; i++) {
statements[i].transform(multi_replacer);
}
value_def.single_use = false;
}
}
if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1);
Expand Down Expand Up @@ -3958,13 +3959,14 @@ merge(Compressor.prototype, {
&& (exp === fn ? !fn.name
: compressor.option("unused")
&& (def = exp.definition()).references.length == 1
&& !recursive_ref(compressor, def))
&& !recursive_ref(compressor, def)
&& fn.is_constant_expression(exp.scope))
&& !self.has_pure_annotation(compressor)
&& !fn.contains_this()
&& (scope = can_flatten_args(fn))
&& (value = flatten_body(stat))) {
var expressions = flatten_args(fn, scope);
expressions.push(value);
expressions.push(value.clone(true));
return make_sequence(self, expressions).optimize(compressor);
}
if (compressor.option("side_effects") && all(fn.body, is_empty)) {
Expand Down
165 changes: 165 additions & 0 deletions test/compress/functions.js
Expand Up @@ -1258,3 +1258,168 @@ issue_2620_4: {
}
expect_stdout: "PASS"
}

issue_2630_1: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
reduce_funcs: true,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
var c = 0;
(function() {
while (f());
function f() {
var a = function() {
var b = c++, d = c = 1 + c;
}();
}
})();
console.log(c);
}
expect: {
var c = 0;
(function() {
while (c++, void (c = 1 + c));
})(),
console.log(c);
}
expect_stdout: "2"
}

issue_2630_2: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
reduce_vars: true,
sequences: true,
unused: true,
}
input: {
var c = 0;
!function() {
while (f()) {}
function f() {
var not_used = function() {
c = 1 + c;
}(c = c + 1);
}
}();
console.log(c);
}
expect: {
var c = 0;
!function() {
while (c += 1, void (c = 1 + c));
}(), console.log(c);
}
expect_stdout: "2"
}

issue_2630_3: {
options = {
inline: true,
reduce_vars: true,
unused: true,
}
input: {
var x = 2, a = 1;
(function() {
function f1(a) {
f2();
--x >= 0 && f1({});
}
f1(a++);
function f2() {
a++;
}
})();
console.log(a);
}
expect: {
var x = 2, a = 1;
(function() {
function f1(a) {
f2();
--x >= 0 && f1({});
}
f1(a++);
function f2() {
a++;
}
})();
console.log(a);
}
expect_stdout: "5"
}

issue_2630_4: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var x = 3, a = 1, b = 2;
(function() {
(function f1() {
while (--x >= 0 && f2());
}());
function f2() {
a++ + (b += a);
}
})();
console.log(a);
}
expect: {
var x = 3, a = 1, b = 2;
(function() {
(function() {
while (--x >= 0 && void (a++, b += a));
})();
})();
console.log(a);
}
expect_stdout: "2"
}

issue_2630_5: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
unused: true,
}
input: {
var c = 1;
!function() {
do {
c *= 10;
} while (f());
function f() {
return function() {
return (c = 2 + c) < 100;
}(c = c + 3);
}
}();
console.log(c);
}
expect: {
var c = 1;
!function() {
do {
c *= 10;
} while (c += 3, (c = 2 + c) < 100);
}();
console.log(c);
}
expect_stdout: "155"
}

0 comments on commit 202f90e

Please sign in to comment.