propmangle.js 8.7 KB

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