diff --git a/lib/compress/index.js b/lib/compress/index.js index 10c8c7407..ab8d7cf83 100644 --- a/lib/compress/index.js +++ b/lib/compress/index.js @@ -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)) { @@ -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 diff --git a/lib/compress/tighten-body.js b/lib/compress/tighten-body.js index 151383ea3..18a3b734f 100644 --- a/lib/compress/tighten-body.js +++ b/lib/compress/tighten-body.js @@ -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(); diff --git a/test/compress/assignment.js b/test/compress/assignment.js index 903380a97..bb9d08573 100644 --- a/test/compress/assignment.js +++ b/test/compress/assignment.js @@ -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" +} diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 60a6e2328..b562274fa 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -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;