From: Alex Lam S.L Date: Tue, 12 Jan 2021 17:08:16 +0000 (+0000) Subject: fix corner case in `side_effects` (#4545) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=fc816628c12f832ec5d6c3158f1c28938077b098;p=UglifyJS.git fix corner case in `side_effects` (#4545) fixes #4544 --- diff --git a/lib/compress.js b/lib/compress.js index c9bd182e..dde50e04 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -8243,7 +8243,7 @@ merge(Compressor.prototype, { if (compressor.option("side_effects") && can_drop && all(fn.body, is_empty) - && (fn === exp ? fn_name_unused(fn, compressor) : !has_default && !has_destructured) + && (fn === exp ? fn_name_unused(fn, compressor) : !fn.rest && !has_default && !has_destructured) && !(is_arrow(fn) && fn.value)) { return make_sequence(self, convert_args()).optimize(compressor); } @@ -8286,7 +8286,7 @@ merge(Compressor.prototype, { function convert_args(value) { var args = self.args.slice(); - var destructured = has_default > 1 || has_destructured; + var destructured = fn.rest || has_default > 1 || has_destructured; if (destructured || has_spread) args = [ make_node(AST_Array, self, { elements: args }) ]; if (destructured) { var tt = new TreeTransformer(function(node, descend) { @@ -8332,10 +8332,15 @@ merge(Compressor.prototype, { argname = argname.transform(tt); if (argname) lhs[index] = argname; }); + var rest = fn.rest && fn.rest.transform(tt); + if (rest) lhs.length = fn.argnames.length; fill_holes(fn, lhs); args[0] = make_node(AST_Assign, self, { operator: "=", - left: make_node(AST_DestructuredArray, fn, { elements: lhs }), + left: make_node(AST_DestructuredArray, fn, { + elements: lhs, + rest: rest, + }), right: args[0], }); } else fn.argnames.forEach(function(argname) { diff --git a/test/compress/rests.js b/test/compress/rests.js index ed927700..6bc5b756 100644 --- a/test/compress/rests.js +++ b/test/compress/rests.js @@ -600,3 +600,49 @@ issue_4538: { expect_stdout: "function" node_version: ">=6" } + +issue_4544_1: { + options = { + keep_fnames: true, + side_effects: true, + } + input: { + try { + (function f(...[ {} ]) {})(); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try { + [ ...[ {} ] ] = []; + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_4544_2: { + options = { + keep_fnames: true, + side_effects: true, + } + input: { + try { + (function f(a, ...[ {} ]) {})([]); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try { + [ , ...[ {} ] ] = [ [] ]; + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=6" +}