transform.js 9.6 KB

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