From 9a137e8613e49066a54f2a1b734a559c05338f11 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 14 Dec 2017 02:59:59 +0800 Subject: drop local assign-only variable in `return` (#2587) --- lib/compress.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index 6e984ffa..559fb51a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4666,6 +4666,24 @@ merge(Compressor.prototype, { var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ]; var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ]; OPT(AST_Assign, function(self, compressor){ + if (compressor.option("dead_code") + && self.left instanceof AST_SymbolRef + && self.left.definition().scope === compressor.find_parent(AST_Lambda)) { + var level = 0, node, parent = self; + do { + node = parent; + parent = compressor.parent(level++); + if (parent instanceof AST_Exit) { + if (self.operator == "=") return self.right; + return make_node(AST_Binary, self, { + operator: self.operator.slice(0, -1), + left: self.left, + right: self.right + }).optimize(compressor); + } + } while (parent instanceof AST_Binary && parent.right === node + || 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 -- cgit v1.2.3