holmes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/README.md +10 -0
  4. data/Rakefile +1 -0
  5. data/holmes.gemspec +24 -0
  6. data/lib/holmes.rb +38 -0
  7. data/lib/holmes/node_modules/detective/README.markdown +64 -0
  8. data/lib/holmes/node_modules/detective/example/strings.js +6 -0
  9. data/lib/holmes/node_modules/detective/example/strings_src.js +3 -0
  10. data/lib/holmes/node_modules/detective/index.js +65 -0
  11. data/lib/holmes/node_modules/detective/node_modules/burrito/.npmignore +1 -0
  12. data/lib/holmes/node_modules/detective/node_modules/burrito/README.markdown +185 -0
  13. data/lib/holmes/node_modules/detective/node_modules/burrito/browserify.js +4296 -0
  14. data/lib/holmes/node_modules/detective/node_modules/burrito/example/microwave.js +7 -0
  15. data/lib/holmes/node_modules/detective/node_modules/burrito/example/web/bs.js +4832 -0
  16. data/lib/holmes/node_modules/detective/node_modules/burrito/example/web/index.html +14 -0
  17. data/lib/holmes/node_modules/detective/node_modules/burrito/example/web/main.js +17 -0
  18. data/lib/holmes/node_modules/detective/node_modules/burrito/example/web/server.js +12 -0
  19. data/lib/holmes/node_modules/detective/node_modules/burrito/example/wrap.js +7 -0
  20. data/lib/holmes/node_modules/detective/node_modules/burrito/index.html +8 -0
  21. data/lib/holmes/node_modules/detective/node_modules/burrito/index.js +208 -0
  22. data/lib/holmes/node_modules/detective/node_modules/burrito/main.js +11 -0
  23. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/.bin/uglifyjs +317 -0
  24. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/.npmignore +1 -0
  25. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/LICENSE +24 -0
  26. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/README.markdown +237 -0
  27. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/examples/json.js +16 -0
  28. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/examples/leaves.js +15 -0
  29. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/examples/negative.js +8 -0
  30. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/examples/scrub.js +10 -0
  31. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/examples/stringify.js +38 -0
  32. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/index.js +267 -0
  33. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/main.js +10 -0
  34. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/package.json +18 -0
  35. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/circular.js +115 -0
  36. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/date.js +35 -0
  37. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/equal.js +220 -0
  38. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/instance.js +17 -0
  39. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/interface.js +42 -0
  40. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/json.js +47 -0
  41. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/keys.js +29 -0
  42. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/leaves.js +21 -0
  43. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/lib/deep_equal.js +92 -0
  44. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/mutability.js +252 -0
  45. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/negative.js +20 -0
  46. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/obj.js +15 -0
  47. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/siblings.js +35 -0
  48. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/stop.js +41 -0
  49. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/stringify.js +36 -0
  50. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/subexpr.js +34 -0
  51. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/traverse/test/super_deep.js +55 -0
  52. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/.npmignore +4 -0
  53. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/README.html +888 -0
  54. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/README.org +463 -0
  55. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/bin/uglifyjs +317 -0
  56. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/docstyle.css +75 -0
  57. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/lib/object-ast.js +75 -0
  58. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/lib/parse-js.js +1341 -0
  59. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/lib/process.js +1949 -0
  60. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/lib/squeeze-more.js +51 -0
  61. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/package.json +22 -0
  62. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/beautify.js +28 -0
  63. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/testparser.js +402 -0
  64. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/array1.js +1 -0
  65. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/array2.js +1 -0
  66. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/array3.js +1 -0
  67. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/array4.js +1 -0
  68. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/assignment.js +1 -0
  69. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/concatstring.js +1 -0
  70. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/const.js +1 -0
  71. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/empty-blocks.js +1 -0
  72. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/forstatement.js +1 -0
  73. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/if.js +1 -0
  74. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/ifreturn.js +1 -0
  75. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/ifreturn2.js +1 -0
  76. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue10.js +1 -0
  77. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue11.js +1 -0
  78. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue13.js +1 -0
  79. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue14.js +1 -0
  80. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue16.js +1 -0
  81. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue17.js +1 -0
  82. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue20.js +1 -0
  83. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue21.js +1 -0
  84. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue25.js +1 -0
  85. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue27.js +1 -0
  86. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue28.js +1 -0
  87. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue29.js +1 -0
  88. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue30.js +1 -0
  89. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue34.js +1 -0
  90. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue4.js +1 -0
  91. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue48.js +1 -0
  92. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue50.js +1 -0
  93. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue53.js +1 -0
  94. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue54.1.js +1 -0
  95. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue68.js +1 -0
  96. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue69.js +1 -0
  97. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/issue9.js +1 -0
  98. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/mangle.js +1 -0
  99. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/strict-equals.js +1 -0
  100. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/var.js +1 -0
  101. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/expected/with.js +1 -0
  102. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/array1.js +3 -0
  103. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/array2.js +4 -0
  104. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/array3.js +4 -0
  105. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/array4.js +6 -0
  106. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/assignment.js +20 -0
  107. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/concatstring.js +3 -0
  108. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/const.js +5 -0
  109. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/empty-blocks.js +4 -0
  110. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/forstatement.js +10 -0
  111. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/if.js +6 -0
  112. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/ifreturn.js +9 -0
  113. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/ifreturn2.js +16 -0
  114. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue10.js +1 -0
  115. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue11.js +3 -0
  116. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue13.js +1 -0
  117. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue14.js +1 -0
  118. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue16.js +1 -0
  119. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue17.js +4 -0
  120. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue20.js +1 -0
  121. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue21.js +6 -0
  122. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue25.js +7 -0
  123. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue27.js +1 -0
  124. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue28.js +3 -0
  125. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue29.js +1 -0
  126. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue30.js +3 -0
  127. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue34.js +3 -0
  128. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue4.js +3 -0
  129. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue48.js +1 -0
  130. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue50.js +9 -0
  131. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue53.js +1 -0
  132. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue54.1.js +3 -0
  133. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue68.js +5 -0
  134. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue69.js +1 -0
  135. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/issue9.js +4 -0
  136. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/mangle.js +5 -0
  137. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/strict-equals.js +3 -0
  138. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/var.js +3 -0
  139. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/compress/test/with.js +2 -0
  140. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/test/unit/scripts.js +55 -0
  141. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/tmp/hoist.js +33 -0
  142. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/tmp/instrument.js +97 -0
  143. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/tmp/instrument2.js +138 -0
  144. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/tmp/test.js +16 -0
  145. data/lib/holmes/node_modules/detective/node_modules/burrito/node_modules/uglify-js/uglify-js.js +17 -0
  146. data/lib/holmes/node_modules/detective/node_modules/burrito/package.json +43 -0
  147. data/lib/holmes/node_modules/detective/node_modules/burrito/server.js +4 -0
  148. data/lib/holmes/node_modules/detective/node_modules/burrito/test/ast.js +29 -0
  149. data/lib/holmes/node_modules/detective/node_modules/burrito/test/err.js +47 -0
  150. data/lib/holmes/node_modules/detective/node_modules/burrito/test/fail.js +8 -0
  151. data/lib/holmes/node_modules/detective/node_modules/burrito/test/fail/src.js +60 -0
  152. data/lib/holmes/node_modules/detective/node_modules/burrito/test/label.js +110 -0
  153. data/lib/holmes/node_modules/detective/node_modules/burrito/test/microwave.js +33 -0
  154. data/lib/holmes/node_modules/detective/node_modules/burrito/test/parent.js +26 -0
  155. data/lib/holmes/node_modules/detective/node_modules/burrito/test/wrap.js +157 -0
  156. data/lib/holmes/node_modules/detective/package.json +39 -0
  157. data/lib/holmes/node_modules/detective/test/both.js +10 -0
  158. data/lib/holmes/node_modules/detective/test/files/both.js +4 -0
  159. data/lib/holmes/node_modules/detective/test/files/nested.js +22 -0
  160. data/lib/holmes/node_modules/detective/test/files/strings.js +13 -0
  161. data/lib/holmes/node_modules/detective/test/files/word.js +13 -0
  162. data/lib/holmes/node_modules/detective/test/nested.js +8 -0
  163. data/lib/holmes/node_modules/detective/test/strings.js +8 -0
  164. data/lib/holmes/node_modules/detective/test/word.js +11 -0
  165. data/lib/holmes/require.js +5 -0
  166. data/lib/holmes/runner.js +20 -0
  167. data/lib/holmes/version.rb +3 -0
  168. metadata +223 -0
