fix corner cases in strings & templates (#5147)

fixes #5145
This commit is contained in:
Alex Lam S.L
2021-10-16 11:02:23 +01:00
committed by GitHub
parent faf0190546
commit 03aec89f60
3 changed files with 84 additions and 10 deletions

View File

@@ -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, {
if (strs[0] == "") {
var left = make_node(AST_Binary, self, {
operator: "+",
left: exprs[0],
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(1),
strings: strs.slice(1),
expressions: exprs.slice(i),
strings: strs.slice(i),
tag: tag,
}).transform(compressor),
}).optimize(compressor);
}).optimize(compressor));
}
}
self.expressions = exprs;
self.strings = strs;

View File

@@ -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"
}

View File

@@ -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"
}