From 03aec89f609750b7cd854265bab59f841a01822d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 16 Oct 2021 11:02:23 +0100 Subject: [PATCH] fix corner cases in `strings` & `templates` (#5147) fixes #5145 --- lib/compress.js | 35 ++++++++++++++++++-------- test/compress/concat-strings.js | 15 +++++++++++ test/compress/templates.js | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 0f567f57..a7184109 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10663,7 +10663,8 @@ merge(Compressor.prototype, { && self.left.operator == "+" && self.left.left instanceof AST_String && self.left.left.value == "" - && self.right.is_string(compressor)) { + && self.right.is_string(compressor) + && (self.left.right.is_constant() || !self.right.has_side_effects(compressor))) { self.left = self.left.right; return self.optimize(compressor); } @@ -11392,15 +11393,29 @@ merge(Compressor.prototype, { }).transform(compressor), right: exprs[exprs.length - 1], }).optimize(compressor); - if (strs[0] == "") return make_node(AST_Binary, self, { - operator: "+", - left: exprs[0], - right: make_node(AST_Template, self, { - expressions: exprs.slice(1), - strings: strs.slice(1), - tag: tag, - }).transform(compressor), - }).optimize(compressor); + if (strs[0] == "") { + var left = make_node(AST_Binary, self, { + operator: "+", + left: make_node(AST_String, self, { value: "" }), + right: exprs[0], + }); + for (var i = 1; strs[i] == "" && i < exprs.length; i++) { + left = make_node(AST_Binary, self, { + operator: "+", + left: left, + right: exprs[i], + }); + } + return best_of(compressor, self, make_node(AST_Binary, self, { + operator: "+", + left: left.transform(compressor), + right: make_node(AST_Template, self, { + expressions: exprs.slice(i), + strings: strs.slice(i), + tag: tag, + }).transform(compressor), + }).optimize(compressor)); + } } self.expressions = exprs; self.strings = strs; diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js index 48b8d21c..3512fbce 100644 --- a/test/compress/concat-strings.js +++ b/test/compress/concat-strings.js @@ -289,3 +289,18 @@ issue_3689: { } expect_stdout: "00" } + +issue_5145: { + options = { + strings: true, + } + input: { + var a = []; + console.log("" + a + ((a[0] = 4) + "2")); + } + expect: { + var a = []; + console.log("" + a + (a[0] = 4) + "2"); + } + expect_stdout: "42" +} diff --git a/test/compress/templates.js b/test/compress/templates.js index cbd4b85b..f0b6ebea 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -699,3 +699,47 @@ issue_5136: { expect_stdout: "42" node_version: ">=4" } + +issue_5145_1: { + options = { + strings: true, + templates: true, + } + input: { + var a = []; + console.log(`${a}${a[0] = 42} +`); + } + expect: { + var a = []; + console.log(`${a}${a[0] = 42} +`); + } + expect_stdout: [ + "42", + "", + ] + node_version: ">=4" +} + +issue_5145_2: { + options = { + strings: true, + templates: true, + } + input: { + var a = []; + console.log(`${a}${a}${a[0] = 42} +`); + } + expect: { + var a = []; + console.log("" + a + a + (a[0] = 42) + ` +`); + } + expect_stdout: [ + "42", + "", + ] + node_version: ">=4" +}