From 1d835ac17de613093a538a4ab72160508197e08c Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 29 Sep 2020 00:01:38 +0100 Subject: [PATCH] fix corner case in `inline` (#4160) fixes #4159 --- lib/compress.js | 18 ++++++++++++------ test/compress/functions.js | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 360bbe7a..69394a3d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4793,15 +4793,15 @@ merge(Compressor.prototype, { for (var a = node.argnames, i = a.length; --i >= 0;) { var sym = a[i]; var def = sym.definition(); - if (!(def.id in in_use_ids)) { + if (def.id in in_use_ids) { + trim = false; + if (indexOf_assign(def, sym) < 0) sym.__unused = null; + } else { sym.__unused = true; if (trim) { log(sym, "Dropping unused function argument {name}"); a.pop(); } - } else { - trim = false; - if (indexOf_assign(def, sym) < 0) sym.__unused = null; } } fns_with_marked_args.push(node); @@ -7047,9 +7047,10 @@ merge(Compressor.prototype, { value: null })); } + if (!value) return; var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); - if (value) expressions.push(make_node(AST_Assign, self, { + expressions.push(make_node(AST_Assign, self, { operator: "=", left: sym, right: value @@ -7070,7 +7071,12 @@ merge(Compressor.prototype, { var symbol = make_node(AST_SymbolVar, name, name); name.definition().orig.push(symbol); if (!value && in_loop) value = make_node(AST_Undefined, self); - append_var(decls, expressions, symbol, value); + if ("__unused" in name) { + append_var(decls, expressions, symbol); + if (value) expressions.push(value); + } else { + append_var(decls, expressions, symbol, value); + } } } decls.reverse(); diff --git a/test/compress/functions.js b/test/compress/functions.js index 1be01167..4d753469 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -4808,3 +4808,24 @@ issue_4155: { "function", ] } + +issue_4159: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = 42, c = function(b) { + (b = a) && console.log(a++, b); + }(c = a); + } + expect: { + var a = 42; + (b = a) && console.log(a++, b); + var b; + } + expect_stdout: "42 42" +} -- 2.34.1