diff --git a/lib/propmangle.js b/lib/propmangle.js index d5e13daa..f22ac5b4 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -43,10 +43,11 @@ "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", @@ -54,30 +55,72 @@ var builtins = function() { "-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) { @@ -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); }); diff --git a/test/compress/issue-1770.js b/test/compress/issue-1770.js index cd86d9d5..3b2f8cba 100644 --- a/test/compress/issue-1770.js +++ b/test/compress/issue-1770.js @@ -110,7 +110,9 @@ numeric_literal: { identifier: { mangle = { - properties: true, + properties: { + builtins: true, + }, } input: { var obj = { @@ -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, }; } } diff --git a/test/compress/properties.js b/test/compress/properties.js index cbbe8985..39553d65 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -206,6 +206,7 @@ mangle_unquoted_properties: { } mangle = { properties: { + builtins: true, keep_quoted: true, }, } @@ -305,6 +306,7 @@ mangle_debug_suffix_keep_quoted: { } mangle = { properties: { + builtins: true, debug: "XYZ", keep_quoted: true, reserved: [],