propmangle.js 8.6KB

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