transform.js 6.8 KB

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