Skip to content

Commit

Permalink
enhance assignments & unused
Browse files Browse the repository at this point in the history
closes #3427
  • Loading branch information
alexlamsl committed May 28, 2019
1 parent e4f5ba1 commit 4d42c5e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
47 changes: 33 additions & 14 deletions lib/compress.js
Expand Up @@ -3618,7 +3618,7 @@ merge(Compressor.prototype, {
var value = null;
if (node instanceof AST_Assign) {
if (!in_use || node.left === sym && def.id in fixed_ids && fixed_ids[def.id] !== node) {
value = node.right;
value = get_rhs(node);
}
} else if (!in_use) {
value = make_node(AST_Number, node, {
Expand Down Expand Up @@ -3843,16 +3843,26 @@ merge(Compressor.prototype, {
}
}

function get_rhs(assign) {
var rhs = assign.right;
if (!assign.write_only) return rhs;
if (!(rhs instanceof AST_Binary && lazy_op[rhs.operator])) return rhs;
var sym = assign.left;
if (!(sym instanceof AST_SymbolRef) || sym.name != rhs.left.name) return rhs;
return rhs.right;
}

function scan_ref_scoped(node, descend) {
var node_def, props = [], sym = assign_as_unused(node, props);
if (sym && self.variables.get(sym.name) === (node_def = sym.definition())) {
props.forEach(function(prop) {
prop.walk(tw);
});
if (node instanceof AST_Assign) {
node.right.walk(tw);
var right = get_rhs(node);
right.walk(tw);
if (node.left === sym) {
if (!node_def.chained && sym.fixed_value(true) === node.right) {
if (!node_def.chained && sym.fixed_value(true) === right) {
fixed_ids[node_def.id] = node;
}
if (!node.write_only) {
Expand Down Expand Up @@ -5556,20 +5566,29 @@ merge(Compressor.prototype, {
self.right = tmp;
}
}
if (commutativeOperators[self.operator]) {
if (self.right.is_constant()
&& !self.left.is_constant()) {
// if right is a constant, whatever side effects the
// left side might have could not influence the
// result. hence, force switch.

if (!(self.left instanceof AST_Binary
&& PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
reverse();
}
if (commutativeOperators[self.operator] && self.right.is_constant() && !self.left.is_constant()) {
// if right is a constant, whatever side effects the
// left side might have could not influence the
// result. hence, force switch.
if (!(self.left instanceof AST_Binary
&& PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
reverse();
}
}
self = self.lift_sequences(compressor);
if (compressor.option("assignments") && lazy_op[self.operator]) {
var assign = self.right;
// a || (a = x) => a = a || x
// a && (a = x) => a = a && x
if (self.left instanceof AST_SymbolRef
&& assign instanceof AST_Assign
&& assign.operator == "="
&& self.left.equivalent_to(assign.left)) {
self.right = assign.right;
assign.right = self;
return assign;
}
}
if (compressor.option("comparisons")) switch (self.operator) {
case "===":
case "!==":
Expand Down
16 changes: 16 additions & 0 deletions test/compress/assignment.js
Expand Up @@ -311,3 +311,19 @@ issue_3375: {
}
expect_stdout: "string"
}

issue_3427: {
options = {
assignments: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
(function() {
var a;
a || (a = {});
})();
}
expect: {}
}
34 changes: 34 additions & 0 deletions test/compress/drop-unused.js
Expand Up @@ -2028,3 +2028,37 @@ issue_3375: {
}
expect_stdout: "0 0"
}

issue_3427_1: {
options = {
sequences: true,
side_effects: true,
unused: true,
}
input: {
(function() {
var a;
a = a || {};
})();
}
expect: {}
}

issue_3427_2: {
options = {
unused: true,
}
input: {
(function() {
var s = "PASS";
console.log(s = s || "FAIL");
})();
}
expect: {
(function() {
var s = "PASS";
console.log(s = s || "FAIL");
})();
}
expect_stdout: "PASS"
}
23 changes: 23 additions & 0 deletions test/compress/pure_getters.js
Expand Up @@ -1187,3 +1187,26 @@ drop_arguments: {
}
expect_stdout: "PASS"
}

issue_3427: {
options = {
assignments: true,
collapse_vars: true,
inline: true,
pure_getters: "strict",
sequences: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
var a;
(function(b) {
b.p = 42;
})(a || (a = {}));
}
expect: {
var a;
(a = a || {}).p = 42;
}
}

0 comments on commit 4d42c5e

Please sign in to comment.