From 998c9792dab292545162cd33d79e27aafe35f6c2 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 6 Apr 2022 05:23:47 +0100 Subject: [PATCH] fix corner case in `inline` (#5410) fixes #5409 --- lib/compress.js | 36 ++++++++++++++++++++---------------- test/compress/functions.js | 28 ++++++++++++++++++++++++++++ test/compress/keep_fargs.js | 3 +-- test/compress/reduce_vars.js | 3 +-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index cd8dd459..88231a47 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -13160,6 +13160,19 @@ Compressor.prototype.compress = function(node) { return found; } + function insert_assign(def, assign) { + var visited = []; + def.references.forEach(function(ref) { + var fixed = ref.fixed; + if (!fixed || !push_uniq(visited, fixed)) return; + if (fixed.assigns) { + fixed.assigns.unshift(assign); + } else { + fixed.assigns = [ assign ]; + } + }); + } + function init_ref(compressor, name) { var sym = make_node(AST_SymbolRef, name, name); var assign = make_node(AST_Assign, name, { @@ -13173,16 +13186,7 @@ Compressor.prototype.compress = function(node) { return assign.right; }; sym.fixed.assigns = [ assign ]; - var visited = []; - def.references.forEach(function(ref) { - var fixed = ref.fixed; - if (!fixed || !push_uniq(visited, fixed)) return; - if (fixed.assigns) { - fixed.assigns.unshift(assign); - } else { - fixed.assigns = [ assign ]; - } - }); + insert_assign(def, assign); } def.assignments++; def.references.push(sym); @@ -13391,12 +13395,12 @@ Compressor.prototype.compress = function(node) { if (value) body.push(make_node(AST_SimpleStatement, call, { body: value })); return; } - body.push(make_node(AST_Var, call, { - definitions: [ make_node(AST_VarDef, call, { - name: argname.convert_symbol(AST_SymbolVar, process), - value: value || make_node(AST_Undefined, call).transform(compressor), - }) ], - })); + var defn = make_node(AST_VarDef, call, { + name: argname.convert_symbol(AST_SymbolVar, process), + value: value || make_node(AST_Undefined, call).transform(compressor), + }); + if (argname instanceof AST_SymbolFunarg) insert_assign(argname.definition(), defn); + body.push(make_node(AST_Var, call, { definitions: [ defn ] })); }); if (values.length) body.push(make_node(AST_SimpleStatement, call, { body: make_sequence(call, values), diff --git a/test/compress/functions.js b/test/compress/functions.js index 79be3969..0db59da5 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8370,3 +8370,31 @@ issue_5401: { } expect_stdout: "PASS" } + +issue_5409: { + options = { + inline: true, + merge_vars: true, + reduce_vars: true, + unused: true, + } + input: { + (function(a) { + (a = console) || FAIL(a); + (function(b) { + console.log(b && b); + while (!console); + })(); + })(); + } + expect: { + (function(a) { + (a = console) || FAIL(a); + a = void 0; + console.log(a && a); + while (!console); + return; + })(); + } + expect_stdout: "undefined" +} diff --git a/test/compress/keep_fargs.js b/test/compress/keep_fargs.js index 93c431b9..a8e34cca 100644 --- a/test/compress/keep_fargs.js +++ b/test/compress/keep_fargs.js @@ -1258,8 +1258,7 @@ issues_3267_1: { } expect: { !function() { - var i = Object(); - if (i) + if (Object()) return console.log("PASS"); throw "FAIL"; }(); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index c2bd380a..a7360434 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -6689,8 +6689,7 @@ issues_3267_1: { } expect: { !function(x) { - var i = Object(); - if (i) + if (Object()) return console.log("PASS"); throw "FAIL"; }();