improve reduce_vars (#2592)

- account for hoisting nature of `var`
This commit is contained in:
Alex Lam S.L
2017-12-14 15:32:13 +08:00
committed by GitHub
parent 738fd52bc4
commit 02a6ce07eb
2 changed files with 61 additions and 4 deletions

View File

@@ -381,7 +381,10 @@ merge(Compressor.prototype, {
&& node.operator == "=" && node.operator == "="
&& node.left instanceof AST_SymbolRef) { && node.left instanceof AST_SymbolRef) {
var d = node.left.definition(); var d = node.left.definition();
if (safe_to_assign(d, node.right)) { if (safe_to_assign(d, node.right)
|| d.fixed === undefined && all(d.orig, function(sym) {
return sym instanceof AST_SymbolVar;
})) {
d.references.push(node.left); d.references.push(node.left);
d.fixed = function() { d.fixed = function() {
return node.right; return node.right;
@@ -561,9 +564,9 @@ merge(Compressor.prototype, {
if (!safe_to_read(def)) return false; if (!safe_to_read(def)) return false;
if (def.fixed === false) return false; if (def.fixed === false) return false;
if (def.fixed != null && (!value || def.references.length > 0)) return false; if (def.fixed != null && (!value || def.references.length > 0)) return false;
return !def.orig.some(function(sym) { return all(def.orig, function(sym) {
return sym instanceof AST_SymbolDefun return !(sym instanceof AST_SymbolDefun
|| sym instanceof AST_SymbolLambda; || sym instanceof AST_SymbolLambda);
}); });
} }

View File

@@ -4784,3 +4784,57 @@ escape_local_throw: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
inverted_var: {
options = {
evaluate: true,
inline: true,
passes: 3,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
console.log(function() {
var a = 1;
return a;
}(), function() {
var b;
b = 2;
return b;
}(), function() {
c = 3;
return c;
var c;
}(), function(c) {
c = 4;
return c;
}(), function (c) {
c = 5;
return c;
var c;
}(), function c() {
c = 6;
return c;
}(), function c() {
c = 7;
return c;
var c;
}(), function() {
c = 8;
return c;
var c = "foo";
}());
}
expect: {
console.log(1, 2, 3, 4, 5, function c() {
c = 6;
return c;
}(), 7, function() {
c = 8;
return c;
var c = "foo";
}());
}
expect_stdout: true
}