fix pre-existing issues

- reference counting on assignment
- walking of anonymous functions
This commit is contained in:
alexlamsl 2017-04-18 04:02:44 +08:00
parent 9dc5247bc7
commit 79d533a560
3 changed files with 63 additions and 7 deletions

View File

@ -298,6 +298,7 @@ merge(Compressor.prototype, {
&& node.left instanceof AST_SymbolRef) { && node.left instanceof AST_SymbolRef) {
var d = node.left.definition(); var d = node.left.definition();
if (HOP(safe_ids, d.id) && safe_to_assign(d, node.right)) { if (HOP(safe_ids, d.id) && safe_to_assign(d, node.right)) {
d.references.push(node.left);
d.fixed = function() { d.fixed = function() {
return node.right; return node.right;
}; };
@ -321,13 +322,11 @@ merge(Compressor.prototype, {
safe_ids = save_ids; safe_ids = save_ids;
return true; return true;
} }
var iife; if (node instanceof AST_Function) {
if (node instanceof AST_Function var iife;
&& (iife = tw.parent()) instanceof AST_Call if (!node.name
&& iife.expression === node) { && (iife = tw.parent()) instanceof AST_Call
if (node.name) { && iife.expression === node) {
node.name.definition().fixed = node;
} else {
// Virtually turn IIFE parameters into variable definitions: // Virtually turn IIFE parameters into variable definitions:
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})() // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
// So existing transformation rules can work on them. // So existing transformation rules can work on them.
@ -339,6 +338,10 @@ merge(Compressor.prototype, {
mark(d, true); mark(d, true);
}); });
} }
push();
descend();
pop();
return true;
} }
if (node instanceof AST_Binary if (node instanceof AST_Binary
&& (node.operator == "&&" || node.operator == "||")) { && (node.operator == "&&" || node.operator == "||")) {

View File

@ -1592,3 +1592,25 @@ var_side_effects_3: {
} }
expect_stdout: true expect_stdout: true
} }
reduce_vars_assign: {
options = {
collapse_vars: true,
reduce_vars: true,
}
input: {
!function() {
var a = 1;
a = [].length,
console.log(a);
}();
}
expect: {
!function() {
var a = 1;
a = [].length,
console.log(a);
}();
}
expect_stdout: "0"
}

View File

@ -2258,3 +2258,34 @@ cond_assign: {
} }
expect_stdout: "undefined" expect_stdout: "undefined"
} }
iife_assign: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
!function() {
var a = 1, b = 0;
!function() {
b++;
return;
a = 2;
}();
console.log(a);
}();
}
expect: {
!function() {
var a = 1, b = 0;
!function() {
b++;
return;
a = 2;
}();
console.log(a);
}();
}
expect_stdout: "1"
}