Skip to content

Commit 8a07f12

Browse files
authored
expand builtins exclusions (#5601)
closes #5600
1 parent 41b65af commit 8a07f12

File tree

3 files changed

+97
-50
lines changed

3 files changed

+97
-50
lines changed

lib/propmangle.js

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,84 @@
4343

4444
"use strict";
4545

46-
var builtins = function() {
46+
function get_builtins() {
4747
var names = new Dictionary();
48-
// NaN will be included due to Number.NaN
48+
// constants
4949
[
50+
"NaN",
5051
"null",
5152
"true",
5253
"false",
5354
"Infinity",
5455
"-Infinity",
5556
"undefined",
5657
].forEach(add);
58+
// global functions
5759
[
58-
Array,
59-
Boolean,
60-
Date,
61-
Error,
62-
Function,
63-
Math,
64-
Number,
65-
Object,
66-
RegExp,
67-
String,
68-
].forEach(function(ctor) {
60+
"encodeURI",
61+
"encodeURIComponent",
62+
"escape",
63+
"eval",
64+
"decodeURI",
65+
"decodeURIComponent",
66+
"isFinite",
67+
"isNaN",
68+
"parseFloat",
69+
"parseInt",
70+
"unescape",
71+
].forEach(add);
72+
// global constructors & objects
73+
var global = Function("return this")();
74+
[
75+
"Array",
76+
"ArrayBuffer",
77+
"Atomics",
78+
"BigInt",
79+
"Boolean",
80+
"console",
81+
"DataView",
82+
"Date",
83+
"Error",
84+
"Function",
85+
"Int8Array",
86+
"Intl",
87+
"JSON",
88+
"Map",
89+
"Math",
90+
"Number",
91+
"Object",
92+
"Promise",
93+
"Proxy",
94+
"Reflect",
95+
"RegExp",
96+
"Set",
97+
"String",
98+
"Symbol",
99+
"WebAssembly",
100+
].forEach(function(name) {
101+
add(name);
102+
var ctor = global[name];
103+
if (!ctor) return;
69104
Object.getOwnPropertyNames(ctor).map(add);
70-
if (ctor.prototype) {
105+
if (typeof ctor != "function") return;
106+
if (ctor.__proto__) Object.getOwnPropertyNames(ctor.__proto__).map(add);
107+
if (ctor.prototype) Object.getOwnPropertyNames(ctor.prototype).map(add);
108+
try {
71109
Object.getOwnPropertyNames(new ctor()).map(add);
72-
Object.getOwnPropertyNames(ctor.prototype).map(add);
110+
} catch (e) {
111+
try {
112+
Object.getOwnPropertyNames(ctor()).map(add);
113+
} catch (e) {}
73114
}
74115
});
75-
return names;
116+
return (get_builtins = function() {
117+
return names.clone();
118+
})();
76119

77120
function add(name) {
78121
names.set(name, true);
79122
}
80-
}();
123+
}
81124

82125
function reserve_quoted_keys(ast, reserved) {
83126
ast.walk(new TreeWalker(function(node) {
@@ -116,7 +159,7 @@ function mangle_properties(ast, options) {
116159
reserved: null,
117160
}, true);
118161

119-
var reserved = options.builtins ? new Dictionary() : builtins.clone();
162+
var reserved = options.builtins ? new Dictionary() : get_builtins();
120163
if (Array.isArray(options.reserved)) options.reserved.forEach(function(name) {
121164
reserved.set(name, true);
122165
});

test/compress/issue-1770.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ numeric_literal: {
110110

111111
identifier: {
112112
mangle = {
113-
properties: true,
113+
properties: {
114+
builtins: true,
115+
},
114116
}
115117
input: {
116118
var obj = {
@@ -209,37 +211,37 @@ identifier: {
209211
B: 28,
210212
C: 29,
211213
D: 30,
212-
F: 31,
213-
G: 32,
214-
false: 33,
215-
null: 34,
216-
true: 35,
217-
H: 36,
218-
I: 37,
219-
J: 38,
220-
K: 39,
221-
L: 40,
222-
M: 41,
223-
N: 42,
224-
O: 43,
225-
P: 44,
226-
Q: 45,
227-
R: 46,
228-
S: 47,
229-
T: 48,
230-
U: 49,
231-
V: 50,
232-
W: 51,
233-
X: 52,
234-
Y: 53,
235-
Z: 54,
236-
$: 55,
237-
_: 56,
238-
ee: 57,
239-
te: 58,
240-
ne: 59,
241-
ae: 60,
242-
ie: 61,
214+
E: 31,
215+
F: 32,
216+
G: 33,
217+
H: 34,
218+
I: 35,
219+
J: 36,
220+
K: 37,
221+
L: 38,
222+
M: 39,
223+
N: 40,
224+
O: 41,
225+
P: 42,
226+
Q: 43,
227+
R: 44,
228+
S: 45,
229+
T: 46,
230+
U: 47,
231+
V: 48,
232+
W: 49,
233+
X: 50,
234+
Y: 51,
235+
Z: 52,
236+
$: 53,
237+
_: 54,
238+
ee: 55,
239+
te: 56,
240+
ne: 57,
241+
ae: 58,
242+
ie: 59,
243+
oe: 60,
244+
re: 61,
243245
};
244246
}
245247
}

test/compress/properties.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ mangle_unquoted_properties: {
206206
}
207207
mangle = {
208208
properties: {
209+
builtins: true,
209210
keep_quoted: true,
210211
},
211212
}
@@ -305,6 +306,7 @@ mangle_debug_suffix_keep_quoted: {
305306
}
306307
mangle = {
307308
properties: {
309+
builtins: true,
308310
debug: "XYZ",
309311
keep_quoted: true,
310312
reserved: [],

0 commit comments

Comments
 (0)