fix corner cases in inline & unused (#4509)

fixes #4508
This commit is contained in:
Alex Lam S.L
2021-01-05 16:15:12 +00:00
committed by GitHub
parent 6eceac0966
commit 7ebfb22d16
3 changed files with 46 additions and 20 deletions

View File

@@ -5674,12 +5674,11 @@ merge(Compressor.prototype, {
var write_only = def.value.write_only;
var value = def.value.drop_side_effect_free(compressor);
if (def.value !== value) {
def.value = value && make_sequence(def.value, [
value,
make_node(AST_Number, def.value, {
value: 0
}),
]);
def.value = null;
if (value) {
AST_Node.warn("Side effects in last use of variable {name} [{file}:{line},{col}]", template(def.name));
side_effects.push(value);
}
} else if (def.value.write_only !== write_only) {
def.value.write_only = write_only;
}
@@ -8139,7 +8138,7 @@ merge(Compressor.prototype, {
return node;
}
}
var insert, in_loop, scope;
var arg_used, insert, in_loop, scope;
if (replacing && can_inject_symbols()) {
fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope;
@@ -8453,8 +8452,9 @@ merge(Compressor.prototype, {
}
}
var inline = compressor.option("inline");
var used = Object.create(defined);
if (!can_inject_args(defined, used, inline >= 2 && safe_to_inject)) return false;
arg_used = Object.create(defined);
if (!can_inject_args(defined, arg_used, inline >= 2 && safe_to_inject)) return false;
var used = Object.create(arg_used);
if (!can_inject_vars(defined, used, inline >= 3 && safe_to_inject)) return false;
return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop);
}
@@ -8558,10 +8558,7 @@ merge(Compressor.prototype, {
name.thedef = redef;
}
append_var(decls, expressions, name, var_def.value);
if (in_loop && all(fn.argnames, function(argname) {
if (argname instanceof AST_DefaultValue) argname = argname.name;
return argname.name != name.name;
})) {
if (in_loop && !HOP(arg_used, name.name)) {
var def = fn.variables.get(name.name);
var sym = make_node(AST_SymbolRef, name, name);
def.references.push(sym);

View File

@@ -2356,3 +2356,31 @@ issue_4504: {
expect_stdout: "PASS"
node_version: ">=6"
}
issue_4508: {
options = {
inline: true,
toplevel: true,
unused: true,
}
input: {
for (var i = 0; i < 2; i++)
(function f([ a ]) {
var a = console.log(a) && b, b = null;
})([ "PASS" ]);
}
expect: {
for (var i = 0; i < 2; i++)
[ [ a ] ] = [ [ "PASS" ] ],
b = void 0,
a = console.log(a) && b,
b = null,
void 0;
var a, b;
}
expect_stdout: [
"PASS",
"PASS",
]
node_version: ">=6"
}

View File

@@ -2669,8 +2669,7 @@ issue_3956: {
})();
}
expect: {
var c, d;
c += 0,
var d;
console.log(NaN),
d = 1 ^ console.log(1),
console.log(d);
@@ -2703,13 +2702,13 @@ issue_3962_1: {
}
expect: {
var a = 0;
a = (function(c) {
(function(c) {
do {
console;
0..toString();
} while (0);
if (c) console.log("PASS");
}(1), 0);
})(1);
void 0;
}
expect_stdout: "PASS"
@@ -2736,13 +2735,13 @@ issue_3962_2: {
}
expect: {
var a = 0;
a = (function(c) {
(function(c) {
do {
console;
0..toString();
} while (0);
if (c) console.log("PASS");
}(1), 0);
})(1);
}
expect_stdout: "PASS"
}
@@ -2799,7 +2798,9 @@ issue_4017: {
var a = 0;
console.log(function() {
c &= 0;
var c = (a++, A = a, 0);
var c;
a++,
A = a;
}());
}
expect_stdout: "undefined"