Skip to content

Commit

Permalink
fix corner case in unused (#5668)
Browse files Browse the repository at this point in the history
fixes #5663
  • Loading branch information
alexlamsl authored Sep 17, 2022
1 parent e4bff31 commit eb22f01
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7937,7 +7937,14 @@ Compressor.prototype.compress = function(node) {
drop = false;
value = value.fixed_value();
}
var values = value instanceof AST_Array && value.elements;
var native, values;
if (value instanceof AST_Array) {
native = true;
values = value.elements;
} else {
native = value && value.is_string(compressor);
values = false;
}
var elements = [], newValues = drop && [], pos = 0;
node.elements.forEach(function(element, index) {
value = values && values[index];
Expand Down Expand Up @@ -7988,8 +7995,9 @@ Compressor.prototype.compress = function(node) {
value = value.clone();
value.elements = newValues;
}
if (!node.rest && (value instanceof AST_Array
|| value && value.is_string(compressor))) switch (elements.length) {
if (!native) {
elements.length = node.elements.length;
} else if (!node.rest) switch (elements.length) {
case 0:
if (node === root) break;
if (drop) value = value.drop_side_effect_free(compressor);
Expand Down
2 changes: 1 addition & 1 deletion test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ issue_5533_2_drop_fargs: {
try {
(function() {
for (;;) {
var [ [] = [] ] = [];
var [ [ , ] = [] ] = [];
throw "PASS";
}
})();
Expand Down
2 changes: 1 addition & 1 deletion test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ keep_reference: {
}
expect: {
var a = [ {}, 42 ];
var [ b ] = a;
var b = a[0];
console.log(a[0] === b ? "PASS" : "FAIL");
}
expect_stdout: "PASS"
Expand Down
34 changes: 34 additions & 0 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -1735,3 +1735,37 @@ issue_5576: {
]
node_version: ">=10"
}

issue_5663: {
options = {
toplevel: true,
unused: true,
}
input: {
var [ , a ] = function*() {
console.log("foo");
yield console.log("bar");
console.log("baz");
yield console.log("moo");
console.log("moz");
yield FAIL;
}();
}
expect: {
var [ , , ] = function*() {
console.log("foo");
yield console.log("bar");
console.log("baz");
yield console.log("moo");
console.log("moz");
yield FAIL;
}();
}
expect_stdout: [
"foo",
"bar",
"baz",
"moo",
]
node_version: ">=6"
}

0 comments on commit eb22f01

Please sign in to comment.