Skip to content

Commit

Permalink
fix corner case in join_vars (#5832)
Browse files Browse the repository at this point in the history
fixes #5831
  • Loading branch information
alexlamsl authored Jun 8, 2024
1 parent 2cb6454 commit 3dfb379
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4265,8 +4265,17 @@ Compressor.prototype.compress = function(node) {
prop = +prop;
var len = value.elements.length;
if (prop > len + 4) return;
while (len < prop) value.elements[len++] = make_node(AST_Hole, value);
value.elements[prop] = node.right;
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);
}
} else {
while (prop > len) value.elements[len++] = make_node(AST_Hole, value);
value.elements[prop] = node.right;
}
return true;
}

Expand Down
18 changes: 17 additions & 1 deletion test/compress/join_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ join_array_assignments_1: {
}
expect: {
console.log(function () {
var a = ["moz", "baz", "bar", , , , , "moo"];
var a = [("foo", "moz"), "baz", "bar", , , , , "moo"];
return a;
}().join());
}
Expand Down Expand Up @@ -93,6 +93,7 @@ join_array_assignments_4: {
options = {
evaluate: true,
join_vars: true,
side_effects: true,
}
input: {
console.log(function () {
Expand Down Expand Up @@ -1492,3 +1493,18 @@ issue_5175: {
}
expect_stdout: "PASS PASS"
}

issue_5831: {
options = {
evaluate: true,
join_vars: true,
}
input: {
var a = [ console.log("PASS") ];
a[0] = 42;
}
expect: {
var a = [ (console.log("PASS"), 42) ];
}
expect_stdout: "PASS"
}

0 comments on commit 3dfb379

Please sign in to comment.