Skip to content

Commit

Permalink
enhance unused (#4922)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 11, 2021
1 parent 7b8570f commit b1cfa71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6057,7 +6057,7 @@ merge(Compressor.prototype, {
if (!side_effects) {
initializations.add(def.id, value);
} else if (shared) {
verify_safe_usage(def, true, value_modified[def.id]);
verify_safe_usage(def, name, value_modified[def.id]);
}
assignments.add(def.id, defn);
}
Expand Down Expand Up @@ -6200,19 +6200,13 @@ merge(Compressor.prototype, {
if (drop_vars) {
var props = [], sym = assign_as_unused(node, props);
if (sym) {
var def = sym.definition();
var in_use = def.id in in_use_ids;
var value;
if (node instanceof AST_Assign) {
if (!in_use || node.left === sym && indexOf_assign(def, node) < 0) {
if (can_drop_lhs(sym, node)) {
if (node instanceof AST_Assign) {
value = get_rhs(node);
if (node.write_only === true) {
value = value.drop_side_effect_free(compressor)
|| make_node(AST_Number, node, { value: 0 });
}
if (node.write_only === true) value = value.drop_side_effect_free(compressor);
}
} else if (!in_use || node.expression === sym && indexOf_assign(def, node) < 0) {
value = make_node(AST_Number, node, { value: 0 });
if (!value) value = make_node(AST_Number, node, { value: 0 });
}
if (value) {
if (props.assign) {
Expand Down Expand Up @@ -6705,14 +6699,22 @@ merge(Compressor.prototype, {
function verify_safe_usage(def, read, modified) {
if (def.id in in_use_ids) return;
if (read && modified) {
in_use_ids[def.id] = true;
in_use_ids[def.id] = read;
in_use.push(def);
} else {
value_read[def.id] = read;
value_modified[def.id] = modified;
}
}

function can_drop_lhs(sym, node) {
var def = sym.definition();
var in_use = in_use_ids[def.id];
if (!in_use) return true;
if (node[node instanceof AST_Assign ? "left" : "expression"] !== sym) return false;
return in_use === sym && def.references.length - def.replaced == 1 || indexOf_assign(def, node) < 0;
}

function get_rhs(assign) {
var rhs = assign.right;
if (!assign.write_only) return rhs;
Expand Down Expand Up @@ -6775,7 +6777,7 @@ merge(Compressor.prototype, {
}
if (node.left === sym) {
if (!node.write_only || shared) {
verify_safe_usage(node_def, true, value_modified[node_def.id]);
verify_safe_usage(node_def, sym, value_modified[node_def.id]);
}
} else {
var fixed = sym.fixed_value();
Expand Down
2 changes: 1 addition & 1 deletion test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,7 @@ issue_4912_2: {
console.log(function() {
var g, f = function() {};
f.p = {};
(g = f.p.q = function() {}).r = "PASS";
(f.p.q = function() {}).r = "PASS";
return f;
}().p.q.r);
}
Expand Down

0 comments on commit b1cfa71

Please sign in to comment.