From 5a4cd09938e46bbf7d9a875e03ce7e99c6e6c442 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 9 Aug 2022 15:54:29 +0100 Subject: [PATCH] enhance `inline` (#5610) --- lib/compress.js | 15 ++++++++++++--- test/compress/functions.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 1882b44278a..a01dcfc1d9d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10825,7 +10825,7 @@ Compressor.prototype.compress = function(node) { var begin; var in_order = []; var side_effects = false; - value.walk(new TreeWalker(function(node, descend) { + var tw = new TreeWalker(function(node, descend) { if (abort) return true; if (node instanceof AST_Binary && lazy_op[node.operator] || node instanceof AST_Conditional) { @@ -10841,7 +10841,7 @@ Compressor.prototype.compress = function(node) { return; } if (def.init instanceof AST_LambdaDefinition) return abort = true; - if (is_lhs(node, this.parent())) return abort = true; + if (is_lhs(node, tw.parent())) return abort = true; var index = resolve_index(def); if (!(begin < index)) begin = index; if (!in_order) return; @@ -10852,12 +10852,21 @@ Compressor.prototype.compress = function(node) { } return; } + if (side_effects) return; + if (node instanceof AST_Assign && node.left instanceof AST_PropAccess) { + node.left.expression.walk(tw); + if (node.left instanceof AST_Sub) node.left.property.walk(tw); + node.right.walk(tw); + side_effects = true; + return true; + } if (node.has_side_effects(compressor)) { descend(); side_effects = true; return true; } - })); + }); + value.walk(tw); if (abort) return; var end = self.args.length; if (in_order && fn.argnames.length >= end) { diff --git a/test/compress/functions.js b/test/compress/functions.js index dd6f26a8bd2..7dada302cde 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -5512,6 +5512,40 @@ substitute_use_strict: { ] } +substitute_assignment: { + options = { + evaluate: true, + inline: true, + passes: 2, + properties: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f(a, b, c) { + a[b] = c; + } + var o = {}; + f(o, 42, null); + f(o, "foo", "bar"); + for (var k in o) + console.log(k, o[k]); + } + expect: { + var o = {}; + o[42] = null; + o.foo = "bar"; + for (var k in o) + console.log(k, o[k]); + } + expect_stdout: [ + "42 null", + "foo bar", + ] +} + issue_3833_1: { options = { inline: 3,