propmangle.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS
  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. var builtins = function() {
  35. var names = [];
  36. // NaN will be included due to Number.NaN
  37. [
  38. "null",
  39. "true",
  40. "false",
  41. "Infinity",
  42. "-Infinity",
  43. "undefined",
  44. ].forEach(add);
  45. [
  46. Array,
  47. Boolean,
  48. Date,
  49. Error,
  50. Function,
  51. Math,
  52. Number,
  53. Object,
  54. RegExp,
  55. String,
  56. ].forEach(function(ctor) {
  57. Object.getOwnPropertyNames(ctor).map(add);
  58. if (ctor.prototype) {
  59. Object.getOwnPropertyNames(new ctor()).map(add);
  60. Object.getOwnPropertyNames(ctor.prototype).map(add);
  61. }
  62. });
  63. return makePredicate(names);
  64. function add(name) {
  65. names.push(name);
  66. }
  67. }();
  68. function reserve_quoted_keys(ast, reserved) {
  69. ast.walk(new TreeWalker(function(node) {
  70. if (node instanceof AST_ClassProperty) {
  71. if (node.start && node.start.quote) add(node.key);
  72. } else if (node instanceof AST_ObjectProperty) {
  73. if (node.start && node.start.quote) add(node.key);
  74. } else if (node instanceof AST_Sub) {
  75. addStrings(node.property, add);
  76. }
  77. }));
  78. function add(name) {
  79. push_uniq(reserved, name);
  80. }
  81. }
  82. function addStrings(node, add) {
  83. if (node instanceof AST_Conditional) {
  84. addStrings(node.consequent, add);
  85. addStrings(node.alternative, add);
  86. } else if (node instanceof AST_Sequence) {
  87. addStrings(node.tail_node(), add);
  88. } else if (node instanceof AST_String) {
  89. add(node.value);
  90. }
  91. }
  92. function mangle_properties(ast, options) {
  93. options = defaults(options, {
  94. builtins: false,
  95. cache: null,
  96. debug: false,
  97. keep_quoted: false,
  98. regex: null,
  99. reserved: null,
  100. }, true);
  101. var reserved = Object.create(options.builtins ? null : builtins);
  102. if (Array.isArray(options.reserved)) options.reserved.forEach(function(name) {
  103. reserved[name] = true;
  104. });
  105. var cname = -1;
  106. var cache;
  107. if (options.cache) {
  108. cache = options.cache.props;
  109. cache.each(function(name) {
  110. reserved[name] = true;
  111. });
  112. } else {
  113. cache = new Dictionary();
  114. }
  115. var regex = options.regex;
  116. // note debug is either false (disabled), or a string of the debug suffix to use (enabled).
  117. // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
  118. // the same as passing an empty string.
  119. var debug = options.debug !== false;
  120. var debug_suffix;
  121. if (debug) debug_suffix = options.debug === true ? "" : options.debug;
  122. var names_to_mangle = Object.create(null);
  123. var unmangleable = Object.create(reserved);
  124. // step 1: find candidates to mangle
  125. ast.walk(new TreeWalker(function(node) {
  126. if (node instanceof AST_Binary) {
  127. if (node.operator == "in") addStrings(node.left, add);
  128. } else if (node.TYPE == "Call") {
  129. var exp = node.expression;
  130. if (exp instanceof AST_Dot) switch (exp.property) {
  131. case "defineProperty":
  132. case "getOwnPropertyDescriptor":
  133. if (node.args.length < 2) break;
  134. exp = exp.expression;
  135. if (!(exp instanceof AST_SymbolRef)) break;
  136. if (exp.name != "Object") break;
  137. if (!exp.definition().undeclared) break;
  138. addStrings(node.args[1], add);
  139. break;
  140. case "hasOwnProperty":
  141. if (node.args.length < 1) break;
  142. addStrings(node.args[0], add);
  143. break;
  144. }
  145. } else if (node instanceof AST_ClassProperty) {
  146. if (typeof node.key == "string") add(node.key);
  147. } else if (node instanceof AST_Dot) {
  148. add(node.property);
  149. } else if (node instanceof AST_ObjectProperty) {
  150. if (typeof node.key == "string") add(node.key);
  151. } else if (node instanceof AST_Sub) {
  152. addStrings(node.property, add);
  153. }
  154. }));
  155. // step 2: renaming properties
  156. ast.walk(new TreeWalker(function(node) {
  157. if (node instanceof AST_Binary) {
  158. if (node.operator == "in") mangleStrings(node.left);
  159. } else if (node.TYPE == "Call") {
  160. var exp = node.expression;
  161. if (exp instanceof AST_Dot) switch (exp.property) {
  162. case "defineProperty":
  163. case "getOwnPropertyDescriptor":
  164. if (node.args.length < 2) break;
  165. exp = exp.expression;
  166. if (!(exp instanceof AST_SymbolRef)) break;
  167. if (exp.name != "Object") break;
  168. if (!exp.definition().undeclared) break;
  169. mangleStrings(node.args[1]);
  170. break;
  171. case "hasOwnProperty":
  172. if (node.args.length < 1) break;
  173. mangleStrings(node.args[0]);
  174. break;
  175. }
  176. } else if (node instanceof AST_ClassProperty) {
  177. if (typeof node.key == "string") node.key = mangle(node.key);
  178. } else if (node instanceof AST_Dot) {
  179. node.property = mangle(node.property);
  180. } else if (node instanceof AST_ObjectProperty) {
  181. if (typeof node.key == "string") node.key = mangle(node.key);
  182. } else if (node instanceof AST_Sub) {
  183. if (!options.keep_quoted) mangleStrings(node.property);
  184. }
  185. }));
  186. // only function declarations after this line
  187. function can_mangle(name) {
  188. if (unmangleable[name]) return false;
  189. if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
  190. return true;
  191. }
  192. function should_mangle(name) {
  193. if (reserved[name]) return false;
  194. if (regex && !regex.test(name)) return false;
  195. return cache.has(name) || names_to_mangle[name];
  196. }
  197. function add(name) {
  198. if (can_mangle(name)) names_to_mangle[name] = true;
  199. if (!should_mangle(name)) unmangleable[name] = true;
  200. }
  201. function mangle(name) {
  202. if (!should_mangle(name)) return name;
  203. var mangled = cache.get(name);
  204. if (!mangled) {
  205. if (debug) {
  206. // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo ---> o._$foo$NNN_.
  207. var debug_mangled = "_$" + name + "$" + debug_suffix + "_";
  208. if (can_mangle(debug_mangled)) mangled = debug_mangled;
  209. }
  210. // either debug mode is off, or it is on and we could not use the mangled name
  211. if (!mangled) do {
  212. mangled = base54(++cname);
  213. } while (!can_mangle(mangled));
  214. if (/^#/.test(name)) mangled = "#" + mangled;
  215. cache.set(name, mangled);
  216. }
  217. return mangled;
  218. }
  219. function mangleStrings(node) {
  220. if (node instanceof AST_Sequence) {
  221. mangleStrings(node.expressions.tail_node());
  222. } else if (node instanceof AST_String) {
  223. node.value = mangle(node.value);
  224. } else if (node instanceof AST_Conditional) {
  225. mangleStrings(node.consequent);
  226. mangleStrings(node.alternative);
  227. }
  228. }
  229. }