@@ -0,0 +1,1949 @@
1
+ /***********************************************************************
2
+
3
+ A JavaScript tokenizer / parser / beautifier / compressor.
4
+
5
+ This version is suitable for Node.js. With minimal changes (the
6
+ exports stuff) it should work on any JS platform.
7
+
8
+ This file implements some AST processors. They work on data built
9
+ by parse-js.
10
+
11
+ Exported functions:
12
+
13
+ - ast_mangle(ast, options) -- mangles the variable/function names
14
+ in the AST. Returns an AST.
15
+
16
+ - ast_squeeze(ast) -- employs various optimizations to make the
17
+ final generated code even smaller. Returns an AST.
18
+
19
+ - gen_code(ast, options) -- generates JS code from the AST. Pass
20
+ true (or an object, see the code for some options) as second
21
+ argument to get "pretty" (indented) code.
22
+
23
+ -------------------------------- (C) ---------------------------------
24
+
25
+ Author: Mihai Bazon
26
+ <mihai.bazon@gmail.com>
27
+ http://mihai.bazon.net/blog
28
+
29
+ Distributed under the BSD license:
30
+
31
+ Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
32
+
33
+ Redistribution and use in source and binary forms, with or without
34
+ modification, are permitted provided that the following conditions
35
+ are met:
36
+
37
+ * Redistributions of source code must retain the above
38
+ copyright notice, this list of conditions and the following
39
+ disclaimer.
40
+
41
+ * Redistributions in binary form must reproduce the above
42
+ copyright notice, this list of conditions and the following
43
+ disclaimer in the documentation and/or other materials
44
+ provided with the distribution.
45
+
46
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
47
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
50
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
51
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
52
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
53
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
55
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
56
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57
+ SUCH DAMAGE.
58
+
59
+ ***********************************************************************/
60
+
61
+ var jsp = require("./parse-js"),
62
+ slice = jsp.slice,
63
+ member = jsp.member,
64
+ PRECEDENCE = jsp.PRECEDENCE,
65
+ OPERATORS = jsp.OPERATORS;
66
+
67
+ /* -----[ helper for AST traversal ]----- */
68
+
69
+ function ast_walker() {
70
+ function _vardefs(defs) {
71
+ return [ this[0], MAP(defs, function(def){
72
+ var a = [ def[0] ];
73
+ if (def.length > 1)
74
+ a[1] = walk(def[1]);
75
+ return a;
76
+ }) ];
77
+ };
78
+ function _block(statements) {
79
+ var out = [ this[0] ];
80
+ if (statements != null)
81
+ out.push(MAP(statements, walk));
82
+ return out;
83
+ };
84
+ var walkers = {
85
+ "string": function(str) {
86
+ return [ this[0], str ];
87
+ },
88
+ "num": function(num) {
89
+ return [ this[0], num ];
90
+ },
91
+ "name": function(name) {
92
+ return [ this[0], name ];
93
+ },
94
+ "toplevel": function(statements) {
95
+ return [ this[0], MAP(statements, walk) ];
96
+ },
97
+ "block": _block,
98
+ "splice": _block,
99
+ "var": _vardefs,
100
+ "const": _vardefs,
101
+ "try": function(t, c, f) {
102
+ return [
103
+ this[0],
104
+ MAP(t, walk),
105
+ c != null ? [ c[0], MAP(c[1], walk) ] : null,
106
+ f != null ? MAP(f, walk) : null
107
+ ];
108
+ },
109
+ "throw": function(expr) {
110
+ return [ this[0], walk(expr) ];
111
+ },
112
+ "new": function(ctor, args) {
113
+ return [ this[0], walk(ctor), MAP(args, walk) ];
114
+ },
115
+ "switch": function(expr, body) {
116
+ return [ this[0], walk(expr), MAP(body, function(branch){
117
+ return [ branch[0] ? walk(branch[0]) : null,
118
+ MAP(branch[1], walk) ];
119
+ }) ];
120
+ },
121
+ "break": function(label) {
122
+ return [ this[0], label ];
123
+ },
124
+ "continue": function(label) {
125
+ return [ this[0], label ];
126
+ },
127
+ "conditional": function(cond, t, e) {
128
+ return [ this[0], walk(cond), walk(t), walk(e) ];
129
+ },
130
+ "assign": function(op, lvalue, rvalue) {
131
+ return [ this[0], op, walk(lvalue), walk(rvalue) ];
132
+ },
133
+ "dot": function(expr) {
134
+ return [ this[0], walk(expr) ].concat(slice(arguments, 1));
135
+ },
136
+ "call": function(expr, args) {
137
+ return [ this[0], walk(expr), MAP(args, walk) ];
138
+ },
139
+ "function": function(name, args, body) {
140
+ return [ this[0], name, args.slice(), MAP(body, walk) ];
141
+ },
142
+ "defun": function(name, args, body) {
143
+ return [ this[0], name, args.slice(), MAP(body, walk) ];
144
+ },
145
+ "if": function(conditional, t, e) {
146
+ return [ this[0], walk(conditional), walk(t), walk(e) ];
147
+ },
148
+ "for": function(init, cond, step, block) {
149
+ return [ this[0], walk(init), walk(cond), walk(step), walk(block) ];
150
+ },
151
+ "for-in": function(vvar, key, hash, block) {
152
+ return [ this[0], walk(vvar), walk(key), walk(hash), walk(block) ];
153
+ },
154
+ "while": function(cond, block) {
155
+ return [ this[0], walk(cond), walk(block) ];
156
+ },
157
+ "do": function(cond, block) {
158
+ return [ this[0], walk(cond), walk(block) ];
159
+ },
160
+ "return": function(expr) {
161
+ return [ this[0], walk(expr) ];
162
+ },
163
+ "binary": function(op, left, right) {
164
+ return [ this[0], op, walk(left), walk(right) ];
165
+ },
166
+ "unary-prefix": function(op, expr) {
167
+ return [ this[0], op, walk(expr) ];
168
+ },
169
+ "unary-postfix": function(op, expr) {
170
+ return [ this[0], op, walk(expr) ];
171
+ },
172
+ "sub": function(expr, subscript) {
173
+ return [ this[0], walk(expr), walk(subscript) ];
174
+ },
175
+ "object": function(props) {
176
+ return [ this[0], MAP(props, function(p){
177
+ return p.length == 2
178
+ ? [ p[0], walk(p[1]) ]
179
+ : [ p[0], walk(p[1]), p[2] ]; // get/set-ter
180
+ }) ];
181
+ },
182
+ "regexp": function(rx, mods) {
183
+ return [ this[0], rx, mods ];
184
+ },
185
+ "array": function(elements) {
186
+ return [ this[0], MAP(elements, walk) ];
187
+ },
188
+ "stat": function(stat) {
189
+ return [ this[0], walk(stat) ];
190
+ },
191
+ "seq": function() {
192
+ return [ this[0] ].concat(MAP(slice(arguments), walk));
193
+ },
194
+ "label": function(name, block) {
195
+ return [ this[0], name, walk(block) ];
196
+ },
197
+ "with": function(expr, block) {
198
+ return [ this[0], walk(expr), walk(block) ];
199
+ },
200
+ "atom": function(name) {
201
+ return [ this[0], name ];
202
+ }
203
+ };
204
+
205
+ var user = {};
206
+ var stack = [];
207
+ function walk(ast) {
208
+ if (ast == null)
209
+ return null;
210
+ try {
211
+ stack.push(ast);
212
+ var type = ast[0];
213
+ var gen = user[type];
214
+ if (gen) {
215
+ var ret = gen.apply(ast, ast.slice(1));
216
+ if (ret != null)
217
+ return ret;
218
+ }
219
+ gen = walkers[type];
220
+ return gen.apply(ast, ast.slice(1));
221
+ } finally {
222
+ stack.pop();
223
+ }
224
+ };
225
+
226
+ function with_walkers(walkers, cont){
227
+ var save = {}, i;
228
+ for (i in walkers) if (HOP(walkers, i)) {
229
+ save[i] = user[i];
230
+ user[i] = walkers[i];
231
+ }
232
+ var ret = cont();
233
+ for (i in save) if (HOP(save, i)) {
234
+ if (!save[i]) delete user[i];
235
+ else user[i] = save[i];
236
+ }
237
+ return ret;
238
+ };
239
+
240
+ return {
241
+ walk: walk,
242
+ with_walkers: with_walkers,
243
+ parent: function() {
244
+ return stack[stack.length - 2]; // last one is current node
245
+ },
246
+ stack: function() {
247
+ return stack;
248
+ }
249
+ };
250
+ };
251
+
252
+ /* -----[ Scope and mangling ]----- */
253
+
254
+ function Scope(parent) {
255
+ this.names = {}; // names defined in this scope
256
+ this.mangled = {}; // mangled names (orig.name => mangled)
257
+ this.rev_mangled = {}; // reverse lookup (mangled => orig.name)
258
+ this.cname = -1; // current mangled name
259
+ this.refs = {}; // names referenced from this scope
260
+ this.uses_with = false; // will become TRUE if with() is detected in this or any subscopes
261
+ this.uses_eval = false; // will become TRUE if eval() is detected in this or any subscopes
262
+ this.parent = parent; // parent scope
263
+ this.children = []; // sub-scopes
264
+ if (parent) {
265
+ this.level = parent.level + 1;
266
+ parent.children.push(this);
267
+ } else {
268
+ this.level = 0;
269
+ }
270
+ };
271
+
272
+ var base54 = (function(){
273
+ var DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
274
+ return function(num) {
275
+ var ret = "";
276
+ do {
277
+ ret = DIGITS.charAt(num % 54) + ret;
278
+ num = Math.floor(num / 54);
279
+ } while (num > 0);
280
+ return ret;
281
+ };
282
+ })();
283
+
284
+ Scope.prototype = {
285
+ has: function(name) {
286
+ for (var s = this; s; s = s.parent)
287
+ if (HOP(s.names, name))
288
+ return s;
289
+ },
290
+ has_mangled: function(mname) {
291
+ for (var s = this; s; s = s.parent)
292
+ if (HOP(s.rev_mangled, mname))
293
+ return s;
294
+ },
295
+ toJSON: function() {
296
+ return {
297
+ names: this.names,
298
+ uses_eval: this.uses_eval,
299
+ uses_with: this.uses_with
300
+ };
301
+ },
302
+
303
+ next_mangled: function() {
304
+ // we must be careful that the new mangled name:
305
+ //
306
+ // 1. doesn't shadow a mangled name from a parent
307
+ // scope, unless we don't reference the original
308
+ // name from this scope OR from any sub-scopes!
309
+ // This will get slow.
310
+ //
311
+ // 2. doesn't shadow an original name from a parent
312
+ // scope, in the event that the name is not mangled
313
+ // in the parent scope and we reference that name
314
+ // here OR IN ANY SUBSCOPES!
315
+ //
316
+ // 3. doesn't shadow a name that is referenced but not
317
+ // defined (possibly global defined elsewhere).
318
+ for (;;) {
319
+ var m = base54(++this.cname), prior;
320
+
321
+ // case 1.
322
+ prior = this.has_mangled(m);
323
+ if (prior && this.refs[prior.rev_mangled[m]] === prior)
324
+ continue;
325
+
326
+ // case 2.
327
+ prior = this.has(m);
328
+ if (prior && prior !== this && this.refs[m] === prior && !prior.has_mangled(m))
329
+ continue;
330
+
331
+ // case 3.
332
+ if (HOP(this.refs, m) && this.refs[m] == null)
333
+ continue;
334
+
335
+ // I got "do" once. :-/
336
+ if (!is_identifier(m))
337
+ continue;
338
+
339
+ return m;
340
+ }
341
+ },
342
+ set_mangle: function(name, m) {
343
+ this.rev_mangled[m] = name;
344
+ return this.mangled[name] = m;
345
+ },
346
+ get_mangled: function(name, newMangle) {
347
+ if (this.uses_eval || this.uses_with) return name; // no mangle if eval or with is in use
348
+ var s = this.has(name);
349
+ if (!s) return name; // not in visible scope, no mangle
350
+ if (HOP(s.mangled, name)) return s.mangled[name]; // already mangled in this scope
351
+ if (!newMangle) return name; // not found and no mangling requested
352
+ return s.set_mangle(name, s.next_mangled());
353
+ },
354
+ references: function(name) {
355
+ return name && !this.parent || this.uses_with || this.uses_eval || this.refs[name];
356
+ },
357
+ define: function(name, type) {
358
+ if (name != null) {
359
+ if (type == "var" || !HOP(this.names, name))
360
+ this.names[name] = type || "var";
361
+ return name;
362
+ }
363
+ }
364
+ };
365
+
366
+ function ast_add_scope(ast) {
367
+
368
+ var current_scope = null;
369
+ var w = ast_walker(), walk = w.walk;
370
+ var having_eval = [];
371
+
372
+ function with_new_scope(cont) {
373
+ current_scope = new Scope(current_scope);
374
+ var ret = current_scope.body = cont();
375
+ ret.scope = current_scope;
376
+ current_scope = current_scope.parent;
377
+ return ret;
378
+ };
379
+
380
+ function define(name, type) {
381
+ return current_scope.define(name, type);
382
+ };
383
+
384
+ function reference(name) {
385
+ current_scope.refs[name] = true;
386
+ };
387
+
388
+ function _lambda(name, args, body) {
389
+ var is_defun = this[0] == "defun";
390
+ return [ this[0], is_defun ? define(name, "defun") : name, args, with_new_scope(function(){
391
+ if (!is_defun) define(name, "lambda");
392
+ MAP(args, function(name){ define(name, "arg") });
393
+ return MAP(body, walk);
394
+ })];
395
+ };
396
+
397
+ function _vardefs(type) {
398
+ return function(defs) {
399
+ MAP(defs, function(d){
400
+ define(d[0], type);
401
+ if (d[1]) reference(d[0]);
402
+ });
403
+ };
404
+ };
405
+
406
+ return with_new_scope(function(){
407
+ // process AST
408
+ var ret = w.with_walkers({
409
+ "function": _lambda,
410
+ "defun": _lambda,
411
+ "label": function(name, stat) { define(name, "label") },
412
+ "break": function(label) { if (label) reference(label) },
413
+ "continue": function(label) { if (label) reference(label) },
414
+ "with": function(expr, block) {
415
+ for (var s = current_scope; s; s = s.parent)
416
+ s.uses_with = true;
417
+ },
418
+ "var": _vardefs("var"),
419
+ "const": _vardefs("const"),
420
+ "try": function(t, c, f) {
421
+ if (c != null) return [
422
+ this[0],
423
+ MAP(t, walk),
424
+ [ define(c[0], "catch"), MAP(c[1], walk) ],
425
+ f != null ? MAP(f, walk) : null
426
+ ];
427
+ },
428
+ "name": function(name) {
429
+ if (name == "eval")
430
+ having_eval.push(current_scope);
431
+ reference(name);
432
+ }
433
+ }, function(){
434
+ return walk(ast);
435
+ });
436
+
437
+ // the reason why we need an additional pass here is
438
+ // that names can be used prior to their definition.
439
+
440
+ // scopes where eval was detected and their parents
441
+ // are marked with uses_eval, unless they define the
442
+ // "eval" name.
443
+ MAP(having_eval, function(scope){
444
+ if (!scope.has("eval")) while (scope) {
445
+ scope.uses_eval = true;
446
+ scope = scope.parent;
447
+ }
448
+ });
449
+
450
+ // for referenced names it might be useful to know
451
+ // their origin scope. current_scope here is the
452
+ // toplevel one.
453
+ function fixrefs(scope, i) {
454
+ // do children first; order shouldn't matter
455
+ for (i = scope.children.length; --i >= 0;)
456
+ fixrefs(scope.children[i]);
457
+ for (i in scope.refs) if (HOP(scope.refs, i)) {
458
+ // find origin scope and propagate the reference to origin
459
+ for (var origin = scope.has(i), s = scope; s; s = s.parent) {
460
+ s.refs[i] = origin;
461
+ if (s === origin) break;
462
+ }
463
+ }
464
+ };
465
+ fixrefs(current_scope);
466
+
467
+ return ret;
468
+ });
469
+
470
+ };
471
+
472
+ /* -----[ mangle names ]----- */
473
+
474
+ function ast_mangle(ast, options) {
475
+ var w = ast_walker(), walk = w.walk, scope;
476
+ options = options || {};
477
+
478
+ function get_mangled(name, newMangle) {
479
+ if (!options.toplevel && !scope.parent) return name; // don't mangle toplevel
480
+ if (options.except && member(name, options.except))
481
+ return name;
482
+ return scope.get_mangled(name, newMangle);
483
+ };
484
+
485
+ function get_define(name) {
486
+ if (options.defines) {
487
+ // we always lookup a defined symbol for the current scope FIRST, so declared
488
+ // vars trump a DEFINE symbol, but if no such var is found, then match a DEFINE value
489
+ if (!scope.has(name)) {
490
+ if (HOP(options.defines, name)) {
491
+ return options.defines[name];
492
+ }
493
+ }
494
+ return null;
495
+ }
496
+ };
497
+
498
+ function _lambda(name, args, body) {
499
+ var is_defun = this[0] == "defun", extra;
500
+ if (name) {
501
+ if (is_defun) name = get_mangled(name);
502
+ else {
503
+ extra = {};
504
+ if (!(scope.uses_eval || scope.uses_with))
505
+ name = extra[name] = scope.next_mangled();
506
+ else
507
+ extra[name] = name;
508
+ }
509
+ }
510
+ body = with_scope(body.scope, function(){
511
+ args = MAP(args, function(name){ return get_mangled(name) });
512
+ return MAP(body, walk);
513
+ }, extra);
514
+ return [ this[0], name, args, body ];
515
+ };
516
+
517
+ function with_scope(s, cont, extra) {
518
+ var _scope = scope;
519
+ scope = s;
520
+ if (extra) for (var i in extra) if (HOP(extra, i)) {
521
+ s.set_mangle(i, extra[i]);
522
+ }
523
+ for (var i in s.names) if (HOP(s.names, i)) {
524
+ get_mangled(i, true);
525
+ }
526
+ var ret = cont();
527
+ ret.scope = s;
528
+ scope = _scope;
529
+ return ret;
530
+ };
531
+
532
+ function _vardefs(defs) {
533
+ return [ this[0], MAP(defs, function(d){
534
+ return [ get_mangled(d[0]), walk(d[1]) ];
535
+ }) ];
536
+ };
537
+
538
+ return w.with_walkers({
539
+ "function": _lambda,
540
+ "defun": function() {
541
+ // move function declarations to the top when
542
+ // they are not in some block.
543
+ var ast = _lambda.apply(this, arguments);
544
+ switch (w.parent()[0]) {
545
+ case "toplevel":
546
+ case "function":
547
+ case "defun":
548
+ return MAP.at_top(ast);
549
+ }
550
+ return ast;
551
+ },
552
+ "label": function(label, stat) { return [ this[0], get_mangled(label), walk(stat) ] },
553
+ "break": function(label) { if (label) return [ this[0], get_mangled(label) ] },
554
+ "continue": function(label) { if (label) return [ this[0], get_mangled(label) ] },
555
+ "var": _vardefs,
556
+ "const": _vardefs,
557
+ "name": function(name) {
558
+ return get_define(name) || [ this[0], get_mangled(name) ];
559
+ },
560
+ "try": function(t, c, f) {
561
+ return [ this[0],
562
+ MAP(t, walk),
563
+ c != null ? [ get_mangled(c[0]), MAP(c[1], walk) ] : null,
564
+ f != null ? MAP(f, walk) : null ];
565
+ },
566
+ "toplevel": function(body) {
567
+ var self = this;
568
+ return with_scope(self.scope, function(){
569
+ return [ self[0], MAP(body, walk) ];
570
+ });
571
+ }
572
+ }, function() {
573
+ return walk(ast_add_scope(ast));
574
+ });
575
+ };
576
+
577
+ /* -----[
578
+ - compress foo["bar"] into foo.bar,
579
+ - remove block brackets {} where possible
580
+ - join consecutive var declarations
581
+ - various optimizations for IFs:
582
+ - if (cond) foo(); else bar(); ==> cond?foo():bar();
583
+ - if (cond) foo(); ==> cond&&foo();
584
+ - if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); // also for throw
585
+ - if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
586
+ ]----- */
587
+
588
+ var warn = function(){};
589
+
590
+ function best_of(ast1, ast2) {
591
+ return gen_code(ast1).length > gen_code(ast2[0] == "stat" ? ast2[1] : ast2).length ? ast2 : ast1;
592
+ };
593
+
594
+ function last_stat(b) {
595
+ if (b[0] == "block" && b[1] && b[1].length > 0)
596
+ return b[1][b[1].length - 1];
597
+ return b;
598
+ }
599
+
600
+ function aborts(t) {
601
+ if (t) switch (last_stat(t)[0]) {
602
+ case "return":
603
+ case "break":
604
+ case "continue":
605
+ case "throw":
606
+ return true;
607
+ }
608
+ };
609
+
610
+ function boolean_expr(expr) {
611
+ return ( (expr[0] == "unary-prefix"
612
+ && member(expr[1], [ "!", "delete" ])) ||
613
+
614
+ (expr[0] == "binary"
615
+ && member(expr[1], [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ])) ||
616
+
617
+ (expr[0] == "binary"
618
+ && member(expr[1], [ "&&", "||" ])
619
+ && boolean_expr(expr[2])
620
+ && boolean_expr(expr[3])) ||
621
+
622
+ (expr[0] == "conditional"
623
+ && boolean_expr(expr[2])
624
+ && boolean_expr(expr[3])) ||
625
+
626
+ (expr[0] == "assign"
627
+ && expr[1] === true
628
+ && boolean_expr(expr[3])) ||
629
+
630
+ (expr[0] == "seq"
631
+ && boolean_expr(expr[expr.length - 1]))
632
+ );
633
+ };
634
+
635
+ function make_conditional(c, t, e) {
636
+ var make_real_conditional = function() {
637
+ if (c[0] == "unary-prefix" && c[1] == "!") {
638
+ return e ? [ "conditional", c[2], e, t ] : [ "binary", "||", c[2], t ];
639
+ } else {
640
+ return e ? [ "conditional", c, t, e ] : [ "binary", "&&", c, t ];
641
+ }
642
+ };
643
+ // shortcut the conditional if the expression has a constant value
644
+ return when_constant(c, function(ast, val){
645
+ warn_unreachable(val ? e : t);
646
+ return (val ? t : e);
647
+ }, make_real_conditional);
648
+ };
649
+
650
+ function empty(b) {
651
+ return !b || (b[0] == "block" && (!b[1] || b[1].length == 0));
652
+ };
653
+
654
+ function is_string(node) {
655
+ return (node[0] == "string" ||
656
+ node[0] == "unary-prefix" && node[1] == "typeof" ||
657
+ node[0] == "binary" && node[1] == "+" &&
658
+ (is_string(node[2]) || is_string(node[3])));
659
+ };
660
+
661
+ var when_constant = (function(){
662
+
663
+ var $NOT_CONSTANT = {};
664
+
665
+ // this can only evaluate constant expressions. If it finds anything
666
+ // not constant, it throws $NOT_CONSTANT.
667
+ function evaluate(expr) {
668
+ switch (expr[0]) {
669
+ case "string":
670
+ case "num":
671
+ return expr[1];
672
+ case "name":
673
+ case "atom":
674
+ switch (expr[1]) {
675
+ case "true": return true;
676
+ case "false": return false;
677
+ }
678
+ break;
679
+ case "unary-prefix":
680
+ switch (expr[1]) {
681
+ case "!": return !evaluate(expr[2]);
682
+ case "typeof": return typeof evaluate(expr[2]);
683
+ case "~": return ~evaluate(expr[2]);
684
+ case "-": return -evaluate(expr[2]);
685
+ case "+": return +evaluate(expr[2]);
686
+ }
687
+ break;
688
+ case "binary":
689
+ var left = expr[2], right = expr[3];
690
+ switch (expr[1]) {
691
+ case "&&" : return evaluate(left) && evaluate(right);
692
+ case "||" : return evaluate(left) || evaluate(right);
693
+ case "|" : return evaluate(left) | evaluate(right);
694
+ case "&" : return evaluate(left) & evaluate(right);
695
+ case "^" : return evaluate(left) ^ evaluate(right);
696
+ case "+" : return evaluate(left) + evaluate(right);
697
+ case "*" : return evaluate(left) * evaluate(right);
698
+ case "/" : return evaluate(left) / evaluate(right);
699
+ case "-" : return evaluate(left) - evaluate(right);
700
+ case "<<" : return evaluate(left) << evaluate(right);
701
+ case ">>" : return evaluate(left) >> evaluate(right);
702
+ case ">>>" : return evaluate(left) >>> evaluate(right);
703
+ case "==" : return evaluate(left) == evaluate(right);
704
+ case "===" : return evaluate(left) === evaluate(right);
705
+ case "!=" : return evaluate(left) != evaluate(right);
706
+ case "!==" : return evaluate(left) !== evaluate(right);
707
+ case "<" : return evaluate(left) < evaluate(right);
708
+ case "<=" : return evaluate(left) <= evaluate(right);
709
+ case ">" : return evaluate(left) > evaluate(right);
710
+ case ">=" : return evaluate(left) >= evaluate(right);
711
+ case "in" : return evaluate(left) in evaluate(right);
712
+ case "instanceof" : return evaluate(left) instanceof evaluate(right);
713
+ }
714
+ }
715
+ throw $NOT_CONSTANT;
716
+ };
717
+
718
+ return function(expr, yes, no) {
719
+ try {
720
+ var val = evaluate(expr), ast;
721
+ switch (typeof val) {
722
+ case "string": ast = [ "string", val ]; break;
723
+ case "number": ast = [ "num", val ]; break;
724
+ case "boolean": ast = [ "name", String(val) ]; break;
725
+ default: throw new Error("Can't handle constant of type: " + (typeof val));
726
+ }
727
+ return yes.call(expr, ast, val);
728
+ } catch(ex) {
729
+ if (ex === $NOT_CONSTANT) {
730
+ if (expr[0] == "binary"
731
+ && (expr[1] == "===" || expr[1] == "!==")
732
+ && ((is_string(expr[2]) && is_string(expr[3]))
733
+ || (boolean_expr(expr[2]) && boolean_expr(expr[3])))) {
734
+ expr[1] = expr[1].substr(0, 2);
735
+ }
736
+ else if (no && expr[0] == "binary"
737
+ && (expr[1] == "||" || expr[1] == "&&")) {
738
+ // the whole expression is not constant but the lval may be...
739
+ try {
740
+ var lval = evaluate(expr[2]);
741
+ expr = ((expr[1] == "&&" && (lval ? expr[3] : lval)) ||
742
+ (expr[1] == "||" && (lval ? lval : expr[3])) ||
743
+ expr);
744
+ } catch(ex2) {
745
+ // IGNORE... lval is not constant
746
+ }
747
+ }
748
+ return no ? no.call(expr, expr) : null;
749
+ }
750
+ else throw ex;
751
+ }
752
+ };
753
+
754
+ })();
755
+
756
+ function warn_unreachable(ast) {
757
+ if (!empty(ast))
758
+ warn("Dropping unreachable code: " + gen_code(ast, true));
759
+ };
760
+
761
+ function prepare_ifs(ast) {
762
+ var w = ast_walker(), walk = w.walk;
763
+ // In this first pass, we rewrite ifs which abort with no else with an
764
+ // if-else. For example:
765
+ //
766
+ // if (x) {
767
+ // blah();
768
+ // return y;
769
+ // }
770
+ // foobar();
771
+ //
772
+ // is rewritten into:
773
+ //
774
+ // if (x) {
775
+ // blah();
776
+ // return y;
777
+ // } else {
778
+ // foobar();
779
+ // }
780
+ function redo_if(statements) {
781
+ statements = MAP(statements, walk);
782
+
783
+ for (var i = 0; i < statements.length; ++i) {
784
+ var fi = statements[i];
785
+ if (fi[0] != "if") continue;
786
+
787
+ if (fi[3] && walk(fi[3])) continue;
788
+
789
+ var t = walk(fi[2]);
790
+ if (!aborts(t)) continue;
791
+
792
+ var conditional = walk(fi[1]);
793
+
794
+ var e_body = statements.slice(i + 1);
795
+ var e = e_body.length == 1 ? e_body[0] : [ "block", e_body ];
796
+
797
+ var ret = statements.slice(0, i).concat([ [
798
+ fi[0], // "if"
799
+ conditional, // conditional
800
+ t, // then
801
+ e // else
802
+ ] ]);
803
+
804
+ return redo_if(ret);
805
+ }
806
+
807
+ return statements;
808
+ };
809
+
810
+ function redo_if_lambda(name, args, body) {
811
+ body = redo_if(body);
812
+ return [ this[0], name, args, body ];
813
+ };
814
+
815
+ function redo_if_block(statements) {
816
+ return [ this[0], statements != null ? redo_if(statements) : null ];
817
+ };
818
+
819
+ return w.with_walkers({
820
+ "defun": redo_if_lambda,
821
+ "function": redo_if_lambda,
822
+ "block": redo_if_block,
823
+ "splice": redo_if_block,
824
+ "toplevel": function(statements) {
825
+ return [ this[0], redo_if(statements) ];
826
+ },
827
+ "try": function(t, c, f) {
828
+ return [
829
+ this[0],
830
+ redo_if(t),
831
+ c != null ? [ c[0], redo_if(c[1]) ] : null,
832
+ f != null ? redo_if(f) : null
833
+ ];
834
+ }
835
+ }, function() {
836
+ return walk(ast);
837
+ });
838
+ };
839
+
840
+ function for_side_effects(ast, handler) {
841
+ var w = ast_walker(), walk = w.walk;
842
+ var $stop = {}, $restart = {};
843
+ function stop() { throw $stop };
844
+ function restart() { throw $restart };
845
+ function found(){ return handler.call(this, this, w, stop, restart) };
846
+ function unary(op) {
847
+ if (op == "++" || op == "--")
848
+ return found.apply(this, arguments);
849
+ };
850
+ return w.with_walkers({
851
+ "try": found,
852
+ "throw": found,
853
+ "return": found,
854
+ "new": found,
855
+ "switch": found,
856
+ "break": found,
857
+ "continue": found,
858
+ "assign": found,
859
+ "call": found,
860
+ "if": found,
861
+ "for": found,
862
+ "for-in": found,
863
+ "while": found,
864
+ "do": found,
865
+ "return": found,
866
+ "unary-prefix": unary,
867
+ "unary-postfix": unary,
868
+ "defun": found
869
+ }, function(){
870
+ while (true) try {
871
+ walk(ast);
872
+ break;
873
+ } catch(ex) {
874
+ if (ex === $stop) break;
875
+ if (ex === $restart) continue;
876
+ throw ex;
877
+ }
878
+ });
879
+ };
880
+
881
+ function ast_lift_variables(ast) {
882
+ var w = ast_walker(), walk = w.walk, scope;
883
+ function do_body(body, env) {
884
+ var _scope = scope;
885
+ scope = env;
886
+ body = MAP(body, walk);
887
+ var hash = {}, names = MAP(env.names, function(type, name){
888
+ if (type != "var") return MAP.skip;
889
+ if (!env.references(name)) return MAP.skip;
890
+ hash[name] = true;
891
+ return [ name ];
892
+ });
893
+ if (names.length > 0) {
894
+ // looking for assignments to any of these variables.
895
+ // we can save considerable space by moving the definitions
896
+ // in the var declaration.
897
+ for_side_effects([ "block", body ], function(ast, walker, stop, restart) {
898
+ if (ast[0] == "assign"
899
+ && ast[1] === true
900
+ && ast[2][0] == "name"
901
+ && HOP(hash, ast[2][1])) {
902
+ // insert the definition into the var declaration
903
+ for (var i = names.length; --i >= 0;) {
904
+ if (names[i][0] == ast[2][1]) {
905
+ if (names[i][1]) // this name already defined, we must stop
906
+ stop();
907
+ names[i][1] = ast[3]; // definition
908
+ names.push(names.splice(i, 1)[0]);
909
+ break;
910
+ }
911
+ }
912
+ // remove this assignment from the AST.
913
+ var p = walker.parent();
914
+ if (p[0] == "seq") {
915
+ var a = p[2];
916
+ a.unshift(0, p.length);
917
+ p.splice.apply(p, a);
918
+ }
919
+ else if (p[0] == "stat") {
920
+ p.splice(0, p.length, "block"); // empty statement
921
+ }
922
+ else {
923
+ stop();
924
+ }
925
+ restart();
926
+ }
927
+ stop();
928
+ });
929
+ body.unshift([ "var", names ]);
930
+ }
931
+ scope = _scope;
932
+ return body;
933
+ };
934
+ function _vardefs(defs) {
935
+ var ret = null;
936
+ for (var i = defs.length; --i >= 0;) {
937
+ var d = defs[i];
938
+ if (!d[1]) continue;
939
+ d = [ "assign", true, [ "name", d[0] ], d[1] ];
940
+ if (ret == null) ret = d;
941
+ else ret = [ "seq", d, ret ];
942
+ }
943
+ if (ret == null) {
944
+ if (w.parent()[0] == "for-in")
945
+ return [ "name", defs[0][0] ];
946
+ return MAP.skip;
947
+ }
948
+ return [ "stat", ret ];
949
+ };
950
+ function _toplevel(body) {
951
+ return [ this[0], do_body(body, this.scope) ];
952
+ };
953
+ return w.with_walkers({
954
+ "function": function(name, args, body){
955
+ for (var i = args.length; --i >= 0 && !body.scope.references(args[i]);)
956
+ args.pop();
957
+ if (!body.scope.references(name)) name = null;
958
+ return [ this[0], name, args, do_body(body, body.scope) ];
959
+ },
960
+ "defun": function(name, args, body){
961
+ if (!scope.references(name)) return MAP.skip;
962
+ for (var i = args.length; --i >= 0 && !body.scope.references(args[i]);)
963
+ args.pop();
964
+ return [ this[0], name, args, do_body(body, body.scope) ];
965
+ },
966
+ "var": _vardefs,
967
+ "toplevel": _toplevel
968
+ }, function(){
969
+ return walk(ast_add_scope(ast));
970
+ });
971
+ };
972
+
973
+ function ast_squeeze(ast, options) {
974
+ options = defaults(options, {
975
+ make_seqs : true,
976
+ dead_code : true,
977
+ keep_comps : true,
978
+ no_warnings : false
979
+ });
980
+
981
+ var w = ast_walker(), walk = w.walk, scope;
982
+
983
+ function negate(c) {
984
+ var not_c = [ "unary-prefix", "!", c ];
985
+ switch (c[0]) {
986
+ case "unary-prefix":
987
+ return c[1] == "!" && boolean_expr(c[2]) ? c[2] : not_c;
988
+ case "seq":
989
+ c = slice(c);
990
+ c[c.length - 1] = negate(c[c.length - 1]);
991
+ return c;
992
+ case "conditional":
993
+ return best_of(not_c, [ "conditional", c[1], negate(c[2]), negate(c[3]) ]);
994
+ case "binary":
995
+ var op = c[1], left = c[2], right = c[3];
996
+ if (!options.keep_comps) switch (op) {
997
+ case "<=" : return [ "binary", ">", left, right ];
998
+ case "<" : return [ "binary", ">=", left, right ];
999
+ case ">=" : return [ "binary", "<", left, right ];
1000
+ case ">" : return [ "binary", "<=", left, right ];
1001
+ }
1002
+ switch (op) {
1003
+ case "==" : return [ "binary", "!=", left, right ];
1004
+ case "!=" : return [ "binary", "==", left, right ];
1005
+ case "===" : return [ "binary", "!==", left, right ];
1006
+ case "!==" : return [ "binary", "===", left, right ];
1007
+ case "&&" : return best_of(not_c, [ "binary", "||", negate(left), negate(right) ]);
1008
+ case "||" : return best_of(not_c, [ "binary", "&&", negate(left), negate(right) ]);
1009
+ }
1010
+ break;
1011
+ }
1012
+ return not_c;
1013
+ };
1014
+
1015
+ function with_scope(s, cont) {
1016
+ var _scope = scope;
1017
+ scope = s;
1018
+ var ret = cont();
1019
+ ret.scope = s;
1020
+ scope = _scope;
1021
+ return ret;
1022
+ };
1023
+
1024
+ function rmblock(block) {
1025
+ if (block != null && block[0] == "block" && block[1]) {
1026
+ if (block[1].length == 1)
1027
+ block = block[1][0];
1028
+ else if (block[1].length == 0)
1029
+ block = [ "block" ];
1030
+ }
1031
+ return block;
1032
+ };
1033
+
1034
+ function _lambda(name, args, body) {
1035
+ var is_defun = this[0] == "defun";
1036
+ body = with_scope(body.scope, function(){
1037
+ var ret = tighten(body, "lambda");
1038
+ if (!is_defun && name && !scope.references(name))
1039
+ name = null;
1040
+ return ret;
1041
+ });
1042
+ return [ this[0], name, args, body ];
1043
+ };
1044
+
1045
+ // this function does a few things:
1046
+ // 1. discard useless blocks
1047
+ // 2. join consecutive var declarations
1048
+ // 3. remove obviously dead code
1049
+ // 4. transform consecutive statements using the comma operator
1050
+ // 5. if block_type == "lambda" and it detects constructs like if(foo) return ... - rewrite like if (!foo) { ... }
1051
+ function tighten(statements, block_type) {
1052
+ statements = MAP(statements, walk);
1053
+
1054
+ statements = statements.reduce(function(a, stat){
1055
+ if (stat[0] == "block") {
1056
+ if (stat[1]) {
1057
+ a.push.apply(a, stat[1]);
1058
+ }
1059
+ } else {
1060
+ a.push(stat);
1061
+ }
1062
+ return a;
1063
+ }, []);
1064
+
1065
+ statements = (function(a, prev){
1066
+ statements.forEach(function(cur){
1067
+ if (prev && ((cur[0] == "var" && prev[0] == "var") ||
1068
+ (cur[0] == "const" && prev[0] == "const"))) {
1069
+ prev[1] = prev[1].concat(cur[1]);
1070
+ } else {
1071
+ a.push(cur);
1072
+ prev = cur;
1073
+ }
1074
+ });
1075
+ return a;
1076
+ })([]);
1077
+
1078
+ if (options.dead_code) statements = (function(a, has_quit){
1079
+ statements.forEach(function(st){
1080
+ if (has_quit) {
1081
+ if (st[0] == "function" || st[0] == "defun") {
1082
+ a.push(st);
1083
+ }
1084
+ else if (st[0] == "var" || st[0] == "const") {
1085
+ if (!options.no_warnings)
1086
+ warn("Variables declared in unreachable code");
1087
+ st[1] = MAP(st[1], function(def){
1088
+ if (def[1] && !options.no_warnings)
1089
+ warn_unreachable([ "assign", true, [ "name", def[0] ], def[1] ]);
1090
+ return [ def[0] ];
1091
+ });
1092
+ a.push(st);
1093
+ }
1094
+ else if (!options.no_warnings)
1095
+ warn_unreachable(st);
1096
+ }
1097
+ else {
1098
+ a.push(st);
1099
+ if (member(st[0], [ "return", "throw", "break", "continue" ]))
1100
+ has_quit = true;
1101
+ }
1102
+ });
1103
+ return a;
1104
+ })([]);
1105
+
1106
+ if (options.make_seqs) statements = (function(a, prev) {
1107
+ statements.forEach(function(cur){
1108
+ if (prev && prev[0] == "stat" && cur[0] == "stat") {
1109
+ prev[1] = [ "seq", prev[1], cur[1] ];
1110
+ } else {
1111
+ a.push(cur);
1112
+ prev = cur;
1113
+ }
1114
+ });
1115
+ if (a.length >= 2
1116
+ && a[a.length-2][0] == "stat"
1117
+ && (a[a.length-1][0] == "return" || a[a.length-1][0] == "throw")
1118
+ && a[a.length-1][1])
1119
+ {
1120
+ a.splice(a.length - 2, 2,
1121
+ [ a[a.length-1][0],
1122
+ [ "seq", a[a.length-2][1], a[a.length-1][1] ]]);
1123
+ }
1124
+ return a;
1125
+ })([]);
1126
+
1127
+ // this increases jQuery by 1K. Probably not such a good idea after all..
1128
+ // part of this is done in prepare_ifs anyway.
1129
+ // if (block_type == "lambda") statements = (function(i, a, stat){
1130
+ // while (i < statements.length) {
1131
+ // stat = statements[i++];
1132
+ // if (stat[0] == "if" && !stat[3]) {
1133
+ // if (stat[2][0] == "return" && stat[2][1] == null) {
1134
+ // a.push(make_if(negate(stat[1]), [ "block", statements.slice(i) ]));
1135
+ // break;
1136
+ // }
1137
+ // var last = last_stat(stat[2]);
1138
+ // if (last[0] == "return" && last[1] == null) {
1139
+ // a.push(make_if(stat[1], [ "block", stat[2][1].slice(0, -1) ], [ "block", statements.slice(i) ]));
1140
+ // break;
1141
+ // }
1142
+ // }
1143
+ // a.push(stat);
1144
+ // }
1145
+ // return a;
1146
+ // })(0, []);
1147
+
1148
+ return statements;
1149
+ };
1150
+
1151
+ function make_if(c, t, e) {
1152
+ return when_constant(c, function(ast, val){
1153
+ if (val) {
1154
+ warn_unreachable(e);
1155
+ return t;
1156
+ } else {
1157
+ warn_unreachable(t);
1158
+ return e;
1159
+ }
1160
+ }, function() {
1161
+ return make_real_if(c, t, e);
1162
+ });
1163
+ };
1164
+
1165
+ function make_real_if(c, t, e) {
1166
+ c = walk(c);
1167
+ t = walk(t);
1168
+ e = walk(e);
1169
+
1170
+ if (empty(t)) {
1171
+ c = negate(c);
1172
+ t = e;
1173
+ e = null;
1174
+ } else if (empty(e)) {
1175
+ e = null;
1176
+ } else {
1177
+ // if we have both else and then, maybe it makes sense to switch them?
1178
+ (function(){
1179
+ var a = gen_code(c);
1180
+ var n = negate(c);
1181
+ var b = gen_code(n);
1182
+ if (b.length < a.length) {
1183
+ var tmp = t;
1184
+ t = e;
1185
+ e = tmp;
1186
+ c = n;
1187
+ }
1188
+ })();
1189
+ }
1190
+ if (empty(e) && empty(t))
1191
+ return [ "stat", c ];
1192
+ var ret = [ "if", c, t, e ];
1193
+ if (t[0] == "if" && empty(t[3]) && empty(e)) {
1194
+ ret = best_of(ret, walk([ "if", [ "binary", "&&", c, t[1] ], t[2] ]));
1195
+ }
1196
+ else if (t[0] == "stat") {
1197
+ if (e) {
1198
+ if (e[0] == "stat") {
1199
+ ret = best_of(ret, [ "stat", make_conditional(c, t[1], e[1]) ]);
1200
+ }
1201
+ }
1202
+ else {
1203
+ ret = best_of(ret, [ "stat", make_conditional(c, t[1]) ]);
1204
+ }
1205
+ }
1206
+ else if (e && t[0] == e[0] && (t[0] == "return" || t[0] == "throw") && t[1] && e[1]) {
1207
+ ret = best_of(ret, [ t[0], make_conditional(c, t[1], e[1] ) ]);
1208
+ }
1209
+ else if (e && aborts(t)) {
1210
+ ret = [ [ "if", c, t ] ];
1211
+ if (e[0] == "block") {
1212
+ if (e[1]) ret = ret.concat(e[1]);
1213
+ }
1214
+ else {
1215
+ ret.push(e);
1216
+ }
1217
+ ret = walk([ "block", ret ]);
1218
+ }
1219
+ else if (t && aborts(e)) {
1220
+ ret = [ [ "if", negate(c), e ] ];
1221
+ if (t[0] == "block") {
1222
+ if (t[1]) ret = ret.concat(t[1]);
1223
+ } else {
1224
+ ret.push(t);
1225
+ }
1226
+ ret = walk([ "block", ret ]);
1227
+ }
1228
+ return ret;
1229
+ };
1230
+
1231
+ function _do_while(cond, body) {
1232
+ return when_constant(cond, function(cond, val){
1233
+ if (!val) {
1234
+ warn_unreachable(body);
1235
+ return [ "block" ];
1236
+ } else {
1237
+ return [ "for", null, null, null, walk(body) ];
1238
+ }
1239
+ });
1240
+ };
1241
+
1242
+ return w.with_walkers({
1243
+ "sub": function(expr, subscript) {
1244
+ if (subscript[0] == "string") {
1245
+ var name = subscript[1];
1246
+ if (is_identifier(name))
1247
+ return [ "dot", walk(expr), name ];
1248
+ else if (/^[1-9][0-9]*$/.test(name) || name === "0")
1249
+ return [ "sub", walk(expr), [ "num", parseInt(name, 10) ] ];
1250
+ }
1251
+ },
1252
+ "if": make_if,
1253
+ "toplevel": function(body) {
1254
+ return [ "toplevel", with_scope(this.scope, function(){
1255
+ return tighten(body);
1256
+ }) ];
1257
+ },
1258
+ "switch": function(expr, body) {
1259
+ var last = body.length - 1;
1260
+ return [ "switch", walk(expr), MAP(body, function(branch, i){
1261
+ var block = tighten(branch[1]);
1262
+ if (i == last && block.length > 0) {
1263
+ var node = block[block.length - 1];
1264
+ if (node[0] == "break" && !node[1])
1265
+ block.pop();
1266
+ }
1267
+ return [ branch[0] ? walk(branch[0]) : null, block ];
1268
+ }) ];
1269
+ },
1270
+ "function": _lambda,
1271
+ "defun": _lambda,
1272
+ "block": function(body) {
1273
+ if (body) return rmblock([ "block", tighten(body) ]);
1274
+ },
1275
+ "binary": function(op, left, right) {
1276
+ return when_constant([ "binary", op, walk(left), walk(right) ], function yes(c){
1277
+ return best_of(walk(c), this);
1278
+ }, function no() {
1279
+ return this;
1280
+ });
1281
+ },
1282
+ "conditional": function(c, t, e) {
1283
+ return make_conditional(walk(c), walk(t), walk(e));
1284
+ },
1285
+ "try": function(t, c, f) {
1286
+ return [
1287
+ "try",
1288
+ tighten(t),
1289
+ c != null ? [ c[0], tighten(c[1]) ] : null,
1290
+ f != null ? tighten(f) : null
1291
+ ];
1292
+ },
1293
+ "unary-prefix": function(op, expr) {
1294
+ expr = walk(expr);
1295
+ var ret = [ "unary-prefix", op, expr ];
1296
+ if (op == "!")
1297
+ ret = best_of(ret, negate(expr));
1298
+ return when_constant(ret, function(ast, val){
1299
+ return walk(ast); // it's either true or false, so minifies to !0 or !1
1300
+ }, function() { return ret });
1301
+ },
1302
+ "name": function(name) {
1303
+ switch (name) {
1304
+ case "true": return [ "unary-prefix", "!", [ "num", 0 ]];
1305
+ case "false": return [ "unary-prefix", "!", [ "num", 1 ]];
1306
+ }
1307
+ },
1308
+ "while": _do_while
1309
+ }, function() {
1310
+ for (var i = 0; i < 2; ++i) {
1311
+ ast = prepare_ifs(ast);
1312
+ ast = ast_add_scope(ast);
1313
+ ast = walk(ast);
1314
+ }
1315
+ return ast;
1316
+ });
1317
+ };
1318
+
1319
+ /* -----[ re-generate code from the AST ]----- */
1320
+
1321
+ var DOT_CALL_NO_PARENS = jsp.array_to_hash([
1322
+ "name",
1323
+ "array",
1324
+ "object",
1325
+ "string",
1326
+ "dot",
1327
+ "sub",
1328
+ "call",
1329
+ "regexp"
1330
+ ]);
1331
+
1332
+ function make_string(str, ascii_only) {
1333
+ var dq = 0, sq = 0;
1334
+ str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029]/g, function(s){
1335
+ switch (s) {
1336
+ case "\\": return "\\\\";
1337
+ case "\b": return "\\b";
1338
+ case "\f": return "\\f";
1339
+ case "\n": return "\\n";
1340
+ case "\r": return "\\r";
1341
+ case "\t": return "\\t";
1342
+ case "\u2028": return "\\u2028";
1343
+ case "\u2029": return "\\u2029";
1344
+ case '"': ++dq; return '"';
1345
+ case "'": ++sq; return "'";
1346
+ }
1347
+ return s;
1348
+ });
1349
+ if (ascii_only) str = to_ascii(str);
1350
+ if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
1351
+ else return '"' + str.replace(/\x22/g, '\\"') + '"';
1352
+ };
1353
+
1354
+ function to_ascii(str) {
1355
+ return str.replace(/[\u0080-\uffff]/g, function(ch) {
1356
+ var code = ch.charCodeAt(0).toString(16);
1357
+ while (code.length < 4) code = "0" + code;
1358
+ return "\\u" + code;
1359
+ });
1360
+ };
1361
+
1362
+ var SPLICE_NEEDS_BRACKETS = jsp.array_to_hash([ "if", "while", "do", "for", "for-in", "with" ]);
1363
+
1364
+ function gen_code(ast, options) {
1365
+ options = defaults(options, {
1366
+ indent_start : 0,
1367
+ indent_level : 4,
1368
+ quote_keys : false,
1369
+ space_colon : false,
1370
+ beautify : false,
1371
+ ascii_only : false,
1372
+ inline_script: false
1373
+ });
1374
+ var beautify = !!options.beautify;
1375
+ var indentation = 0,
1376
+ newline = beautify ? "\n" : "",
1377
+ space = beautify ? " " : "";
1378
+
1379
+ function encode_string(str) {
1380
+ var ret = make_string(str, options.ascii_only);
1381
+ if (options.inline_script)
1382
+ ret = ret.replace(/<\x2fscript([>/\t\n\f\r ])/gi, "<\\/script$1");
1383
+ return ret;
1384
+ };
1385
+
1386
+ function make_name(name) {
1387
+ name = name.toString();
1388
+ if (options.ascii_only)
1389
+ name = to_ascii(name);
1390
+ return name;
1391
+ };
1392
+
1393
+ function indent(line) {
1394
+ if (line == null)
1395
+ line = "";
1396
+ if (beautify)
1397
+ line = repeat_string(" ", options.indent_start + indentation * options.indent_level) + line;
1398
+ return line;
1399
+ };
1400
+
1401
+ function with_indent(cont, incr) {
1402
+ if (incr == null) incr = 1;
1403
+ indentation += incr;
1404
+ try { return cont.apply(null, slice(arguments, 1)); }
1405
+ finally { indentation -= incr; }
1406
+ };
1407
+
1408
+ function add_spaces(a) {
1409
+ if (beautify)
1410
+ return a.join(" ");
1411
+ var b = [];
1412
+ for (var i = 0; i < a.length; ++i) {
1413
+ var next = a[i + 1];
1414
+ b.push(a[i]);
1415
+ if (next &&
1416
+ ((/[a-z0-9_\x24]$/i.test(a[i].toString()) && /^[a-z0-9_\x24]/i.test(next.toString())) ||
1417
+ (/[\+\-]$/.test(a[i].toString()) && /^[\+\-]/.test(next.toString())))) {
1418
+ b.push(" ");
1419
+ }
1420
+ }
1421
+ return b.join("");
1422
+ };
1423
+
1424
+ function add_commas(a) {
1425
+ return a.join("," + space);
1426
+ };
1427
+
1428
+ function parenthesize(expr) {
1429
+ var gen = make(expr);
1430
+ for (var i = 1; i < arguments.length; ++i) {
1431
+ var el = arguments[i];
1432
+ if ((el instanceof Function && el(expr)) || expr[0] == el)
1433
+ return "(" + gen + ")";
1434
+ }
1435
+ return gen;
1436
+ };
1437
+
1438
+ function best_of(a) {
1439
+ if (a.length == 1) {
1440
+ return a[0];
1441
+ }
1442
+ if (a.length == 2) {
1443
+ var b = a[1];
1444
+ a = a[0];
1445
+ return a.length <= b.length ? a : b;
1446
+ }
1447
+ return best_of([ a[0], best_of(a.slice(1)) ]);
1448
+ };
1449
+
1450
+ function needs_parens(expr) {
1451
+ if (expr[0] == "function" || expr[0] == "object") {
1452
+ // dot/call on a literal function requires the
1453
+ // function literal itself to be parenthesized
1454
+ // only if it's the first "thing" in a
1455
+ // statement. This means that the parent is
1456
+ // "stat", but it could also be a "seq" and
1457
+ // we're the first in this "seq" and the
1458
+ // parent is "stat", and so on. Messy stuff,
1459
+ // but it worths the trouble.
1460
+ var a = slice(w.stack()), self = a.pop(), p = a.pop();
1461
+ while (p) {
1462
+ if (p[0] == "stat") return true;
1463
+ if (((p[0] == "seq" || p[0] == "call" || p[0] == "dot" || p[0] == "sub" || p[0] == "conditional") && p[1] === self) ||
1464
+ ((p[0] == "binary" || p[0] == "assign" || p[0] == "unary-postfix") && p[2] === self)) {
1465
+ self = p;
1466
+ p = a.pop();
1467
+ } else {
1468
+ return false;
1469
+ }
1470
+ }
1471
+ }
1472
+ return !HOP(DOT_CALL_NO_PARENS, expr[0]);
1473
+ };
1474
+
1475
+ function make_num(num) {
1476
+ var str = num.toString(10), a = [ str.replace(/^0\./, ".") ], m;
1477
+ if (Math.floor(num) === num) {
1478
+ a.push("0x" + num.toString(16).toLowerCase(), // probably pointless
1479
+ "0" + num.toString(8)); // same.
1480
+ if ((m = /^(.*?)(0+)$/.exec(num))) {
1481
+ a.push(m[1] + "e" + m[2].length);
1482
+ }
1483
+ } else if ((m = /^0?\.(0+)(.*)$/.exec(num))) {
1484
+ a.push(m[2] + "e-" + (m[1].length + m[2].length),
1485
+ str.substr(str.indexOf(".")));
1486
+ }
1487
+ return best_of(a);
1488
+ };
1489
+
1490
+ var w = ast_walker();
1491
+ var make = w.walk;
1492
+ return w.with_walkers({
1493
+ "string": encode_string,
1494
+ "num": make_num,
1495
+ "name": make_name,
1496
+ "toplevel": function(statements) {
1497
+ return make_block_statements(statements)
1498
+ .join(newline + newline);
1499
+ },
1500
+ "splice": function(statements) {
1501
+ var parent = w.parent();
1502
+ if (HOP(SPLICE_NEEDS_BRACKETS, parent)) {
1503
+ // we need block brackets in this case
1504
+ return make_block.apply(this, arguments);
1505
+ } else {
1506
+ return MAP(make_block_statements(statements, true),
1507
+ function(line, i) {
1508
+ // the first line is already indented
1509
+ return i > 0 ? indent(line) : line;
1510
+ }).join(newline);
1511
+ }
1512
+ },
1513
+ "block": make_block,
1514
+ "var": function(defs) {
1515
+ return "var " + add_commas(MAP(defs, make_1vardef)) + ";";
1516
+ },
1517
+ "const": function(defs) {
1518
+ return "const " + add_commas(MAP(defs, make_1vardef)) + ";";
1519
+ },
1520
+ "try": function(tr, ca, fi) {
1521
+ var out = [ "try", make_block(tr) ];
1522
+ if (ca) out.push("catch", "(" + ca[0] + ")", make_block(ca[1]));
1523
+ if (fi) out.push("finally", make_block(fi));
1524
+ return add_spaces(out);
1525
+ },
1526
+ "throw": function(expr) {
1527
+ return add_spaces([ "throw", make(expr) ]) + ";";
1528
+ },
1529
+ "new": function(ctor, args) {
1530
+ args = args.length > 0 ? "(" + add_commas(MAP(args, make)) + ")" : "";
1531
+ return add_spaces([ "new", parenthesize(ctor, "seq", "binary", "conditional", "assign", function(expr){
1532
+ var w = ast_walker(), has_call = {};
1533
+ try {
1534
+ w.with_walkers({
1535
+ "call": function() { throw has_call },
1536
+ "function": function() { return this }
1537
+ }, function(){
1538
+ w.walk(expr);
1539
+ });
1540
+ } catch(ex) {
1541
+ if (ex === has_call)
1542
+ return true;
1543
+ throw ex;
1544
+ }
1545
+ }) + args ]);
1546
+ },
1547
+ "switch": function(expr, body) {
1548
+ return add_spaces([ "switch", "(" + make(expr) + ")", make_switch_block(body) ]);
1549
+ },
1550
+ "break": function(label) {
1551
+ var out = "break";
1552
+ if (label != null)
1553
+ out += " " + make_name(label);
1554
+ return out + ";";
1555
+ },
1556
+ "continue": function(label) {
1557
+ var out = "continue";
1558
+ if (label != null)
1559
+ out += " " + make_name(label);
1560
+ return out + ";";
1561
+ },
1562
+ "conditional": function(co, th, el) {
1563
+ return add_spaces([ parenthesize(co, "assign", "seq", "conditional"), "?",
1564
+ parenthesize(th, "seq"), ":",
1565
+ parenthesize(el, "seq") ]);
1566
+ },
1567
+ "assign": function(op, lvalue, rvalue) {
1568
+ if (op && op !== true) op += "=";
1569
+ else op = "=";
1570
+ return add_spaces([ make(lvalue), op, parenthesize(rvalue, "seq") ]);
1571
+ },
1572
+ "dot": function(expr) {
1573
+ var out = make(expr), i = 1;
1574
+ if (expr[0] == "num") {
1575
+ if (!/\./.test(expr[1]))
1576
+ out += ".";
1577
+ } else if (needs_parens(expr))
1578
+ out = "(" + out + ")";
1579
+ while (i < arguments.length)
1580
+ out += "." + make_name(arguments[i++]);
1581
+ return out;
1582
+ },
1583
+ "call": function(func, args) {
1584
+ var f = make(func);
1585
+ if (needs_parens(func))
1586
+ f = "(" + f + ")";
1587
+ return f + "(" + add_commas(MAP(args, function(expr){
1588
+ return parenthesize(expr, "seq");
1589
+ })) + ")";
1590
+ },
1591
+ "function": make_function,
1592
+ "defun": make_function,
1593
+ "if": function(co, th, el) {
1594
+ var out = [ "if", "(" + make(co) + ")", el ? make_then(th) : make(th) ];
1595
+ if (el) {
1596
+ out.push("else", make(el));
1597
+ }
1598
+ return add_spaces(out);
1599
+ },
1600
+ "for": function(init, cond, step, block) {
1601
+ var out = [ "for" ];
1602
+ init = (init != null ? make(init) : "").replace(/;*\s*$/, ";" + space);
1603
+ cond = (cond != null ? make(cond) : "").replace(/;*\s*$/, ";" + space);
1604
+ step = (step != null ? make(step) : "").replace(/;*\s*$/, "");
1605
+ var args = init + cond + step;
1606
+ if (args == "; ; ") args = ";;";
1607
+ out.push("(" + args + ")", make(block));
1608
+ return add_spaces(out);
1609
+ },
1610
+ "for-in": function(vvar, key, hash, block) {
1611
+ return add_spaces([ "for", "(" +
1612
+ (vvar ? make(vvar).replace(/;+$/, "") : make(key)),
1613
+ "in",
1614
+ make(hash) + ")", make(block) ]);
1615
+ },
1616
+ "while": function(condition, block) {
1617
+ return add_spaces([ "while", "(" + make(condition) + ")", make(block) ]);
1618
+ },
1619
+ "do": function(condition, block) {
1620
+ return add_spaces([ "do", make(block), "while", "(" + make(condition) + ")" ]) + ";";
1621
+ },
1622
+ "return": function(expr) {
1623
+ var out = [ "return" ];
1624
+ if (expr != null) out.push(make(expr));
1625
+ return add_spaces(out) + ";";
1626
+ },
1627
+ "binary": function(operator, lvalue, rvalue) {
1628
+ var left = make(lvalue), right = make(rvalue);
1629
+ // XXX: I'm pretty sure other cases will bite here.
1630
+ // we need to be smarter.
1631
+ // adding parens all the time is the safest bet.
1632
+ if (member(lvalue[0], [ "assign", "conditional", "seq" ]) ||
1633
+ lvalue[0] == "binary" && PRECEDENCE[operator] > PRECEDENCE[lvalue[1]]) {
1634
+ left = "(" + left + ")";
1635
+ }
1636
+ if (member(rvalue[0], [ "assign", "conditional", "seq" ]) ||
1637
+ rvalue[0] == "binary" && PRECEDENCE[operator] >= PRECEDENCE[rvalue[1]] &&
1638
+ !(rvalue[1] == operator && member(operator, [ "&&", "||", "*" ]))) {
1639
+ right = "(" + right + ")";
1640
+ }
1641
+ else if (!beautify && options.inline_script && (operator == "<" || operator == "<<")
1642
+ && rvalue[0] == "regexp" && /^script/i.test(rvalue[1])) {
1643
+ right = " " + right;
1644
+ }
1645
+ return add_spaces([ left, operator, right ]);
1646
+ },
1647
+ "unary-prefix": function(operator, expr) {
1648
+ var val = make(expr);
1649
+ if (!(expr[0] == "num" || (expr[0] == "unary-prefix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
1650
+ val = "(" + val + ")";
1651
+ return operator + (jsp.is_alphanumeric_char(operator.charAt(0)) ? " " : "") + val;
1652
+ },
1653
+ "unary-postfix": function(operator, expr) {
1654
+ var val = make(expr);
1655
+ if (!(expr[0] == "num" || (expr[0] == "unary-postfix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
1656
+ val = "(" + val + ")";
1657
+ return val + operator;
1658
+ },
1659
+ "sub": function(expr, subscript) {
1660
+ var hash = make(expr);
1661
+ if (needs_parens(expr))
1662
+ hash = "(" + hash + ")";
1663
+ return hash + "[" + make(subscript) + "]";
1664
+ },
1665
+ "object": function(props) {
1666
+ if (props.length == 0)
1667
+ return "{}";
1668
+ return "{" + newline + with_indent(function(){
1669
+ return MAP(props, function(p){
1670
+ if (p.length == 3) {
1671
+ // getter/setter. The name is in p[0], the arg.list in p[1][2], the
1672
+ // body in p[1][3] and type ("get" / "set") in p[2].
1673
+ return indent(make_function(p[0], p[1][2], p[1][3], p[2]));
1674
+ }
1675
+ var key = p[0], val = parenthesize(p[1], "seq");
1676
+ if (options.quote_keys) {
1677
+ key = encode_string(key);
1678
+ } else if ((typeof key == "number" || !beautify && +key + "" == key)
1679
+ && parseFloat(key) >= 0) {
1680
+ key = make_num(+key);
1681
+ } else if (!is_identifier(key)) {
1682
+ key = encode_string(key);
1683
+ }
1684
+ return indent(add_spaces(beautify && options.space_colon
1685
+ ? [ key, ":", val ]
1686
+ : [ key + ":", val ]));
1687
+ }).join("," + newline);
1688
+ }) + newline + indent("}");
1689
+ },
1690
+ "regexp": function(rx, mods) {
1691
+ return "/" + rx + "/" + mods;
1692
+ },
1693
+ "array": function(elements) {
1694
+ if (elements.length == 0) return "[]";
1695
+ return add_spaces([ "[", add_commas(MAP(elements, function(el){
1696
+ if (!beautify && el[0] == "atom" && el[1] == "undefined") return "";
1697
+ return parenthesize(el, "seq");
1698
+ })), "]" ]);
1699
+ },
1700
+ "stat": function(stmt) {
1701
+ return make(stmt).replace(/;*\s*$/, ";");
1702
+ },
1703
+ "seq": function() {
1704
+ return add_commas(MAP(slice(arguments), make));
1705
+ },
1706
+ "label": function(name, block) {
1707
+ return add_spaces([ make_name(name), ":", make(block) ]);
1708
+ },
1709
+ "with": function(expr, block) {
1710
+ return add_spaces([ "with", "(" + make(expr) + ")", make(block) ]);
1711
+ },
1712
+ "atom": function(name) {
1713
+ return make_name(name);
1714
+ }
1715
+ }, function(){ return make(ast) });
1716
+
1717
+ // The squeezer replaces "block"-s that contain only a single
1718
+ // statement with the statement itself; technically, the AST
1719
+ // is correct, but this can create problems when we output an
1720
+ // IF having an ELSE clause where the THEN clause ends in an
1721
+ // IF *without* an ELSE block (then the outer ELSE would refer
1722
+ // to the inner IF). This function checks for this case and
1723
+ // adds the block brackets if needed.
1724
+ function make_then(th) {
1725
+ if (th[0] == "do") {
1726
+ // https://github.com/mishoo/UglifyJS/issues/#issue/57
1727
+ // IE croaks with "syntax error" on code like this:
1728
+ // if (foo) do ... while(cond); else ...
1729
+ // we need block brackets around do/while
1730
+ return make_block([ th ]);
1731
+ }
1732
+ var b = th;
1733
+ while (true) {
1734
+ var type = b[0];
1735
+ if (type == "if") {
1736
+ if (!b[3])
1737
+ // no else, we must add the block
1738
+ return make([ "block", [ th ]]);
1739
+ b = b[3];
1740
+ }
1741
+ else if (type == "while" || type == "do") b = b[2];
1742
+ else if (type == "for" || type == "for-in") b = b[4];
1743
+ else break;
1744
+ }
1745
+ return make(th);
1746
+ };
1747
+
1748
+ function make_function(name, args, body, keyword) {
1749
+ var out = keyword || "function";
1750
+ if (name) {
1751
+ out += " " + make_name(name);
1752
+ }
1753
+ out += "(" + add_commas(MAP(args, make_name)) + ")";
1754
+ return add_spaces([ out, make_block(body) ]);
1755
+ };
1756
+
1757
+ function must_has_semicolon(node) {
1758
+ switch (node[0]) {
1759
+ case "with":
1760
+ case "while":
1761
+ return empty(node[2]); // `with' or `while' with empty body?
1762
+ case "for":
1763
+ case "for-in":
1764
+ return empty(node[4]); // `for' with empty body?
1765
+ case "if":
1766
+ if (empty(node[2]) && !node[3]) return true; // `if' with empty `then' and no `else'
1767
+ if (node[3]) {
1768
+ if (empty(node[3])) return true; // `else' present but empty
1769
+ return must_has_semicolon(node[3]); // dive into the `else' branch
1770
+ }
1771
+ return must_has_semicolon(node[2]); // dive into the `then' branch
1772
+ }
1773
+ };
1774
+
1775
+ function make_block_statements(statements, noindent) {
1776
+ for (var a = [], last = statements.length - 1, i = 0; i <= last; ++i) {
1777
+ var stat = statements[i];
1778
+ var code = make(stat);
1779
+ if (code != ";") {
1780
+ if (!beautify && i == last && !must_has_semicolon(stat)) {
1781
+ code = code.replace(/;+\s*$/, "");
1782
+ }
1783
+ a.push(code);
1784
+ }
1785
+ }
1786
+ return noindent ? a : MAP(a, indent);
1787
+ };
1788
+
1789
+ function make_switch_block(body) {
1790
+ var n = body.length;
1791
+ if (n == 0) return "{}";
1792
+ return "{" + newline + MAP(body, function(branch, i){
1793
+ var has_body = branch[1].length > 0, code = with_indent(function(){
1794
+ return indent(branch[0]
1795
+ ? add_spaces([ "case", make(branch[0]) + ":" ])
1796
+ : "default:");
1797
+ }, 0.5) + (has_body ? newline + with_indent(function(){
1798
+ return make_block_statements(branch[1]).join(newline);
1799
+ }) : "");
1800
+ if (!beautify && has_body && i < n - 1)
1801
+ code += ";";
1802
+ return code;
1803
+ }).join(newline) + newline + indent("}");
1804
+ };
1805
+
1806
+ function make_block(statements) {
1807
+ if (!statements) return ";";
1808
+ if (statements.length == 0) return "{}";
1809
+ return "{" + newline + with_indent(function(){
1810
+ return make_block_statements(statements).join(newline);
1811
+ }) + newline + indent("}");
1812
+ };
1813
+
1814
+ function make_1vardef(def) {
1815
+ var name = def[0], val = def[1];
1816
+ if (val != null)
1817
+ name = add_spaces([ make_name(name), "=", parenthesize(val, "seq") ]);
1818
+ return name;
1819
+ };
1820
+
1821
+ };
1822
+
1823
+ function split_lines(code, max_line_length) {
1824
+ var splits = [ 0 ];
1825
+ jsp.parse(function(){
1826
+ var next_token = jsp.tokenizer(code);
1827
+ var last_split = 0;
1828
+ var prev_token;
1829
+ function current_length(tok) {
1830
+ return tok.pos - last_split;
1831
+ };
1832
+ function split_here(tok) {
1833
+ last_split = tok.pos;
1834
+ splits.push(last_split);
1835
+ };
1836
+ function custom(){
1837
+ var tok = next_token.apply(this, arguments);
1838
+ out: {
1839
+ if (prev_token) {
1840
+ if (prev_token.type == "keyword") break out;
1841
+ }
1842
+ if (current_length(tok) > max_line_length) {
1843
+ switch (tok.type) {
1844
+ case "keyword":
1845
+ case "atom":
1846
+ case "name":
1847
+ case "punc":
1848
+ split_here(tok);
1849
+ break out;
1850
+ }
1851
+ }
1852
+ }
1853
+ prev_token = tok;
1854
+ return tok;
1855
+ };
1856
+ custom.context = function() {
1857
+ return next_token.context.apply(this, arguments);
1858
+ };
1859
+ return custom;
1860
+ }());
1861
+ return splits.map(function(pos, i){
1862
+ return code.substring(pos, splits[i + 1] || code.length);
1863
+ }).join("\n");
1864
+ };
1865
+
1866
+ /* -----[ Utilities ]----- */
1867
+
1868
+ function repeat_string(str, i) {
1869
+ if (i <= 0) return "";
1870
+ if (i == 1) return str;
1871
+ var d = repeat_string(str, i >> 1);
1872
+ d += d;
1873
+ if (i & 1) d += str;
1874
+ return d;
1875
+ };
1876
+
1877
+ function defaults(args, defs) {
1878
+ var ret = {};
1879
+ if (args === true)
1880
+ args = {};
1881
+ for (var i in defs) if (HOP(defs, i)) {
1882
+ ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
1883
+ }
1884
+ return ret;
1885
+ };
1886
+
1887
+ function is_identifier(name) {
1888
+ return /^[a-z_$][a-z0-9_$]*$/i.test(name)
1889
+ && name != "this"
1890
+ && !HOP(jsp.KEYWORDS_ATOM, name)
1891
+ && !HOP(jsp.RESERVED_WORDS, name)
1892
+ && !HOP(jsp.KEYWORDS, name);
1893
+ };
1894
+
1895
+ function HOP(obj, prop) {
1896
+ return Object.prototype.hasOwnProperty.call(obj, prop);
1897
+ };
1898
+
1899
+ // some utilities
1900
+
1901
+ var MAP;
1902
+
1903
+ (function(){
1904
+ MAP = function(a, f, o) {
1905
+ var ret = [], top = [], i;
1906
+ function doit() {
1907
+ var val = f.call(o, a[i], i);
1908
+ if (val instanceof AtTop) {
1909
+ val = val.v;
1910
+ if (val instanceof Splice) {
1911
+ top.push.apply(top, val.v);
1912
+ } else {
1913
+ top.push(val);
1914
+ }
1915
+ }
1916
+ else if (val != skip) {
1917
+ if (val instanceof Splice) {
1918
+ ret.push.apply(ret, val.v);
1919
+ } else {
1920
+ ret.push(val);
1921
+ }
1922
+ }
1923
+ };
1924
+ if (a instanceof Array) for (i = 0; i < a.length; ++i) doit();
1925
+ else for (i in a) if (HOP(a, i)) doit();
1926
+ return top.concat(ret);
1927
+ };
1928
+ MAP.at_top = function(val) { return new AtTop(val) };
1929
+ MAP.splice = function(val) { return new Splice(val) };
1930
+ var skip = MAP.skip = {};
1931
+ function AtTop(val) { this.v = val };
1932
+ function Splice(val) { this.v = val };
1933
+ })();
1934
+
1935
+ /* -----[ Exports ]----- */
1936
+
1937
+ exports.ast_walker = ast_walker;
1938
+ exports.ast_mangle = ast_mangle;
1939
+ exports.ast_squeeze = ast_squeeze;
1940
+ exports.ast_lift_variables = ast_lift_variables;
1941
+ exports.gen_code = gen_code;
1942
+ exports.ast_add_scope = ast_add_scope;
1943
+ exports.set_logger = function(logger) { warn = logger };
1944
+ exports.make_string = make_string;
1945
+ exports.split_lines = split_lines;
1946
+ exports.MAP = MAP;
1947
+
1948
+ // keep this last!
1949
+ exports.ast_squeeze_more = require("./squeeze-more").ast_squeeze_more;