Skip to content

Commit

Permalink
fix corner case in unsafe (#4779)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 15, 2021
1 parent 2619bff commit 149d75c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/compress.js
Expand Up @@ -8533,20 +8533,25 @@ merge(Compressor.prototype, {
var separator = self.args[0];
// [].join() ---> ""
// [].join(x) ---> (x, "")
if (exp.expression.elements.length == 0) return separator ? make_sequence(self, [
separator,
make_node(AST_String, self, { value: "" }),
]).optimize(compressor) : make_node(AST_String, self, { value: "" });
if (exp.expression.elements.length == 0 && !(separator instanceof AST_Spread)) {
return separator ? make_sequence(self, [
separator,
make_node(AST_String, self, { value: "" }),
]).optimize(compressor) : make_node(AST_String, self, { value: "" });
}
if (separator) {
separator = separator.evaluate(compressor);
if (separator instanceof AST_Node) break EXIT; // not a constant
}
var elements = [];
var consts = [];
exp.expression.elements.forEach(function(el) {
for (var i = 0; i < exp.expression.elements.length; i++) {
var el = exp.expression.elements[i];
var value = el.evaluate(compressor);
if (value !== el) {
consts.push(value);
} else if (el instanceof AST_Spread) {
break EXIT;
} else {
if (consts.length > 0) {
elements.push(make_node(AST_String, self, {
Expand All @@ -8556,7 +8561,7 @@ merge(Compressor.prototype, {
}
elements.push(el);
}
});
}
if (consts.length > 0) elements.push(make_node(AST_String, self, {
value: consts.join(separator),
}));
Expand Down
51 changes: 51 additions & 0 deletions test/compress/spreads.js
Expand Up @@ -689,6 +689,57 @@ unused_var_side_effects: {
node_version: ">=8"
}

unsafe_join_1: {
options = {
unsafe: true,
}
input: {
console.log([ ..."foo" ].join());
}
expect: {
console.log([ ..."foo" ].join());
}
expect_stdout: "f,o,o"
node_version: ">=6"
}

unsafe_join_2: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log([ "foo", ..."bar" ].join(""));
}
expect: {
console.log([ "foo", ..."bar" ].join(""));
}
expect_stdout: "foobar"
node_version: ">=6"
}

unsafe_join_3: {
options = {
unsafe: true,
}
input: {
try {
[].join(...console);
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
[].join(...console);
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

issue_4329: {
options = {
objects: true,
Expand Down

0 comments on commit 149d75c

Please sign in to comment.