Skip to content

Commit

Permalink
fix corner case in unused (#4913)
Browse files Browse the repository at this point in the history
fixes #4912
  • Loading branch information
alexlamsl committed May 7, 2021
1 parent d464be3 commit 19d232b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
21 changes: 14 additions & 7 deletions lib/compress.js
Expand Up @@ -6030,8 +6030,10 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Definitions) {
node.definitions.forEach(function(defn) {
var side_effects = defn.value
&& (defn.name instanceof AST_Destructured || defn.value.has_side_effects(compressor));
var value = defn.value;
var side_effects = value
&& (defn.name instanceof AST_Destructured || value.has_side_effects(compressor));
var shared = side_effects && value.tail_node().operator == "=";
defn.name.mark_symbol(function(name) {
if (!(name instanceof AST_SymbolDeclaration)) return;
var def = name.definition();
Expand All @@ -6046,13 +6048,17 @@ merge(Compressor.prototype, {
in_use_ids[def.id] = true;
in_use.push(def);
}
if (defn.value) {
if (!side_effects) initializations.add(def.id, defn.value);
if (value) {
if (!side_effects) {
initializations.add(def.id, value);
} else if (shared) {
verify_safe_usage(def, true, value_modified[def.id]);
}
assignments.add(def.id, defn);
}
return true;
}, tw);
if (side_effects) defn.value.walk(tw);
if (side_effects) value.walk(tw);
});
return true;
}
Expand Down Expand Up @@ -6755,14 +6761,15 @@ merge(Compressor.prototype, {
prop.walk(tw);
});
if (node instanceof AST_Assign) {
var right = get_rhs(node);
var right = get_rhs(node), shared = false;
if (init && node.write_only === true && node_def.scope === self && !right.has_side_effects(compressor)) {
initializations.add(node_def.id, right);
} else {
right.walk(tw);
shared = right.tail_node().operator == "=";
}
if (node.left === sym) {
if (!node.write_only) {
if (!node.write_only || shared) {
verify_safe_usage(node_def, true, value_modified[node_def.id]);
}
} else {
Expand Down
84 changes: 83 additions & 1 deletion test/compress/drop-unused.js
Expand Up @@ -2382,7 +2382,7 @@ issue_3664: {
}
expect: {
console.log(function() {
var b = (b && console.log("FAIL"), 0, 0);
var a, b = (a = (a = [ b && console.log("FAIL") ]).p = 0, 0);
return "PASS";
}());
}
Expand Down Expand Up @@ -3388,3 +3388,85 @@ issue_4834: {
}
expect_stdout: "PASS"
}

issue_4912_1: {
options = {
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = A = function() {};
A;
a.prototype = {
f: function() {
console.log("PASS");
},
};
new A().f();
}
expect: {
var a = A = function() {};
A;
a.prototype = {
f: function() {
console.log("PASS");
},
};
new A().f();
}
expect_stdout: "PASS"
}

issue_4912_2: {
options = {
pure_getters: "strict",
unused: true,
}
input: {
console.log(function() {
var g, f = function() {};
f.p = {};
(g = f.p.q = function() {}).r = "PASS";
return f;
}().p.q.r);
}
expect: {
console.log(function() {
var g, f = function() {};
f.p = {};
(g = f.p.q = function() {}).r = "PASS";
return f;
}().p.q.r);
}
expect_stdout: "PASS"
}

issue_4912_3: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
console.log(function(f, g) {
f = function() {};
f.p = {};
g = f.p.q = function() {};
g.r = "PASS";
return f;
}().p.q.r);
}
expect: {
console.log(function(f, g) {
f = function() {};
f.p = {};
g = f.p.q = function() {};
g.r = "PASS";
return f;
}().p.q.r);
}
expect_stdout: "PASS"
}

0 comments on commit 19d232b

Please sign in to comment.