fix corner case in inline (#3853)

fixes #3852
This commit is contained in:
Alex Lam S.L
2020-05-07 13:53:05 +01:00
committed by GitHub
parent 34ead0430b
commit 88985a46ed
4 changed files with 35 additions and 9 deletions

View File

@@ -4179,7 +4179,7 @@ merge(Compressor.prototype, {
}); });
} }
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) { var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
return !compressor.exposed(def) && !def.references.length; return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) { } : function(def) {
// any declarations with same name will overshadow // any declarations with same name will overshadow
// name of this anonymous function and can therefore // name of this anonymous function and can therefore
@@ -4733,7 +4733,7 @@ merge(Compressor.prototype, {
} }
function all_bool(def, bool_returns, compressor) { function all_bool(def, bool_returns, compressor) {
return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length - def.replaced
&& !compressor.exposed(def); && !compressor.exposed(def);
} }
@@ -4919,7 +4919,7 @@ merge(Compressor.prototype, {
if (def.assignments != count) return; if (def.assignments != count) return;
if (def.direct_access) return; if (def.direct_access) return;
if (def.escaped.depth == 1) return; if (def.escaped.depth == 1) return;
if (def.references.length == count) return; if (def.references.length - def.replaced == count) return;
if (def.single_use) return; if (def.single_use) return;
if (top_retain(def)) return; if (top_retain(def)) return;
if (sym.fixed_value() !== right) return; if (sym.fixed_value() !== right) return;
@@ -5009,7 +5009,11 @@ merge(Compressor.prototype, {
exprs = trim(exprs, compressor, first_in_statement); exprs = trim(exprs, compressor, first_in_statement);
return exprs && make_sequence(this, exprs); return exprs && make_sequence(this, exprs);
} }
if (exp instanceof AST_Function && (!exp.name || !exp.name.definition().references.length)) { if (exp instanceof AST_Function) {
if (exp.name) {
var def = exp.name.definition();
if (def.references.length > def.replaced) return this;
}
exp.process_expression(false, function(node) { exp.process_expression(false, function(node) {
var value = node.value && node.value.drop_side_effect_free(compressor, true); var value = node.value && node.value.drop_side_effect_free(compressor, true);
return value ? make_node(AST_SimpleStatement, node, { return value ? make_node(AST_SimpleStatement, node, {
@@ -6177,7 +6181,7 @@ merge(Compressor.prototype, {
if (best_of(compressor, self, node) === node) return node; if (best_of(compressor, self, node) === node) return node;
} }
var scope, in_loop, level = -1; var scope, in_loop, level = -1;
if ((exp === fn || compressor.option("unused") && exp.definition().references.length == 1) if ((exp === fn || compressor.option("unused") && def.references.length - def.replaced == 1)
&& can_inject_symbols()) { && can_inject_symbols()) {
fn._squeezed = true; fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope; if (exp !== fn) fn.parent_scope = exp.scope;
@@ -6261,7 +6265,7 @@ merge(Compressor.prototype, {
if (var_assigned) return; if (var_assigned) return;
if (compressor.option("inline") < 2 && fn.argnames.length) return; if (compressor.option("inline") < 2 && fn.argnames.length) return;
if (!fn.variables.all(function(def) { if (!fn.variables.all(function(def) {
return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg; return def.references.length - def.replaced < 2 && def.orig[0] instanceof AST_SymbolFunarg;
})) return; })) return;
var abort = false; var abort = false;
var begin; var begin;
@@ -7515,7 +7519,8 @@ merge(Compressor.prototype, {
} }
var name_length = def.name.length; var name_length = def.name.length;
if (compressor.option("unused") && !compressor.exposed(def)) { if (compressor.option("unused") && !compressor.exposed(def)) {
name_length += (name_length + 2 + value_length) / (def.references.length - def.assignments); var referenced = def.references.length - def.replaced;
name_length += (name_length + 2 + value_length) / (referenced - def.assignments);
} }
var delta = value_length - Math.floor(name_length); var delta = value_length - Math.floor(name_length);
def.should_replace = delta < compressor.eval_threshold ? fn : false; def.should_replace = delta < compressor.eval_threshold ? fn : false;

View File

@@ -2366,7 +2366,7 @@ function_parameter_ie8: {
} }
expect: { expect: {
(function() { (function() {
(function f() { (function() {
console.log("PASS"); console.log("PASS");
})(); })();
})(); })();

View File

@@ -4660,3 +4660,24 @@ issue_3836: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_3852: {
options = {
collapse_vars: true,
inline: true,
unused: true,
}
input: {
console.log(function(a) {
return function(b) {
return b && (b[0] = 0), "PASS";
}(a);
}(42));
}
expect: {
console.log(function(a) {
return a && (a[0] = 0), "PASS";
}(42));
}
expect_stdout: "PASS"
}

View File

@@ -2389,7 +2389,7 @@ issue_3703: {
var a = "PASS"; var a = "PASS";
(function() { (function() {
var b; var b;
var c = function g() { var c = function() {
a = "FAIL"; a = "FAIL";
}; };
a ? b |= c : b.p; a ? b |= c : b.p;