@@ -808,6 +808,7 @@ merge(Compressor.prototype, {
|
|||||||
function collapse(statements, compressor) {
|
function collapse(statements, compressor) {
|
||||||
var scope = compressor.find_parent(AST_Scope);
|
var scope = compressor.find_parent(AST_Scope);
|
||||||
if (scope.uses_eval || scope.uses_with) return statements;
|
if (scope.uses_eval || scope.uses_with) return statements;
|
||||||
|
var args;
|
||||||
var candidates = [];
|
var candidates = [];
|
||||||
var stat_index = statements.length;
|
var stat_index = statements.length;
|
||||||
while (--stat_index >= 0) {
|
while (--stat_index >= 0) {
|
||||||
@@ -828,7 +829,7 @@ merge(Compressor.prototype, {
|
|||||||
var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
|
var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
|
||||||
var side_effects = value_has_side_effects(candidate);
|
var side_effects = value_has_side_effects(candidate);
|
||||||
var hit = candidate.name instanceof AST_SymbolFunarg;
|
var hit = candidate.name instanceof AST_SymbolFunarg;
|
||||||
var abort = false, replaced = false;
|
var abort = false, replaced = false, can_replace = !args || !hit;
|
||||||
var tt = new TreeTransformer(function(node, descend) {
|
var tt = new TreeTransformer(function(node, descend) {
|
||||||
if (abort) return node;
|
if (abort) return node;
|
||||||
// Skip nodes before `candidate` as quickly as possible
|
// Skip nodes before `candidate` as quickly as possible
|
||||||
@@ -853,7 +854,8 @@ merge(Compressor.prototype, {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
// Replace variable with assignment when found
|
// Replace variable with assignment when found
|
||||||
if (!(node instanceof AST_SymbolDeclaration)
|
if (can_replace
|
||||||
|
&& !(node instanceof AST_SymbolDeclaration)
|
||||||
&& !is_lhs(node, parent)
|
&& !is_lhs(node, parent)
|
||||||
&& lhs.equivalent_to(node)) {
|
&& lhs.equivalent_to(node)) {
|
||||||
CHANGED = replaced = abort = true;
|
CHANGED = replaced = abort = true;
|
||||||
@@ -904,6 +906,12 @@ merge(Compressor.prototype, {
|
|||||||
// Skip (non-executed) functions and (leading) default case in switch statements
|
// Skip (non-executed) functions and (leading) default case in switch statements
|
||||||
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
||||||
});
|
});
|
||||||
|
if (!can_replace) {
|
||||||
|
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; j < args.length; j++) {
|
||||||
|
args[j].transform(tt);
|
||||||
|
}
|
||||||
|
can_replace = true;
|
||||||
|
}
|
||||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||||
statements[i].transform(tt);
|
statements[i].transform(tt);
|
||||||
}
|
}
|
||||||
@@ -921,12 +929,18 @@ merge(Compressor.prototype, {
|
|||||||
&& iife.expression === fn) {
|
&& iife.expression === fn) {
|
||||||
var fn_strict = compressor.has_directive("use strict");
|
var fn_strict = compressor.has_directive("use strict");
|
||||||
if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
|
if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
|
||||||
|
var len = fn.argnames.length;
|
||||||
|
args = iife.args.slice(len);
|
||||||
var names = Object.create(null);
|
var names = Object.create(null);
|
||||||
for (var i = fn.argnames.length; --i >= 0;) {
|
for (var i = len; --i >= 0;) {
|
||||||
var sym = fn.argnames[i];
|
var sym = fn.argnames[i];
|
||||||
|
var arg = iife.args[i];
|
||||||
|
args.unshift(make_node(AST_VarDef, sym, {
|
||||||
|
name: sym,
|
||||||
|
value: arg
|
||||||
|
}));
|
||||||
if (sym.name in names) continue;
|
if (sym.name in names) continue;
|
||||||
names[sym.name] = true;
|
names[sym.name] = true;
|
||||||
var arg = iife.args[i];
|
|
||||||
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
|
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
|
||||||
else {
|
else {
|
||||||
var tw = new TreeWalker(function(node) {
|
var tw = new TreeWalker(function(node) {
|
||||||
|
|||||||
@@ -2949,3 +2949,69 @@ conditional_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "5 5"
|
expect_stdout: "5 5"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_2425_1: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b) {
|
||||||
|
b.toString();
|
||||||
|
})(--a, a |= 10);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b) {
|
||||||
|
b.toString();
|
||||||
|
})(--a, a |= 10);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: "15"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_2425_2: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b, c) {
|
||||||
|
b.toString();
|
||||||
|
})(--a, a |= 10);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b, c) {
|
||||||
|
b.toString();
|
||||||
|
})(--a, a |= 10);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: "15"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_2425_3: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b, b) {
|
||||||
|
b.toString();
|
||||||
|
})(--a, a |= 10);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 8;
|
||||||
|
(function(b, b) {
|
||||||
|
(a |= 10).toString();
|
||||||
|
})(--a);
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: "15"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user