Skip to content

Commit 3fa2086

Browse files
authored
improve usability of mangle.properties (#5683)
fixes #5682
1 parent 8e65413 commit 3fa2086

File tree

7 files changed

+260
-41
lines changed

7 files changed

+260
-41
lines changed

lib/propmangle.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ function get_builtins() {
124124

125125
function reserve_quoted_keys(ast, reserved) {
126126
ast.walk(new TreeWalker(function(node) {
127-
if (node instanceof AST_ClassProperty) {
128-
if (node.start && node.start.quote) add(node.key);
127+
if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
128+
if (node.key instanceof AST_Node) {
129+
addStrings(node.key, add);
130+
} else if (node.start && node.start.quote) {
131+
add(node.key);
132+
}
129133
} else if (node instanceof AST_Dot) {
130134
if (node.quoted) add(node.property);
131-
} else if (node instanceof AST_ObjectProperty) {
132-
if (node.start && node.start.quote) add(node.key);
133135
} else if (node instanceof AST_Sub) {
134136
addStrings(node.property, add);
135137
}
@@ -191,9 +193,7 @@ function mangle_properties(ast, options) {
191193

192194
// step 1: find candidates to mangle
193195
ast.walk(new TreeWalker(function(node) {
194-
if (node instanceof AST_Binary) {
195-
if (node.operator == "in") addStrings(node.left, add);
196-
} else if (node.TYPE == "Call") {
196+
if (node.TYPE == "Call") {
197197
var exp = node.expression;
198198
if (exp instanceof AST_Dot) switch (exp.property) {
199199
case "defineProperty":
@@ -210,14 +210,16 @@ function mangle_properties(ast, options) {
210210
addStrings(node.args[0], add);
211211
break;
212212
}
213-
} else if (node instanceof AST_ClassProperty) {
214-
if (typeof node.key == "string") add(node.key);
213+
} else if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
214+
if (node.key instanceof AST_Node) {
215+
addStrings(node.key, add);
216+
} else {
217+
add(node.key);
218+
}
215219
} else if (node instanceof AST_Dot) {
216-
add(node.property);
217-
} else if (node instanceof AST_ObjectProperty) {
218-
if (typeof node.key == "string") add(node.key);
220+
if (is_lhs(node, this.parent())) add(node.property);
219221
} else if (node instanceof AST_Sub) {
220-
addStrings(node.property, add);
222+
if (is_lhs(node, this.parent())) addStrings(node.property, add);
221223
}
222224
}));
223225

@@ -242,12 +244,14 @@ function mangle_properties(ast, options) {
242244
mangleStrings(node.args[0]);
243245
break;
244246
}
245-
} else if (node instanceof AST_ClassProperty) {
246-
if (typeof node.key == "string") node.key = mangle(node.key);
247+
} else if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
248+
if (node.key instanceof AST_Node) {
249+
mangleStrings(node.key);
250+
} else {
251+
node.key = mangle(node.key);
252+
}
247253
} else if (node instanceof AST_Dot) {
248254
node.property = mangle(node.property);
249-
} else if (node instanceof AST_ObjectProperty) {
250-
if (typeof node.key == "string") node.key = mangle(node.key);
251255
} else if (node instanceof AST_Sub) {
252256
if (!options.keep_quoted) mangleStrings(node.property);
253257
}

test/compress/classes.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,11 +2202,11 @@ mangle_properties: {
22022202
expect_stdout: "PASS 42"
22032203
expect_warnings: [
22042204
"INFO: Preserving reserved property q",
2205-
"INFO: Preserving reserved property log",
22062205
"INFO: Mapping property #P to #t",
22072206
"INFO: Mapping property Q to s",
22082207
"INFO: Mapping property #p to #i",
22092208
"INFO: Mapping property r to e",
2209+
"INFO: Preserving reserved property log",
22102210
]
22112211
node_version: ">=14.6"
22122212
}
@@ -3619,3 +3619,59 @@ issue_5662: {
36193619
expect_stdout: "PASS"
36203620
node_version: ">=6"
36213621
}
3622+
3623+
issue_5682_class_key: {
3624+
mangle = {
3625+
properties: true,
3626+
}
3627+
input: {
3628+
"use strict";
3629+
function f(a) {
3630+
return "foo" in a;
3631+
}
3632+
class A {
3633+
foo() {}
3634+
}
3635+
console.log(f(new A()) ? "PASS" : "FAIL");
3636+
}
3637+
expect: {
3638+
"use strict";
3639+
function f(o) {
3640+
return "o" in o;
3641+
}
3642+
class A {
3643+
o() {}
3644+
}
3645+
console.log(f(new A()) ? "PASS" : "FAIL");
3646+
}
3647+
expect_stdout: "PASS"
3648+
node_version: ">=4"
3649+
}
3650+
3651+
issue_5682_class_key_computed: {
3652+
mangle = {
3653+
properties: true,
3654+
}
3655+
input: {
3656+
"use strict";
3657+
function f(a) {
3658+
return "foo" in a;
3659+
}
3660+
class A {
3661+
["foo"]() {}
3662+
}
3663+
console.log(f(new A()) ? "PASS" : "FAIL");
3664+
}
3665+
expect: {
3666+
"use strict";
3667+
function f(o) {
3668+
return "o" in o;
3669+
}
3670+
class A {
3671+
["o"]() {}
3672+
}
3673+
console.log(f(new A()) ? "PASS" : "FAIL");
3674+
}
3675+
expect_stdout: "PASS"
3676+
node_version: ">=4"
3677+
}

test/compress/issue-1770.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ numeric_literal: {
115115
"8 7 8",
116116
]
117117
expect_warnings: [
118-
"INFO: Preserving reserved property log",
119118
"INFO: Mapping property 0x25 to o",
120119
"INFO: Mapping property 1E42 to b",
120+
"INFO: Preserving reserved property log",
121121
]
122122
}
123123

test/compress/issue-747.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ dont_reuse_prop: {
2121
expect_stdout: "123"
2222
expect_warnings: [
2323
"INFO: Preserving excluded property a",
24-
"INFO: Preserving reserved property log",
2524
"INFO: Mapping property asd to b",
25+
"INFO: Preserving reserved property log",
2626
]
2727
}
2828

@@ -49,7 +49,7 @@ unmangleable_props_should_always_be_reserved: {
4949
expect_stdout: "123"
5050
expect_warnings: [
5151
"INFO: Preserving excluded property a",
52-
"INFO: Preserving reserved property log",
5352
"INFO: Mapping property asd to b",
53+
"INFO: Preserving reserved property log",
5454
]
5555
}

test/compress/properties.js

Lines changed: 165 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ mangle_properties_1: {
147147
a["a"] = "bar";
148148
a.b = "red";
149149
x = {o: 10};
150-
a.r(x.o, a.a);
151-
a['r']({b: "blue", a: "baz"});
150+
a.run(x.o, a.a);
151+
a['run']({b: "blue", a: "baz"});
152152
}
153153
}
154154

@@ -962,14 +962,14 @@ issue_2256: {
962962
}
963963
input: {
964964
({ "keep": 42 });
965-
global.keep = global.change;
965+
global.keep = global.change = "PASS";
966966
console.log(keep);
967967
}
968968
expect: {
969-
global.keep = global.l;
969+
global.keep = global.l = "PASS";
970970
console.log(keep);
971971
}
972-
expect_stdout: "undefined"
972+
expect_stdout: "PASS"
973973
}
974974

975975
lhs_prop_1: {
@@ -1645,3 +1645,163 @@ issue_5177: {
16451645
expect_stdout: "PASS"
16461646
node_version: ">=4"
16471647
}
1648+
1649+
issue_5682_in_1: {
1650+
mangle = {
1651+
properties: true,
1652+
}
1653+
input: {
1654+
function f(a) {
1655+
return "foo" in a;
1656+
}
1657+
var o = {};
1658+
var p = "foo";
1659+
o[p] = 42;
1660+
console.log(f(o) ? "PASS" : "FAIL");
1661+
}
1662+
expect: {
1663+
function f(o) {
1664+
return "foo" in o;
1665+
}
1666+
var o = {};
1667+
var p = "foo";
1668+
o[p] = 42;
1669+
console.log(f(o) ? "PASS" : "FAIL");
1670+
}
1671+
expect_stdout: "PASS"
1672+
}
1673+
1674+
issue_5682_in_2: {
1675+
mangle = {
1676+
properties: true,
1677+
}
1678+
input: {
1679+
function f(a) {
1680+
return "foo" in a;
1681+
}
1682+
var o = { foo: 42 };
1683+
console.log(f(o) ? "PASS" : "FAIL");
1684+
}
1685+
expect: {
1686+
function f(o) {
1687+
return "o" in o;
1688+
}
1689+
var o = { o: 42 };
1690+
console.log(f(o) ? "PASS" : "FAIL");
1691+
}
1692+
expect_stdout: "PASS"
1693+
}
1694+
1695+
issue_5682_dot_1: {
1696+
mangle = {
1697+
properties: true,
1698+
}
1699+
input: {
1700+
function f(a) {
1701+
return a.foo;
1702+
}
1703+
var o = {};
1704+
var p = "foo";
1705+
o[p] = "PASS";
1706+
console.log(f(o));
1707+
}
1708+
expect: {
1709+
function f(o) {
1710+
return o.foo;
1711+
}
1712+
var o = {};
1713+
var p = "foo";
1714+
o[p] = "PASS";
1715+
console.log(f(o));
1716+
}
1717+
expect_stdout: "PASS"
1718+
}
1719+
1720+
issue_5682_dot_2: {
1721+
mangle = {
1722+
properties: true,
1723+
}
1724+
input: {
1725+
function f(a) {
1726+
return a.foo;
1727+
}
1728+
var o = { foo: "PASS" };
1729+
console.log(f(o));
1730+
}
1731+
expect: {
1732+
function f(o) {
1733+
return o.o;
1734+
}
1735+
var o = { o: "PASS" };
1736+
console.log(f(o));
1737+
}
1738+
expect_stdout: "PASS"
1739+
}
1740+
1741+
issue_5682_dot_2_computed: {
1742+
mangle = {
1743+
properties: true,
1744+
}
1745+
input: {
1746+
function f(a) {
1747+
return a.foo;
1748+
}
1749+
var o = { ["foo"]: "PASS" };
1750+
console.log(f(o));
1751+
}
1752+
expect: {
1753+
function f(o) {
1754+
return o.o;
1755+
}
1756+
var o = { ["o"]: "PASS" };
1757+
console.log(f(o));
1758+
}
1759+
expect_stdout: "PASS"
1760+
node_version: ">=4"
1761+
}
1762+
1763+
issue_5682_sub_1: {
1764+
mangle = {
1765+
properties: true,
1766+
}
1767+
input: {
1768+
function f(a) {
1769+
return a["foo"];
1770+
}
1771+
var o = {};
1772+
var p = "foo";
1773+
o[p] = "PASS";
1774+
console.log(f(o));
1775+
}
1776+
expect: {
1777+
function f(o) {
1778+
return o["foo"];
1779+
}
1780+
var o = {};
1781+
var p = "foo";
1782+
o[p] = "PASS";
1783+
console.log(f(o));
1784+
}
1785+
expect_stdout: "PASS"
1786+
}
1787+
1788+
issue_5682_sub_2: {
1789+
mangle = {
1790+
properties: true,
1791+
}
1792+
input: {
1793+
function f(a) {
1794+
return a["foo"];
1795+
}
1796+
var o = { foo: "PASS" };
1797+
console.log(f(o));
1798+
}
1799+
expect: {
1800+
function f(o) {
1801+
return o["o"];
1802+
}
1803+
var o = { o: "PASS" };
1804+
console.log(f(o));
1805+
}
1806+
expect_stdout: "PASS"
1807+
}

0 commit comments

Comments
 (0)