propmangle.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. var a = [];
  36. [ Object, Array, Function, Number,
  37. String, Boolean, Error, Math,
  38. Date, RegExp
  39. ].forEach(function(ctor){
  40. Object.getOwnPropertyNames(ctor).map(add);
  41. if (ctor.prototype) {
  42. Object.getOwnPropertyNames(ctor.prototype).map(add);
  43. }
  44. });
  45. function add(name) {
  46. push_uniq(a, name);
  47. }
  48. return a;
  49. }
  50. function mangle_properties(ast, options) {
  51. options = defaults(options, {
  52. reserved : null,
  53. cache : null,
  54. only_cache : false,
  55. regex : null
  56. });
  57. var reserved = options.reserved;
  58. if (reserved == null)
  59. reserved = find_builtins();
  60. var cache = options.cache;
  61. if (cache == null) {
  62. cache = {
  63. cname: -1,
  64. props: new Dictionary()
  65. };
  66. }
  67. var regex = options.regex;
  68. var names_to_mangle = [];
  69. // step 1: find candidates to mangle
  70. ast.walk(new TreeWalker(function(node){
  71. if (node instanceof AST_ObjectKeyVal) {
  72. add(node.key);
  73. }
  74. else if (node instanceof AST_ObjectProperty) {
  75. // setter or getter, since KeyVal is handled above
  76. add(node.key.name);
  77. }
  78. else if (node instanceof AST_Dot) {
  79. if (this.parent() instanceof AST_Assign) {
  80. add(node.property);
  81. }
  82. }
  83. else if (node instanceof AST_Sub) {
  84. if (this.parent() instanceof AST_Assign) {
  85. addStrings(node.property);
  86. }
  87. }
  88. }));
  89. // step 2: transform the tree, renaming properties
  90. return ast.transform(new TreeTransformer(function(node){
  91. if (node instanceof AST_ObjectKeyVal) {
  92. if (should_mangle(node.key)) {
  93. node.key = mangle(node.key);
  94. }
  95. }
  96. else if (node instanceof AST_ObjectProperty) {
  97. // setter or getter
  98. if (should_mangle(node.key.name)) {
  99. node.key.name = mangle(node.key.name);
  100. }
  101. }
  102. else if (node instanceof AST_Dot) {
  103. if (should_mangle(node.property)) {
  104. node.property = mangle(node.property);
  105. }
  106. }
  107. else if (node instanceof AST_Sub) {
  108. node.property = mangleStrings(node.property);
  109. }
  110. // else if (node instanceof AST_String) {
  111. // if (should_mangle(node.value)) {
  112. // AST_Node.warn(
  113. // "Found \"{prop}\" property candidate for mangling in an arbitrary string [{file}:{line},{col}]", {
  114. // file : node.start.file,
  115. // line : node.start.line,
  116. // col : node.start.col,
  117. // prop : node.value
  118. // }
  119. // );
  120. // }
  121. // }
  122. }));
  123. // only function declarations after this line
  124. function can_mangle(name) {
  125. if (reserved.indexOf(name) >= 0) return false;
  126. if (options.only_cache) {
  127. return cache.props.has(name);
  128. }
  129. if (/^[0-9.]+$/.test(name)) return false;
  130. return true;
  131. }
  132. function should_mangle(name) {
  133. if (regex && !regex.test(name)) return false;
  134. if (reserved.indexOf(name) >= 0) return false;
  135. return cache.props.has(name)
  136. || names_to_mangle.indexOf(name) >= 0;
  137. }
  138. function add(name) {
  139. if (can_mangle(name))
  140. push_uniq(names_to_mangle, name);
  141. }
  142. function mangle(name) {
  143. var mangled = cache.props.get(name);
  144. if (!mangled) {
  145. do {
  146. mangled = base54(++cache.cname);
  147. } while (!can_mangle(mangled));
  148. cache.props.set(name, mangled);
  149. }
  150. return mangled;
  151. }
  152. function addStrings(node) {
  153. var out = {};
  154. try {
  155. (function walk(node){
  156. node.walk(new TreeWalker(function(node){
  157. if (node instanceof AST_Seq) {
  158. walk(node.cdr);
  159. return true;
  160. }
  161. if (node instanceof AST_String) {
  162. add(node.value);
  163. return true;
  164. }
  165. if (node instanceof AST_Conditional) {
  166. walk(node.consequent);
  167. walk(node.alternative);
  168. return true;
  169. }
  170. throw out;
  171. }));
  172. })(node);
  173. } catch(ex) {
  174. if (ex !== out) throw ex;
  175. }
  176. }
  177. function mangleStrings(node) {
  178. return node.transform(new TreeTransformer(function(node){
  179. if (node instanceof AST_Seq) {
  180. node.cdr = mangleStrings(node.cdr);
  181. }
  182. else if (node instanceof AST_String) {
  183. if (should_mangle(node.value)) {
  184. node.value = mangle(node.value);
  185. }
  186. }
  187. else if (node instanceof AST_Conditional) {
  188. node.consequent = mangleStrings(node.consequent);
  189. node.alternative = mangleStrings(node.alternative);
  190. }
  191. return node;
  192. }));
  193. }
  194. }