Skip to content

Commit

Permalink
expand builtins exclusions
Browse files Browse the repository at this point in the history
closes #5600
  • Loading branch information
alexlamsl committed Aug 5, 2022
1 parent 41b65af commit 3d580d1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
79 changes: 61 additions & 18 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,84 @@

"use strict";

var builtins = function() {
function get_builtins() {
var names = new Dictionary();
// NaN will be included due to Number.NaN
// constants
[
"NaN",
"null",
"true",
"false",
"Infinity",
"-Infinity",
"undefined",
].forEach(add);
// global functions
[
Array,
Boolean,
Date,
Error,
Function,
Math,
Number,
Object,
RegExp,
String,
].forEach(function(ctor) {
"encodeURI",
"encodeURIComponent",
"escape",
"eval",
"decodeURI",
"decodeURIComponent",
"isFinite",
"isNaN",
"parseFloat",
"parseInt",
"unescape",
].forEach(add);
// global constructors & objects
var global = Function("return this")();
[
"Array",
"ArrayBuffer",
"Atomics",
"BigInt",
"Boolean",
"console",
"DataView",
"Date",
"Error",
"Function",
"Int8Array",
"Intl",
"JSON",
"Map",
"Math",
"Number",
"Object",
"Promise",
"Proxy",
"Reflect",
"RegExp",
"Set",
"String",
"Symbol",
"WebAssembly",
].forEach(function(name) {
add(name);
var ctor = global[name];
if (!ctor) return;
Object.getOwnPropertyNames(ctor).map(add);
if (ctor.prototype) {
if (typeof ctor != "function") return;
if (ctor.__proto__) Object.getOwnPropertyNames(ctor.__proto__).map(add);
if (ctor.prototype) Object.getOwnPropertyNames(ctor.prototype).map(add);
try {
Object.getOwnPropertyNames(new ctor()).map(add);
Object.getOwnPropertyNames(ctor.prototype).map(add);
} catch (e) {
try {
Object.getOwnPropertyNames(ctor()).map(add);
} catch (e) {}
}
});
return names;
return (get_builtins = function() {
return names.clone();
})();

function add(name) {
names.set(name, true);
}
}();
}

function reserve_quoted_keys(ast, reserved) {
ast.walk(new TreeWalker(function(node) {
Expand Down Expand Up @@ -116,7 +159,7 @@ function mangle_properties(ast, options) {
reserved: null,
}, true);

var reserved = options.builtins ? new Dictionary() : builtins.clone();
var reserved = options.builtins ? new Dictionary() : get_builtins();
if (Array.isArray(options.reserved)) options.reserved.forEach(function(name) {
reserved.set(name, true);
});
Expand Down
48 changes: 24 additions & 24 deletions test/compress/issue-1770.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,30 @@ identifier: {
true: 35,
H: 36,
I: 37,
J: 38,
K: 39,
L: 40,
M: 41,
N: 42,
O: 43,
P: 44,
Q: 45,
R: 46,
S: 47,
T: 48,
U: 49,
V: 50,
W: 51,
X: 52,
Y: 53,
Z: 54,
$: 55,
_: 56,
ee: 57,
te: 58,
ne: 59,
ae: 60,
ie: 61,
catch: 38,
J: 39,
K: 40,
L: 41,
M: 42,
delete: 43,
N: 44,
O: 45,
finally: 46,
for: 47,
P: 48,
Q: 49,
R: 50,
S: 51,
T: 52,
U: 53,
V: 54,
W: 55,
X: 56,
Y: 57,
Z: 58,
$: 59,
_: 60,
ee: 61,
};
}
}
2 changes: 2 additions & 0 deletions test/compress/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ mangle_unquoted_properties: {
}
mangle = {
properties: {
builtins: true,
keep_quoted: true,
},
}
Expand Down Expand Up @@ -305,6 +306,7 @@ mangle_debug_suffix_keep_quoted: {
}
mangle = {
properties: {
builtins: true,
debug: "XYZ",
keep_quoted: true,
reserved: [],
Expand Down

0 comments on commit 3d580d1

Please sign in to comment.