transform.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. import {
  35. AST_Array,
  36. AST_Await,
  37. AST_Binary,
  38. AST_Block,
  39. AST_Call,
  40. AST_Case,
  41. AST_Catch,
  42. AST_Chain,
  43. AST_Class,
  44. AST_Conditional,
  45. AST_Definitions,
  46. AST_Destructuring,
  47. AST_Do,
  48. AST_Dot,
  49. AST_Exit,
  50. AST_Expansion,
  51. AST_Export,
  52. AST_For,
  53. AST_ForIn,
  54. AST_If,
  55. AST_Import,
  56. AST_LabeledStatement,
  57. AST_Lambda,
  58. AST_LoopControl,
  59. AST_NameMapping,
  60. AST_Node,
  61. AST_Number,
  62. AST_Object,
  63. AST_ObjectProperty,
  64. AST_PrefixedTemplateString,
  65. AST_Sequence,
  66. AST_SimpleStatement,
  67. AST_Sub,
  68. AST_Switch,
  69. AST_TemplateString,
  70. AST_Try,
  71. AST_Unary,
  72. AST_VarDef,
  73. AST_While,
  74. AST_With,
  75. AST_Yield,
  76. } from "./ast.js";
  77. import {
  78. MAP,
  79. noop,
  80. } from "./utils/index.js";
  81. function def_transform(node, descend) {
  82. node.DEFMETHOD("transform", function(tw, in_list) {
  83. let transformed = undefined;
  84. tw.push(this);
  85. if (tw.before) transformed = tw.before(this, descend, in_list);
  86. if (transformed === undefined) {
  87. transformed = this;
  88. descend(transformed, tw);
  89. if (tw.after) {
  90. const after_ret = tw.after(transformed, in_list);
  91. if (after_ret !== undefined) transformed = after_ret;
  92. }
  93. }
  94. tw.pop();
  95. return transformed;
  96. });
  97. }
  98. function do_list(list, tw) {
  99. return MAP(list, function(node) {
  100. return node.transform(tw, true);
  101. });
  102. }
  103. def_transform(AST_Node, noop);
  104. def_transform(AST_LabeledStatement, function(self, tw) {
  105. self.label = self.label.transform(tw);
  106. self.body = self.body.transform(tw);
  107. });
  108. def_transform(AST_SimpleStatement, function(self, tw) {
  109. self.body = self.body.transform(tw);
  110. });
  111. def_transform(AST_Block, function(self, tw) {
  112. self.body = do_list(self.body, tw);
  113. });
  114. def_transform(AST_Do, function(self, tw) {
  115. self.body = self.body.transform(tw);
  116. self.condition = self.condition.transform(tw);
  117. });
  118. def_transform(AST_While, function(self, tw) {
  119. self.condition = self.condition.transform(tw);
  120. self.body = self.body.transform(tw);
  121. });
  122. def_transform(AST_For, function(self, tw) {
  123. if (self.init) self.init = self.init.transform(tw);
  124. if (self.condition) self.condition = self.condition.transform(tw);
  125. if (self.step) self.step = self.step.transform(tw);
  126. self.body = self.body.transform(tw);
  127. });
  128. def_transform(AST_ForIn, function(self, tw) {
  129. self.init = self.init.transform(tw);
  130. self.object = self.object.transform(tw);
  131. self.body = self.body.transform(tw);
  132. });
  133. def_transform(AST_With, function(self, tw) {
  134. self.expression = self.expression.transform(tw);
  135. self.body = self.body.transform(tw);
  136. });
  137. def_transform(AST_Exit, function(self, tw) {
  138. if (self.value) self.value = self.value.transform(tw);
  139. });
  140. def_transform(AST_LoopControl, function(self, tw) {
  141. if (self.label) self.label = self.label.transform(tw);
  142. });
  143. def_transform(AST_If, function(self, tw) {
  144. self.condition = self.condition.transform(tw);
  145. self.body = self.body.transform(tw);
  146. if (self.alternative) self.alternative = self.alternative.transform(tw);
  147. });
  148. def_transform(AST_Switch, function(self, tw) {
  149. self.expression = self.expression.transform(tw);
  150. self.body = do_list(self.body, tw);
  151. });
  152. def_transform(AST_Case, function(self, tw) {
  153. self.expression = self.expression.transform(tw);
  154. self.body = do_list(self.body, tw);
  155. });
  156. def_transform(AST_Try, function(self, tw) {
  157. self.body = do_list(self.body, tw);
  158. if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
  159. if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
  160. });
  161. def_transform(AST_Catch, function(self, tw) {
  162. if (self.argname) self.argname = self.argname.transform(tw);
  163. self.body = do_list(self.body, tw);
  164. });
  165. def_transform(AST_Definitions, function(self, tw) {
  166. self.definitions = do_list(self.definitions, tw);
  167. });
  168. def_transform(AST_VarDef, function(self, tw) {
  169. self.name = self.name.transform(tw);
  170. if (self.value) self.value = self.value.transform(tw);
  171. });
  172. def_transform(AST_Destructuring, function(self, tw) {
  173. self.names = do_list(self.names, tw);
  174. });
  175. def_transform(AST_Lambda, function(self, tw) {
  176. if (self.name) self.name = self.name.transform(tw);
  177. self.argnames = do_list(self.argnames, tw);
  178. if (self.body instanceof AST_Node) {
  179. self.body = self.body.transform(tw);
  180. } else {
  181. self.body = do_list(self.body, tw);
  182. }
  183. });
  184. def_transform(AST_Call, function(self, tw) {
  185. self.expression = self.expression.transform(tw);
  186. self.args = do_list(self.args, tw);
  187. });
  188. def_transform(AST_Sequence, function(self, tw) {
  189. const result = do_list(self.expressions, tw);
  190. self.expressions = result.length
  191. ? result
  192. : [new AST_Number({ value: 0 })];
  193. });
  194. def_transform(AST_Dot, function(self, tw) {
  195. self.expression = self.expression.transform(tw);
  196. });
  197. def_transform(AST_Sub, function(self, tw) {
  198. self.expression = self.expression.transform(tw);
  199. self.property = self.property.transform(tw);
  200. });
  201. def_transform(AST_Chain, function(self, tw) {
  202. self.expression = self.expression.transform(tw);
  203. });
  204. def_transform(AST_Yield, function(self, tw) {
  205. if (self.expression) self.expression = self.expression.transform(tw);
  206. });
  207. def_transform(AST_Await, function(self, tw) {
  208. self.expression = self.expression.transform(tw);
  209. });
  210. def_transform(AST_Unary, function(self, tw) {
  211. self.expression = self.expression.transform(tw);
  212. });
  213. def_transform(AST_Binary, function(self, tw) {
  214. self.left = self.left.transform(tw);
  215. self.right = self.right.transform(tw);
  216. });
  217. def_transform(AST_Conditional, function(self, tw) {
  218. self.condition = self.condition.transform(tw);
  219. self.consequent = self.consequent.transform(tw);
  220. self.alternative = self.alternative.transform(tw);
  221. });
  222. def_transform(AST_Array, function(self, tw) {
  223. self.elements = do_list(self.elements, tw);
  224. });
  225. def_transform(AST_Object, function(self, tw) {
  226. self.properties = do_list(self.properties, tw);
  227. });
  228. def_transform(AST_ObjectProperty, function(self, tw) {
  229. if (self.key instanceof AST_Node) {
  230. self.key = self.key.transform(tw);
  231. }
  232. if (self.value) self.value = self.value.transform(tw);
  233. });
  234. def_transform(AST_Class, function(self, tw) {
  235. if (self.name) self.name = self.name.transform(tw);
  236. if (self.extends) self.extends = self.extends.transform(tw);
  237. self.properties = do_list(self.properties, tw);
  238. });
  239. def_transform(AST_Expansion, function(self, tw) {
  240. self.expression = self.expression.transform(tw);
  241. });
  242. def_transform(AST_NameMapping, function(self, tw) {
  243. self.foreign_name = self.foreign_name.transform(tw);
  244. self.name = self.name.transform(tw);
  245. });
  246. def_transform(AST_Import, function(self, tw) {
  247. if (self.imported_name) self.imported_name = self.imported_name.transform(tw);
  248. if (self.imported_names) do_list(self.imported_names, tw);
  249. self.module_name = self.module_name.transform(tw);
  250. });
  251. def_transform(AST_Export, function(self, tw) {
  252. if (self.exported_definition) self.exported_definition = self.exported_definition.transform(tw);
  253. if (self.exported_value) self.exported_value = self.exported_value.transform(tw);
  254. if (self.exported_names) do_list(self.exported_names, tw);
  255. if (self.module_name) self.module_name = self.module_name.transform(tw);
  256. });
  257. def_transform(AST_TemplateString, function(self, tw) {
  258. self.segments = do_list(self.segments, tw);
  259. });
  260. def_transform(AST_PrefixedTemplateString, function(self, tw) {
  261. self.prefix = self.prefix.transform(tw);
  262. self.template_string = self.template_string.transform(tw);
  263. });