account for eval & with in reduce_vars (#2441)

fixes #2440
This commit is contained in:
Alex Lam S.L
2017-11-06 16:10:57 +08:00
committed by GitHub
parent 6c45101870
commit 2cfb5aa7da
2 changed files with 120 additions and 7 deletions

View File

@@ -317,7 +317,7 @@ merge(Compressor.prototype, {
d.fixed = false; d.fixed = false;
} else if (d.fixed) { } else if (d.fixed) {
var value = node.fixed_value(); var value = node.fixed_value();
if (unused && value && d.references.length == 1) { if (value && ref_once(d)) {
if (value instanceof AST_Lambda) { if (value instanceof AST_Lambda) {
d.single_use = d.scope === node.scope d.single_use = d.scope === node.scope
&& !(d.orig[0] instanceof AST_SymbolFunarg) && !(d.orig[0] instanceof AST_SymbolFunarg)
@@ -385,7 +385,7 @@ merge(Compressor.prototype, {
} else { } else {
d.fixed = node; d.fixed = node;
mark(d, true); mark(d, true);
if (unused && d.references.length == 1) { if (ref_once(d)) {
d.single_use = d.scope === d.references[0].scope d.single_use = d.scope === d.references[0].scope
|| node.is_constant_expression(d.references[0].scope); || node.is_constant_expression(d.references[0].scope);
} }
@@ -564,7 +564,7 @@ merge(Compressor.prototype, {
function reset_def(def) { function reset_def(def) {
def.direct_access = false; def.direct_access = false;
def.escaped = false; def.escaped = false;
if (def.scope.uses_eval) { if (def.scope.uses_eval || def.scope.uses_with) {
def.fixed = false; def.fixed = false;
} else if (!compressor.exposed(def)) { } else if (!compressor.exposed(def)) {
def.fixed = undefined; def.fixed = undefined;
@@ -576,6 +576,10 @@ merge(Compressor.prototype, {
def.single_use = undefined; def.single_use = undefined;
} }
function ref_once(def) {
return unused && !def.scope.uses_eval && !def.scope.uses_with && def.references.length == 1;
}
function is_immutable(value) { function is_immutable(value) {
if (!value) return false; if (!value) return false;
return value.is_constant() return value.is_constant()
@@ -4237,10 +4241,7 @@ merge(Compressor.prototype, {
if (fixed instanceof AST_Defun) { if (fixed instanceof AST_Defun) {
d.fixed = fixed = make_node(AST_Function, fixed, fixed); d.fixed = fixed = make_node(AST_Function, fixed, fixed);
} }
if (compressor.option("unused") if (fixed && d.single_use) {
&& fixed
&& d.references.length == 1
&& d.single_use) {
var value = fixed.optimize(compressor); var value = fixed.optimize(compressor);
return value === fixed ? fixed.clone(true) : value; return value === fixed ? fixed.clone(true) : value;
} }

View File

@@ -3542,3 +3542,115 @@ issue_2423_6: {
"2", "2",
] ]
} }
issue_2440_eval_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function foo() {
return bar();
}
baz = {
quux: foo
};
exec = function() {
return eval("foo()");
};
}
expect: {
function foo() {
return bar();
}
baz = {
quux: foo
};
exec = function() {
return eval("foo()");
};
}
}
issue_2440_eval_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
baz = {
quux: foo
};
exec = function() {
return eval("foo()");
};
function foo() {
return bar();
}
}
expect: {
baz = {
quux: foo
};
exec = function() {
return eval("foo()");
};
function foo() {
return bar();
}
}
}
issue_2440_with_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function foo() {
return bar();
}
baz = {
quux: foo
};
with (o) whatever();
}
expect: {
function foo() {
return bar();
}
baz = {
quux: foo
};
with (o) whatever();
}
}
issue_2440_with_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
baz = {
quux: foo
};
with (o) whatever();
function foo() {
return bar();
}
}
expect: {
baz = {
quux: foo
};
with (o) whatever();
function foo() {
return bar();
}
}
}