You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.7 KiB

4 years ago
  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 TreeTransformer(before, after) {
  35. TreeWalker.call(this);
  36. this.before = before;
  37. this.after = after;
  38. }
  39. TreeTransformer.prototype = new TreeWalker;
  40. (function(DEF) {
  41. function do_list(list, tw) {
  42. return MAP(list, function(node) {
  43. return node.transform(tw, true);
  44. });
  45. }
  46. DEF(AST_Node, noop);
  47. DEF(AST_LabeledStatement, function(self, tw) {
  48. self.label = self.label.transform(tw);
  49. self.body = self.body.transform(tw);
  50. });
  51. DEF(AST_SimpleStatement, function(self, tw) {
  52. self.body = self.body.transform(tw);
  53. });
  54. DEF(AST_Block, function(self, tw) {
  55. self.body = do_list(self.body, tw);
  56. });
  57. DEF(AST_Do, function(self, tw) {
  58. self.body = self.body.transform(tw);
  59. self.condition = self.condition.transform(tw);
  60. });
  61. DEF(AST_While, function(self, tw) {
  62. self.condition = self.condition.transform(tw);
  63. self.body = self.body.transform(tw);
  64. });
  65. DEF(AST_For, function(self, tw) {
  66. if (self.init) self.init = self.init.transform(tw);
  67. if (self.condition) self.condition = self.condition.transform(tw);
  68. if (self.step) self.step = self.step.transform(tw);
  69. self.body = self.body.transform(tw);
  70. });
  71. DEF(AST_ForIn, function(self, tw) {
  72. self.init = self.init.transform(tw);
  73. self.object = self.object.transform(tw);
  74. self.body = self.body.transform(tw);
  75. });
  76. DEF(AST_With, function(self, tw) {
  77. self.expression = self.expression.transform(tw);
  78. self.body = self.body.transform(tw);
  79. });
  80. DEF(AST_Exit, function(self, tw) {
  81. if (self.value) self.value = self.value.transform(tw);
  82. });
  83. DEF(AST_LoopControl, function(self, tw) {
  84. if (self.label) self.label = self.label.transform(tw);
  85. });
  86. DEF(AST_If, function(self, tw) {
  87. self.condition = self.condition.transform(tw);
  88. self.body = self.body.transform(tw);
  89. if (self.alternative) self.alternative = self.alternative.transform(tw);
  90. });
  91. DEF(AST_Switch, function(self, tw) {
  92. self.expression = self.expression.transform(tw);
  93. self.body = do_list(self.body, tw);
  94. });
  95. DEF(AST_Case, function(self, tw) {
  96. self.expression = self.expression.transform(tw);
  97. self.body = do_list(self.body, tw);
  98. });
  99. DEF(AST_Try, function(self, tw) {
  100. self.body = do_list(self.body, tw);
  101. if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
  102. if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
  103. });
  104. DEF(AST_Catch, function(self, tw) {
  105. self.argname = self.argname.transform(tw);
  106. self.body = do_list(self.body, tw);
  107. });
  108. DEF(AST_Definitions, function(self, tw) {
  109. self.definitions = do_list(self.definitions, tw);
  110. });
  111. DEF(AST_VarDef, function(self, tw) {
  112. self.name = self.name.transform(tw);
  113. if (self.value) self.value = self.value.transform(tw);
  114. });
  115. DEF(AST_Lambda, function(self, tw) {
  116. if (self.name) self.name = self.name.transform(tw);
  117. self.argnames = do_list(self.argnames, tw);
  118. self.body = do_list(self.body, tw);
  119. });
  120. DEF(AST_Call, function(self, tw) {
  121. self.expression = self.expression.transform(tw);
  122. self.args = do_list(self.args, tw);
  123. });
  124. DEF(AST_Sequence, function(self, tw) {
  125. self.expressions = do_list(self.expressions, tw);
  126. });
  127. DEF(AST_Dot, function(self, tw) {
  128. self.expression = self.expression.transform(tw);
  129. });
  130. DEF(AST_Sub, function(self, tw) {
  131. self.expression = self.expression.transform(tw);
  132. self.property = self.property.transform(tw);
  133. });
  134. DEF(AST_Unary, function(self, tw) {
  135. self.expression = self.expression.transform(tw);
  136. });
  137. DEF(AST_Binary, function(self, tw) {
  138. self.left = self.left.transform(tw);
  139. self.right = self.right.transform(tw);
  140. });
  141. DEF(AST_Conditional, function(self, tw) {
  142. self.condition = self.condition.transform(tw);
  143. self.consequent = self.consequent.transform(tw);
  144. self.alternative = self.alternative.transform(tw);
  145. });
  146. DEF(AST_Array, function(self, tw) {
  147. self.elements = do_list(self.elements, tw);
  148. });
  149. DEF(AST_Object, function(self, tw) {
  150. self.properties = do_list(self.properties, tw);
  151. });
  152. DEF(AST_ObjectProperty, function(self, tw) {
  153. self.value = self.value.transform(tw);
  154. });
  155. })(function(node, descend) {
  156. node.DEFMETHOD("transform", function(tw, in_list) {
  157. var x, y;
  158. tw.push(this);
  159. if (tw.before) x = tw.before(this, descend, in_list);
  160. if (typeof x === "undefined") {
  161. x = this;
  162. descend(x, tw);
  163. if (tw.after) {
  164. y = tw.after(x, in_list);
  165. if (typeof y !== "undefined") x = y;
  166. }
  167. }
  168. tw.pop();
  169. return x;
  170. });
  171. });