Skip to content

Commit 03aec89

Browse files
authored
fix corner cases in strings & templates (#5147)
fixes #5145
1 parent faf0190 commit 03aec89

3 files changed

Lines changed: 84 additions & 10 deletions

File tree

lib/compress.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10663,7 +10663,8 @@ merge(Compressor.prototype, {
1066310663
&& self.left.operator == "+"
1066410664
&& self.left.left instanceof AST_String
1066510665
&& self.left.left.value == ""
10666-
&& self.right.is_string(compressor)) {
10666+
&& self.right.is_string(compressor)
10667+
&& (self.left.right.is_constant() || !self.right.has_side_effects(compressor))) {
1066710668
self.left = self.left.right;
1066810669
return self.optimize(compressor);
1066910670
}
@@ -11392,15 +11393,29 @@ merge(Compressor.prototype, {
1139211393
}).transform(compressor),
1139311394
right: exprs[exprs.length - 1],
1139411395
}).optimize(compressor);
11395-
if (strs[0] == "") return make_node(AST_Binary, self, {
11396-
operator: "+",
11397-
left: exprs[0],
11398-
right: make_node(AST_Template, self, {
11399-
expressions: exprs.slice(1),
11400-
strings: strs.slice(1),
11401-
tag: tag,
11402-
}).transform(compressor),
11403-
}).optimize(compressor);
11396+
if (strs[0] == "") {
11397+
var left = make_node(AST_Binary, self, {
11398+
operator: "+",
11399+
left: make_node(AST_String, self, { value: "" }),
11400+
right: exprs[0],
11401+
});
11402+
for (var i = 1; strs[i] == "" && i < exprs.length; i++) {
11403+
left = make_node(AST_Binary, self, {
11404+
operator: "+",
11405+
left: left,
11406+
right: exprs[i],
11407+
});
11408+
}
11409+
return best_of(compressor, self, make_node(AST_Binary, self, {
11410+
operator: "+",
11411+
left: left.transform(compressor),
11412+
right: make_node(AST_Template, self, {
11413+
expressions: exprs.slice(i),
11414+
strings: strs.slice(i),
11415+
tag: tag,
11416+
}).transform(compressor),
11417+
}).optimize(compressor));
11418+
}
1140411419
}
1140511420
self.expressions = exprs;
1140611421
self.strings = strs;

test/compress/concat-strings.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,18 @@ issue_3689: {
289289
}
290290
expect_stdout: "00"
291291
}
292+
293+
issue_5145: {
294+
options = {
295+
strings: true,
296+
}
297+
input: {
298+
var a = [];
299+
console.log("" + a + ((a[0] = 4) + "2"));
300+
}
301+
expect: {
302+
var a = [];
303+
console.log("" + a + (a[0] = 4) + "2");
304+
}
305+
expect_stdout: "42"
306+
}

test/compress/templates.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,47 @@ issue_5136: {
699699
expect_stdout: "42"
700700
node_version: ">=4"
701701
}
702+
703+
issue_5145_1: {
704+
options = {
705+
strings: true,
706+
templates: true,
707+
}
708+
input: {
709+
var a = [];
710+
console.log(`${a}${a[0] = 42}
711+
`);
712+
}
713+
expect: {
714+
var a = [];
715+
console.log(`${a}${a[0] = 42}
716+
`);
717+
}
718+
expect_stdout: [
719+
"42",
720+
"",
721+
]
722+
node_version: ">=4"
723+
}
724+
725+
issue_5145_2: {
726+
options = {
727+
strings: true,
728+
templates: true,
729+
}
730+
input: {
731+
var a = [];
732+
console.log(`${a}${a}${a[0] = 42}
733+
`);
734+
}
735+
expect: {
736+
var a = [];
737+
console.log("" + a + a + (a[0] = 42) + `
738+
`);
739+
}
740+
expect_stdout: [
741+
"42",
742+
"",
743+
]
744+
node_version: ">=4"
745+
}

0 commit comments

Comments
 (0)