Skip to content

Commit

Permalink
fix corner case in unused (#5847)
Browse files Browse the repository at this point in the history
fixes #5843
  • Loading branch information
alexlamsl authored Jun 17, 2024
1 parent 8dc99fa commit 87c9edb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8284,6 +8284,7 @@ Compressor.prototype.compress = function(node) {
if (prop instanceof AST_Spread) return prop;
var key = prop_keys[index];
if (key instanceof AST_Node) return prop;
if (key === "__proto__") return prop;
if (drop_keys.has(key)) {
var mapped = drop_keys.get(key);
if (!mapped) return prop;
Expand Down Expand Up @@ -8317,7 +8318,10 @@ Compressor.prototype.compress = function(node) {
if (value.has_side_effects(compressor) && prop.value.match_symbol(function(node) {
return node instanceof AST_PropAccess;
})) break;
value = make_node(AST_Sub, node, {
value = is_identifier_string(prop.key) ? make_node(AST_Dot, node, {
expression: value,
property: prop.key,
}) : make_node(AST_Sub, node, {
expression: value,
property: make_node_from_constant(prop.key, prop),
});
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 @@ -2293,7 +2293,7 @@ issue_5340_3: {
}
expect: {
var a;
(function() {})(a = true["p"]);
(function() {})(a = true.p);
console.log(a);
}
expect_stdout: "undefined"
Expand Down
55 changes: 53 additions & 2 deletions test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ side_effects_object: {
}
}
expect: {
var a = null, c = (console, 42["c"]);
var a = null, c = (console, 42..c);
try {
c[a = "PASS"];
} catch (e) {
Expand Down Expand Up @@ -1684,7 +1684,7 @@ singleton_1: {
expect: {
var b, a = "P"[0], o = {};
o.p = [ "FAIL"["1"] ][0];
o.q = { foo: "S"[0] }["foo"];
o.q = { foo: "S"[0] }.foo;
[ b = "S" ] = [];
console.log(a + o.p + o.q + b);
}
Expand Down Expand Up @@ -3886,3 +3886,54 @@ issue_5651: {
expect_stdout: true
node_version: ">=6"
}

issue_5843_1: {
options = {
unused: true,
}
input: {
var { p: a } = {
__proto__: {
p: "PASS",
},
};
console.log(a);
}
expect: {
var a = {
__proto__: {
p: "PASS",
},
}.p;
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5843_2: {
options = {
side_effects: true,
unused: true,
}
input: {
var a;
({ p: a } = {
__proto__: {
p: "PASS",
},
});
console.log(a);
}
expect: {
var a;
a = {
__proto__: {
p: "PASS",
},
}.p;
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}
2 changes: 1 addition & 1 deletion test/compress/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ hoist_exports_2: {
}
}
expect: {
let e, a = 42["foo"];
let e, a = 42..foo;
function f(t, { [e]: o }) {
t(o, f);
}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ issue_5076_1: {
expect: {
var a;
console.log("PASS"),
a = 42["a"];
a = 42..a;
}
expect_stdout: "PASS"
node_version: ">=6"
Expand Down

0 comments on commit 87c9edb

Please sign in to comment.