Skip to content

Commit

Permalink
remove spurious self-assignments. Closes #1081
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 28, 2022
1 parent 56a05f0 commit adf7e85
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/compress/index.js
Expand Up @@ -3081,6 +3081,17 @@ def_optimize(AST_Assign, function(self, compressor) {
}

var def;
// x = x ---> x
if (
self.operator === "="
&& self.left instanceof AST_SymbolRef
&& self.left.name !== "arguments"
&& !(def = self.left.definition()).undeclared
&& self.right.equivalent_to(self.left)
) {
return self.right;
}

if (compressor.option("dead_code")
&& self.left instanceof AST_SymbolRef
&& (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
Expand All @@ -3103,6 +3114,7 @@ def_optimize(AST_Assign, function(self, compressor) {
|| parent instanceof AST_Sequence && parent.tail_node() === node);
}
self = self.lift_sequences(compressor);

if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
// x = expr1 OP expr2
if (self.right.left instanceof AST_SymbolRef
Expand Down
2 changes: 1 addition & 1 deletion lib/compress/tighten-body.js
Expand Up @@ -203,7 +203,7 @@ export function trim_unreachable_code(compressor, stat, target) {
});
}

// Tighten a bunch of statements together, and perform statement-level optimization.
/** Tighten a bunch of statements together, and perform statement-level optimization. */
export function tighten_body(statements, compressor) {
var in_loop, in_try;
var scope = compressor.find_parent(AST_Scope).get_defun_scope();
Expand Down
15 changes: 15 additions & 0 deletions test/compress/assignment.js
Expand Up @@ -236,3 +236,18 @@ op_equals_right_global_var: {
x = g() & x;
}
}

spurious_assignment: {
options = {}
input: {
var x = "PASS";
x = x;
console.log(x);
}
expect: {
var x = "PASS";
x;
console.log(x);
}
expect_stdout: "PASS"
}
2 changes: 1 addition & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -779,7 +779,7 @@ collapse_vars_assignment: {
function log(x) { return console.log(x), x; }
function f0(c) {
var a = 3 / c;
return a = a;
return a;
}
function f1(c) {
return 1 - 3 / c;
Expand Down

0 comments on commit adf7e85

Please sign in to comment.