From 3693bde2dde9cbe0aaa7d7e914ee506ded977705 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 27 Jan 2022 23:17:17 +0000 Subject: [PATCH] fix corner case in `inline` (#5317) fixes #5316 --- lib/compress.js | 40 +++++++++++++++--------------- test/compress/functions.js | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 016216ba..2a8977a1 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10515,16 +10515,8 @@ Compressor.prototype.compress = function(node) { append_var(decl_var, expr_var, name, value); if (!in_loop) continue; if (arg_used.has(name.name)) continue; - var def = name.definition(); - if (def.orig.length == 1 && fn.functions.has(name.name)) continue; - var sym = make_node(AST_SymbolRef, name, name); - def.assignments++; - def.references.push(sym); - expr_loop.push(make_node(AST_Assign, var_def, { - operator: "=", - left: sym, - right: make_node(AST_Undefined, name), - })); + if (name.definition().orig.length == 1 && fn.functions.has(name.name)) continue; + expr_loop.push(init_ref(compressor, name)); } }); [].push.apply(decls, decl_var); @@ -13001,6 +12993,22 @@ Compressor.prototype.compress = function(node) { return found; } + function init_ref(compressor, name) { + var sym = make_node(AST_SymbolRef, name, name); + var def = name.definition(); + def.assignments++; + def.references.push(sym); + var assign = make_node(AST_Assign, name, { + operator: "=", + left: sym, + right: make_node(AST_Undefined, name).transform(compressor), + }); + sym.fixed = function() { + return assign.right; + }; + return assign; + } + (function(def) { def(AST_Node, noop); def(AST_Assign, noop); @@ -13183,17 +13191,7 @@ Compressor.prototype.compress = function(node) { })) return; var sym = def.orig[0]; if (sym instanceof AST_SymbolCatch) return; - var ref = make_node(AST_SymbolRef, sym, flatten_var(sym)); - def = ref.definition(); - def.assignments++; - def.references.push(ref); - body.push(make_node(AST_SimpleStatement, sym, { - body: make_node(AST_Assign, sym, { - operator: "=", - left: ref, - right: make_node(AST_Undefined, sym).transform(compressor), - }), - })); + body.push(make_node(AST_SimpleStatement, sym, { body: init_ref(compressor, flatten_var(sym)) })); }); if (fn.variables.has("NaN")) scope.transform(new TreeTransformer(function(node) { if (node instanceof AST_NaN) return make_node(AST_Binary, node, { diff --git a/test/compress/functions.js b/test/compress/functions.js index 863f990d..0e55e753 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8084,3 +8084,53 @@ issue_5296: { } expect_stdout: "PASS" } + +issue_5316_1: { + options = { + collapse_vars: true, + evaluate: true, + inline: true, + reduce_vars: true, + toplevel: true, + } + input: { + do { + console.log("PASS"); + } while (function() { + var a, b = 42 && (console[a = b] = a++); + }()); + } + expect: { + do { + console.log("PASS"); + } while (b = a = void 0, b = (42, console[a = a] = a++), void 0); + var a, b; + } + expect_stdout: "PASS" +} + +issue_5316_2: { + options = { + collapse_vars: true, + evaluate: true, + inline: true, + reduce_vars: true, + toplevel: true, + } + input: { + do { + (function() { + var a, b = 42 && (console[a = b] = a++); + while (console.log("PASS")); + })(); + } while (!console); + } + expect: { + do { + a = void 0; + var a, b = (42, console[a = b = void 0] = a++); + while (console.log("PASS")); + } while (!console); + } + expect_stdout: "PASS" +}