propmangle.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. /* global global, self */
  35. import {
  36. defaults,
  37. push_uniq,
  38. } from "./utils/index.js";
  39. import { base54 } from "./scope.js";
  40. import {
  41. AST_Binary,
  42. AST_Call,
  43. AST_ClassPrivateProperty,
  44. AST_Conditional,
  45. AST_Dot,
  46. AST_DotHash,
  47. AST_ObjectKeyVal,
  48. AST_ObjectProperty,
  49. AST_PrivateMethod,
  50. AST_Sequence,
  51. AST_String,
  52. AST_Sub,
  53. TreeTransformer,
  54. TreeWalker,
  55. } from "./ast.js";
  56. import { domprops } from "../tools/domprops.js";
  57. function find_builtins(reserved) {
  58. domprops.forEach(add);
  59. // Compatibility fix for some standard defined globals not defined on every js environment
  60. var new_globals = ["Symbol", "Map", "Promise", "Proxy", "Reflect", "Set", "WeakMap", "WeakSet"];
  61. var objects = {};
  62. var global_ref = typeof global === "object" ? global : self;
  63. new_globals.forEach(function (new_global) {
  64. objects[new_global] = global_ref[new_global] || new Function();
  65. });
  66. [
  67. "null",
  68. "true",
  69. "false",
  70. "NaN",
  71. "Infinity",
  72. "-Infinity",
  73. "undefined",
  74. ].forEach(add);
  75. [ Object, Array, Function, Number,
  76. String, Boolean, Error, Math,
  77. Date, RegExp, objects.Symbol, ArrayBuffer,
  78. DataView, decodeURI, decodeURIComponent,
  79. encodeURI, encodeURIComponent, eval, EvalError,
  80. Float32Array, Float64Array, Int8Array, Int16Array,
  81. Int32Array, isFinite, isNaN, JSON, objects.Map, parseFloat,
  82. parseInt, objects.Promise, objects.Proxy, RangeError, ReferenceError,
  83. objects.Reflect, objects.Set, SyntaxError, TypeError, Uint8Array,
  84. Uint8ClampedArray, Uint16Array, Uint32Array, URIError,
  85. objects.WeakMap, objects.WeakSet
  86. ].forEach(function(ctor) {
  87. Object.getOwnPropertyNames(ctor).map(add);
  88. if (ctor.prototype) {
  89. Object.getOwnPropertyNames(ctor.prototype).map(add);
  90. }
  91. });
  92. function add(name) {
  93. reserved.add(name);
  94. }
  95. }
  96. function reserve_quoted_keys(ast, reserved) {
  97. function add(name) {
  98. push_uniq(reserved, name);
  99. }
  100. ast.walk(new TreeWalker(function(node) {
  101. if (node instanceof AST_ObjectKeyVal && node.quote) {
  102. add(node.key);
  103. } else if (node instanceof AST_ObjectProperty && node.quote) {
  104. add(node.key.name);
  105. } else if (node instanceof AST_Sub) {
  106. addStrings(node.property, add);
  107. }
  108. }));
  109. }
  110. function addStrings(node, add) {
  111. node.walk(new TreeWalker(function(node) {
  112. if (node instanceof AST_Sequence) {
  113. addStrings(node.tail_node(), add);
  114. } else if (node instanceof AST_String) {
  115. add(node.value);
  116. } else if (node instanceof AST_Conditional) {
  117. addStrings(node.consequent, add);
  118. addStrings(node.alternative, add);
  119. }
  120. return true;
  121. }));
  122. }
  123. function mangle_properties(ast, options) {
  124. options = defaults(options, {
  125. builtins: false,
  126. cache: null,
  127. debug: false,
  128. keep_quoted: false,
  129. only_cache: false,
  130. regex: null,
  131. reserved: null,
  132. undeclared: false,
  133. }, true);
  134. var reserved_option = options.reserved;
  135. if (!Array.isArray(reserved_option)) reserved_option = [reserved_option];
  136. var reserved = new Set(reserved_option);
  137. if (!options.builtins) find_builtins(reserved);
  138. var cname = -1;
  139. var cprivate = -1;
  140. var cache;
  141. var private_cache = new Map();
  142. if (options.cache) {
  143. cache = options.cache.props;
  144. cache.forEach(function(mangled_name) {
  145. reserved.add(mangled_name);
  146. });
  147. } else {
  148. cache = new Map();
  149. }
  150. var regex = options.regex && new RegExp(options.regex);
  151. // note debug is either false (disabled), or a string of the debug suffix to use (enabled).
  152. // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
  153. // the same as passing an empty string.
  154. var debug = options.debug !== false;
  155. var debug_name_suffix;
  156. if (debug) {
  157. debug_name_suffix = (options.debug === true ? "" : options.debug);
  158. }
  159. var names_to_mangle = new Set();
  160. var unmangleable = new Set();
  161. var private_properties = new Set();
  162. var keep_quoted_strict = options.keep_quoted === "strict";
  163. // step 1: find candidates to mangle
  164. ast.walk(new TreeWalker(function(node) {
  165. if (
  166. node instanceof AST_ClassPrivateProperty
  167. || node instanceof AST_PrivateMethod
  168. ) {
  169. private_properties.add(node.key.name);
  170. } else if (node instanceof AST_DotHash) {
  171. private_properties.add(node.property);
  172. } else if (node instanceof AST_ObjectKeyVal) {
  173. if (typeof node.key == "string" &&
  174. (!keep_quoted_strict || !node.quote)) {
  175. add(node.key);
  176. }
  177. } else if (node instanceof AST_ObjectProperty) {
  178. // setter or getter, since KeyVal is handled above
  179. if (!keep_quoted_strict || !node.key.end.quote) {
  180. add(node.key.name);
  181. }
  182. } else if (node instanceof AST_Dot) {
  183. var declared = !!options.undeclared;
  184. if (!declared) {
  185. var root = node;
  186. while (root.expression) {
  187. root = root.expression;
  188. }
  189. declared = !(root.thedef && root.thedef.undeclared);
  190. }
  191. if (declared &&
  192. (!keep_quoted_strict || !node.quote)) {
  193. add(node.property);
  194. }
  195. } else if (node instanceof AST_Sub) {
  196. if (!keep_quoted_strict) {
  197. addStrings(node.property, add);
  198. }
  199. } else if (node instanceof AST_Call
  200. && node.expression.print_to_string() == "Object.defineProperty") {
  201. addStrings(node.args[1], add);
  202. } else if (node instanceof AST_Binary && node.operator === "in") {
  203. addStrings(node.left, add);
  204. }
  205. }));
  206. // step 2: transform the tree, renaming properties
  207. return ast.transform(new TreeTransformer(function(node) {
  208. if (
  209. node instanceof AST_ClassPrivateProperty
  210. || node instanceof AST_PrivateMethod
  211. ) {
  212. node.key.name = mangle_private(node.key.name);
  213. } else if (node instanceof AST_DotHash) {
  214. node.property = mangle_private(node.property);
  215. } else if (node instanceof AST_ObjectKeyVal) {
  216. if (typeof node.key == "string" &&
  217. (!keep_quoted_strict || !node.quote)) {
  218. node.key = mangle(node.key);
  219. }
  220. } else if (node instanceof AST_ObjectProperty) {
  221. // setter, getter, method or class field
  222. if (!keep_quoted_strict || !node.key.end.quote) {
  223. node.key.name = mangle(node.key.name);
  224. }
  225. } else if (node instanceof AST_Dot) {
  226. if (!keep_quoted_strict || !node.quote) {
  227. node.property = mangle(node.property);
  228. }
  229. } else if (!options.keep_quoted && node instanceof AST_Sub) {
  230. node.property = mangleStrings(node.property);
  231. } else if (node instanceof AST_Call
  232. && node.expression.print_to_string() == "Object.defineProperty") {
  233. node.args[1] = mangleStrings(node.args[1]);
  234. } else if (node instanceof AST_Binary && node.operator === "in") {
  235. node.left = mangleStrings(node.left);
  236. }
  237. }));
  238. // only function declarations after this line
  239. function can_mangle(name) {
  240. if (unmangleable.has(name)) return false;
  241. if (reserved.has(name)) return false;
  242. if (options.only_cache) {
  243. return cache.has(name);
  244. }
  245. if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
  246. return true;
  247. }
  248. function should_mangle(name) {
  249. if (regex && !regex.test(name)) return false;
  250. if (reserved.has(name)) return false;
  251. return cache.has(name)
  252. || names_to_mangle.has(name);
  253. }
  254. function add(name) {
  255. if (can_mangle(name))
  256. names_to_mangle.add(name);
  257. if (!should_mangle(name)) {
  258. unmangleable.add(name);
  259. }
  260. }
  261. function mangle(name) {
  262. if (!should_mangle(name)) {
  263. return name;
  264. }
  265. var mangled = cache.get(name);
  266. if (!mangled) {
  267. if (debug) {
  268. // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
  269. var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_";
  270. if (can_mangle(debug_mangled)) {
  271. mangled = debug_mangled;
  272. }
  273. }
  274. // either debug mode is off, or it is on and we could not use the mangled name
  275. if (!mangled) {
  276. do {
  277. mangled = base54(++cname);
  278. } while (!can_mangle(mangled));
  279. }
  280. cache.set(name, mangled);
  281. }
  282. return mangled;
  283. }
  284. function mangle_private(name) {
  285. let mangled = private_cache.get(name);
  286. if (!mangled) {
  287. mangled = base54(++cprivate);
  288. private_cache.set(name, mangled);
  289. }
  290. return mangled;
  291. }
  292. function mangleStrings(node) {
  293. return node.transform(new TreeTransformer(function(node) {
  294. if (node instanceof AST_Sequence) {
  295. var last = node.expressions.length - 1;
  296. node.expressions[last] = mangleStrings(node.expressions[last]);
  297. } else if (node instanceof AST_String) {
  298. node.value = mangle(node.value);
  299. } else if (node instanceof AST_Conditional) {
  300. node.consequent = mangleStrings(node.consequent);
  301. node.alternative = mangleStrings(node.alternative);
  302. }
  303. return node;
  304. }));
  305. }
  306. }
  307. export {
  308. reserve_quoted_keys,
  309. mangle_properties,
  310. };