Skip to content

Commit

Permalink
fix corner cases in join_vars (#5852)
Browse files Browse the repository at this point in the history
fixes #5849
fixes #5850
  • Loading branch information
alexlamsl authored Jun 17, 2024
1 parent f31311e commit 23d74be
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
18 changes: 9 additions & 9 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4300,18 +4300,18 @@ Compressor.prototype.compress = function(node) {
if (prop instanceof AST_Node) return;
if (!RE_POSITIVE_INTEGER.test("" + prop)) return;
prop = +prop;
var len = value.elements.length;
var elements = value.elements;
var len = elements.length;
if (prop > len + 4) return;
for (var i = Math.min(len, prop + 1); --i >= 0;) {
if (elements[i] instanceof AST_Spread) return;
}
if (prop < len) {
var element = value.elements[prop];
if (element instanceof AST_Hole) {
value.elements[prop] = node.right;
} else {
value.elements[prop] = make_sequence(node, [ element, node.right ]).optimize(compressor);
}
var element = elements[prop].drop_side_effect_free(compressor);
elements[prop] = element ? make_sequence(node, [ element, node.right ]) : node.right;
} else {
while (prop > len) value.elements[len++] = make_node(AST_Hole, value);
value.elements[prop] = node.right;
while (prop > len) elements[len++] = make_node(AST_Hole, value);
elements[prop] = node.right;
}
return true;
}
Expand Down
35 changes: 27 additions & 8 deletions test/compress/join_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ join_array_assignments_1: {
}
input: {
console.log(function () {
var a = ["foo", , "bar"];
var a = [ "foo", , "bar" ];
a[1] = "baz";
a[7] = "moo";
a[0] = "moz";
Expand All @@ -32,7 +32,7 @@ join_array_assignments_1: {
}
expect: {
console.log(function () {
var a = [("foo", "moz"), "baz", "bar", , , , , "moo"];
var a = [ "moz", "baz", "bar", , , , , "moo" ];
return a;
}().join());
}
Expand All @@ -46,7 +46,7 @@ join_array_assignments_2: {
}
input: {
console.log(function () {
var a = ["foo"];
var a = [ "foo" ];
a[1] = "bar";
a[7] = "baz";
a[2] = "moo";
Expand All @@ -55,7 +55,7 @@ join_array_assignments_2: {
}
expect: {
console.log(function () {
var a = ["foo", "bar"];
var a = [ "foo", "bar" ];
a[7] = "baz";
a[2] = "moo";
return a;
Expand All @@ -71,7 +71,7 @@ join_array_assignments_3: {
}
input: {
console.log(function () {
var a = ["foo"];
var a = [ "foo" ];
a[1] = "bar";
a.b = "baz";
a[2] = "moo";
Expand All @@ -80,7 +80,7 @@ join_array_assignments_3: {
}
expect: {
console.log(function () {
var a = ["foo", "bar"];
var a = [ "foo", "bar" ];
a.b = "baz";
a[2] = "moo";
return a;
Expand All @@ -97,7 +97,7 @@ join_array_assignments_4: {
}
input: {
console.log(function () {
var a = ["foo"];
var a = [ "foo" ];
a[0] = "bar";
a[1] = a;
a[2] = "baz";
Expand All @@ -106,7 +106,7 @@ join_array_assignments_4: {
}
expect: {
console.log(function () {
var a = ["bar"];
var a = [ "bar" ];
a[1] = a;
a[2] = "baz";
return a;
Expand Down Expand Up @@ -1508,3 +1508,22 @@ issue_5831: {
}
expect_stdout: "PASS"
}

issue_5849: {
options = {
evaluate: true,
join_vars: true,
side_effects: true,
}
input: {
var a;
a = [ 42 ];
a[0] = "PASS";
console.log(a.join(""));
}
expect: {
var a, a = [ "PASS" ];
console.log(a.join(""));
}
expect_stdout: "PASS"
}
21 changes: 21 additions & 0 deletions test/compress/spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -1253,3 +1253,24 @@ issue_5602: {
]
node_version: ">=6"
}

issue_5850: {
options = {
evaluate: true,
join_vars: true,
unused: true,
}
input: {
var a = [ ..."FAIL" ];
a[0] = "P";
a[2] = a[3] = "S";
console.log(a.join(""));
}
expect: {
var a = [ ..."FAIL" ];
a[0] = "P";
a[2] = a[3] = "S";
console.log(a.join(""));
}
node_version: ">=6"
}

0 comments on commit 23d74be

Please sign in to comment.