From: Alex Lam S.L Date: Sun, 22 Oct 2017 17:00:50 +0000 (+0800) Subject: deduplicate declarations regardless of `toplevel` (#2393) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=8a713e449f551d89745d9a8105b7cb33e9893214;p=UglifyJS.git deduplicate declarations regardless of `toplevel` (#2393) --- diff --git a/lib/compress.js b/lib/compress.js index eb0e2016..7085fcb3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2240,7 +2240,6 @@ merge(Compressor.prototype, { if (self.uses_eval || self.uses_with) return; var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs; var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars; - if (!drop_funcs && !drop_vars) return; var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node) { if (node instanceof AST_Assign && (node.write_only || node.operator == "=")) { return node.left; @@ -2375,7 +2374,8 @@ merge(Compressor.prototype, { } return node; } - if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn && tt.parent().init === node)) { + var parent = tt.parent(); + if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) { // place uninitialized names at the start var body = [], head = [], tail = []; // for unused names whose initialization has @@ -2385,7 +2385,7 @@ merge(Compressor.prototype, { node.definitions.forEach(function(def) { if (def.value) def.value = def.value.transform(tt); var sym = def.name.definition(); - if (sym.id in in_use_ids) { + if (!drop_vars || sym.id in in_use_ids) { if (def.name instanceof AST_SymbolVar) { var var_defs = var_defs_by_id.get(sym.id); if (var_defs.length > 1 && !def.value) { @@ -2467,7 +2467,7 @@ merge(Compressor.prototype, { && !((def = def.definition()).id in in_use_ids) && self.variables.get(def.name) === def) { if (node instanceof AST_Assign) { - return maintain_this_binding(tt.parent(), node, node.right.transform(tt)); + return maintain_this_binding(parent, node, node.right.transform(tt)); } return make_node(AST_Number, node, { value: 0 diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 1f702ad7..b5b97d24 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2051,7 +2051,7 @@ inner_lvalues: { console.log(null, a, b); } expect: { - var a, b = 10; + var b = 10; var a = (--b || a || 3).toString(), c = --b + -a; console.log(null, a, b); }