propmangle.js 7.8 KB

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