fix corner cases in inline & unused (#5058)

fixes #5057
This commit is contained in:
Alex Lam S.L
2021-07-07 03:29:16 +01:00
committed by GitHub
parent 1fefe3f1d1
commit 0c48b84540
2 changed files with 83 additions and 6 deletions

View File

@@ -3643,7 +3643,7 @@ merge(Compressor.prototype, {
stat.init = stat.init.definitions[0].name.convert_symbol(AST_SymbolRef, function(ref, name) { stat.init = stat.init.definitions[0].name.convert_symbol(AST_SymbolRef, function(ref, name) {
defns.push(make_node(AST_VarDef, name, { defns.push(make_node(AST_VarDef, name, {
name: name, name: name,
value: null value: null,
})); }));
name.definition().references.push(ref); name.definition().references.push(ref);
}); });
@@ -7007,6 +7007,7 @@ merge(Compressor.prototype, {
var value = node.value.drop_side_effect_free(compressor); var value = node.value.drop_side_effect_free(compressor);
if (!value) return null; if (!value) return null;
log(node.name, "Side effects in default value of unused variable {name}"); log(node.name, "Side effects in default value of unused variable {name}");
node.name.__unused = null;
node.value = value; node.value = value;
} }
return node; return node;
@@ -9646,7 +9647,6 @@ merge(Compressor.prototype, {
fn.each_argname(function(arg) { fn.each_argname(function(arg) {
if (abort) return; if (abort) return;
if (arg.__unused) return; if (arg.__unused) return;
if (arg instanceof AST_DefaultValue) arg = arg.name;
if (!safe_to_inject || var_exists(defined, arg.name)) return abort = true; if (!safe_to_inject || var_exists(defined, arg.name)) return abort = true;
used[arg.name] = true; used[arg.name] = true;
if (in_loop) in_loop.push(arg.definition()); if (in_loop) in_loop.push(arg.definition());
@@ -9720,22 +9720,22 @@ merge(Compressor.prototype, {
function append_var(decls, expressions, name, value) { function append_var(decls, expressions, name, value) {
var def = name.definition(); var def = name.definition();
scope.variables.set(name.name, def);
scope.enclosed.push(def);
if (!scope.var_names()[name.name]) { if (!scope.var_names()[name.name]) {
scope.var_names()[name.name] = true; scope.var_names()[name.name] = true;
decls.push(make_node(AST_VarDef, name, { decls.push(make_node(AST_VarDef, name, {
name: name, name: name,
value: null value: null,
})); }));
} }
scope.variables.set(name.name, def);
scope.enclosed.push(def);
if (!value) return; if (!value) return;
var sym = make_node(AST_SymbolRef, name, name); var sym = make_node(AST_SymbolRef, name, name);
def.references.push(sym); def.references.push(sym);
expressions.push(make_node(AST_Assign, self, { expressions.push(make_node(AST_Assign, self, {
operator: "=", operator: "=",
left: sym, left: sym,
right: value right: value,
})); }));
} }

View File

@@ -1416,6 +1416,7 @@ issue_4502_1: {
expect: { expect: {
(function() { (function() {
var a = "PASS"; var a = "PASS";
void 0,
console.log(a), console.log(a),
a++, a++,
void 0; void 0;
@@ -1439,6 +1440,7 @@ issue_4502_2: {
expect: { expect: {
(function() { (function() {
var a = "PASS"; var a = "PASS";
void 0,
console.log(a), console.log(a),
a++, a++,
void 0; void 0;
@@ -1754,3 +1756,78 @@ issue_4994: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=6" node_version: ">=6"
} }
issue_5057_1: {
options = {
collapse_vars: true,
inline: true,
sequences: true,
unused: true,
}
input: {
var a = 42;
(function() {
var b = function(c = (console.log("foo"), b = a)) {
a && console.log("bar");
}();
})();
}
expect: {
var a = 42;
console.log("foo"),
void (a && console.log("bar"));
}
expect_stdout: [
"foo",
"bar",
]
node_version: ">=6"
}
issue_5057_2: {
options = {
inline: true,
unused: true,
}
input: {
(function f(a) {
(function(b = console.log("FAIL")) {})(a);
})(42);
console.log(typeof b);
}
expect: {
(function(a) {
[ b = console.log("FAIL") ] = [ a ],
void 0;
var b;
})(42);
console.log(typeof b);
}
expect_stdout: "undefined"
node_version: ">=6"
}
issue_5057_3: {
options = {
inline: true,
unused: true,
}
input: {
(function(a) {
(function f(b) {
(function(a = console.log("FAIL 1")) {})(b);
console.log(a);
})("FAIL 2");
})("PASS");
}
expect: {
(function(a) {
(function(b) {
(function(a = console.log("FAIL 1")) {})(b);
console.log(a);
})("FAIL 2");
})("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}