Skip to content

Commit

Permalink
expand builtins exclusions (#5601)
Browse files Browse the repository at this point in the history
closes #5600
  • Loading branch information
alexlamsl authored Aug 5, 2022
1 parent 41b65af commit 8a07f12
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 50 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
66 changes: 34 additions & 32 deletions test/compress/issue-1770.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ numeric_literal: {

identifier: {
mangle = {
properties: true,
properties: {
builtins: true,
},
}
input: {
var obj = {
Expand Down Expand Up @@ -209,37 +211,37 @@ identifier: {
B: 28,
C: 29,
D: 30,
F: 31,
G: 32,
false: 33,
null: 34,
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,
E: 31,
F: 32,
G: 33,
H: 34,
I: 35,
J: 36,
K: 37,
L: 38,
M: 39,
N: 40,
O: 41,
P: 42,
Q: 43,
R: 44,
S: 45,
T: 46,
U: 47,
V: 48,
W: 49,
X: 50,
Y: 51,
Z: 52,
$: 53,
_: 54,
ee: 55,
te: 56,
ne: 57,
ae: 58,
ie: 59,
oe: 60,
re: 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 8a07f12

Please sign in to comment.