Skip to content

Commit eb22f01

Browse files
authored
fix corner case in unused (#5668)
fixes #5663
1 parent e4bff31 commit eb22f01

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

lib/compress.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7937,7 +7937,14 @@ Compressor.prototype.compress = function(node) {
79377937
drop = false;
79387938
value = value.fixed_value();
79397939
}
7940-
var values = value instanceof AST_Array && value.elements;
7940+
var native, values;
7941+
if (value instanceof AST_Array) {
7942+
native = true;
7943+
values = value.elements;
7944+
} else {
7945+
native = value && value.is_string(compressor);
7946+
values = false;
7947+
}
79417948
var elements = [], newValues = drop && [], pos = 0;
79427949
node.elements.forEach(function(element, index) {
79437950
value = values && values[index];
@@ -7988,8 +7995,9 @@ Compressor.prototype.compress = function(node) {
79887995
value = value.clone();
79897996
value.elements = newValues;
79907997
}
7991-
if (!node.rest && (value instanceof AST_Array
7992-
|| value && value.is_string(compressor))) switch (elements.length) {
7998+
if (!native) {
7999+
elements.length = node.elements.length;
8000+
} else if (!node.rest) switch (elements.length) {
79938001
case 0:
79948002
if (node === root) break;
79958003
if (drop) value = value.drop_side_effect_free(compressor);

test/compress/default-values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ issue_5533_2_drop_fargs: {
27132713
try {
27142714
(function() {
27152715
for (;;) {
2716-
var [ [] = [] ] = [];
2716+
var [ [ , ] = [] ] = [];
27172717
throw "PASS";
27182718
}
27192719
})();

test/compress/destructured.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ keep_reference: {
13061306
}
13071307
expect: {
13081308
var a = [ {}, 42 ];
1309-
var [ b ] = a;
1309+
var b = a[0];
13101310
console.log(a[0] === b ? "PASS" : "FAIL");
13111311
}
13121312
expect_stdout: "PASS"

test/compress/yields.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,3 +1735,37 @@ issue_5576: {
17351735
]
17361736
node_version: ">=10"
17371737
}
1738+
1739+
issue_5663: {
1740+
options = {
1741+
toplevel: true,
1742+
unused: true,
1743+
}
1744+
input: {
1745+
var [ , a ] = function*() {
1746+
console.log("foo");
1747+
yield console.log("bar");
1748+
console.log("baz");
1749+
yield console.log("moo");
1750+
console.log("moz");
1751+
yield FAIL;
1752+
}();
1753+
}
1754+
expect: {
1755+
var [ , , ] = function*() {
1756+
console.log("foo");
1757+
yield console.log("bar");
1758+
console.log("baz");
1759+
yield console.log("moo");
1760+
console.log("moz");
1761+
yield FAIL;
1762+
}();
1763+
}
1764+
expect_stdout: [
1765+
"foo",
1766+
"bar",
1767+
"baz",
1768+
"moo",
1769+
]
1770+
node_version: ">=6"
1771+
}

0 commit comments

Comments
 (0)