UnicodeArrayRotateFunctionNode.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. "use strict";
  2. const estraverse = require('estraverse');
  3. const Node_1 = require('./Node');
  4. const Utils_1 = require('../Utils');
  5. class UnicodeArrayRotateFunctionNode extends Node_1.Node {
  6. constructor(astTree, unicodeArrayRotateFunctionName, unicodeArrayName) {
  7. super();
  8. this.astTree = astTree;
  9. this.unicodeArrayRotateFunctionName = unicodeArrayRotateFunctionName;
  10. this.unicodeArrayName = unicodeArrayName;
  11. this.node = this.getNodeStructure();
  12. }
  13. appendNode() {
  14. estraverse.replace(this.astTree, {
  15. leave: (node, parent) => {
  16. switch (node.type) {
  17. case 'Program':
  18. node.body.push(this.getNode());
  19. break;
  20. }
  21. }
  22. });
  23. }
  24. getNodeIdentifier() {
  25. return this.unicodeArrayRotateFunctionName;
  26. }
  27. getNodeStructure() {
  28. return {
  29. 'type': 'FunctionExpression',
  30. 'id': {
  31. 'type': 'Identifier',
  32. 'name': this.unicodeArrayRotateFunctionName
  33. },
  34. 'params': [
  35. {
  36. 'type': 'Identifier',
  37. 'name': 'array'
  38. },
  39. {
  40. 'type': 'Identifier',
  41. 'name': 'times'
  42. },
  43. {
  44. 'type': 'Identifier',
  45. 'name': 'reverse'
  46. }
  47. ],
  48. 'defaults': [],
  49. 'body': {
  50. 'type': 'BlockStatement',
  51. 'body': [
  52. {
  53. 'type': 'IfStatement',
  54. 'test': {
  55. 'type': 'BinaryExpression',
  56. 'operator': '<',
  57. 'left': {
  58. 'type': 'Identifier',
  59. 'name': 'times'
  60. },
  61. 'right': {
  62. 'type': 'Literal',
  63. 'value': 0,
  64. 'raw': '0'
  65. }
  66. },
  67. 'consequent': {
  68. 'type': 'BlockStatement',
  69. 'body': [
  70. {
  71. 'type': 'ReturnStatement',
  72. 'argument': null
  73. }
  74. ]
  75. },
  76. 'alternate': null
  77. },
  78. {
  79. 'type': 'ExpressionStatement',
  80. 'expression': {
  81. 'type': 'AssignmentExpression',
  82. 'operator': '=',
  83. 'left': {
  84. 'type': 'Identifier',
  85. 'name': 'reverse'
  86. },
  87. 'right': {
  88. 'type': 'LogicalExpression',
  89. 'operator': '||',
  90. 'left': {
  91. 'type': 'Identifier',
  92. 'name': 'reverse'
  93. },
  94. 'right': {
  95. 'type': 'Literal',
  96. 'value': false,
  97. 'raw': 'false'
  98. }
  99. }
  100. }
  101. },
  102. {
  103. 'type': 'VariableDeclaration',
  104. 'declarations': [
  105. {
  106. 'type': 'VariableDeclarator',
  107. 'id': {
  108. 'type': 'Identifier',
  109. 'name': 'temp'
  110. },
  111. 'init': null
  112. }
  113. ],
  114. 'kind': 'var'
  115. },
  116. {
  117. 'type': 'WhileStatement',
  118. 'test': {
  119. 'type': 'UpdateExpression',
  120. 'operator': '--',
  121. 'argument': {
  122. 'type': 'Identifier',
  123. 'name': 'times'
  124. },
  125. 'prefix': false
  126. },
  127. 'body': {
  128. 'type': 'BlockStatement',
  129. 'body': [
  130. {
  131. 'type': 'IfStatement',
  132. 'test': {
  133. 'type': 'UnaryExpression',
  134. 'operator': '!',
  135. 'argument': {
  136. 'type': 'Identifier',
  137. 'name': 'reverse'
  138. },
  139. 'prefix': true
  140. },
  141. 'consequent': {
  142. 'type': 'BlockStatement',
  143. 'body': [
  144. {
  145. 'type': 'ExpressionStatement',
  146. 'expression': {
  147. 'type': 'AssignmentExpression',
  148. 'operator': '=',
  149. 'left': {
  150. 'type': 'Identifier',
  151. 'name': 'temp'
  152. },
  153. 'right': {
  154. 'type': 'CallExpression',
  155. 'callee': {
  156. 'type': 'MemberExpression',
  157. 'computed': true,
  158. 'object': {
  159. 'type': 'Identifier',
  160. 'name': 'array'
  161. },
  162. 'property': {
  163. 'type': 'Literal',
  164. 'name': 'pop',
  165. 'x-verbatim-property': Utils_1.Utils.stringToUnicode('pop')
  166. }
  167. },
  168. 'arguments': []
  169. }
  170. }
  171. },
  172. {
  173. 'type': 'ExpressionStatement',
  174. 'expression': {
  175. 'type': 'CallExpression',
  176. 'callee': {
  177. 'type': 'MemberExpression',
  178. 'computed': true,
  179. 'object': {
  180. 'type': 'Identifier',
  181. 'name': 'array'
  182. },
  183. 'property': {
  184. 'type': 'Literal',
  185. 'name': 'unshift',
  186. 'x-verbatim-property': Utils_1.Utils.stringToUnicode('unshift')
  187. }
  188. },
  189. 'arguments': [
  190. {
  191. 'type': 'Identifier',
  192. 'name': 'temp'
  193. }
  194. ]
  195. }
  196. }
  197. ]
  198. },
  199. 'alternate': {
  200. 'type': 'BlockStatement',
  201. 'body': [
  202. {
  203. 'type': 'ExpressionStatement',
  204. 'expression': {
  205. 'type': 'AssignmentExpression',
  206. 'operator': '=',
  207. 'left': {
  208. 'type': 'Identifier',
  209. 'name': 'temp'
  210. },
  211. 'right': {
  212. 'type': 'CallExpression',
  213. 'callee': {
  214. 'type': 'MemberExpression',
  215. 'computed': true,
  216. 'object': {
  217. 'type': 'Identifier',
  218. 'name': 'array'
  219. },
  220. 'property': {
  221. 'type': 'Literal',
  222. 'name': 'shift',
  223. 'x-verbatim-property': Utils_1.Utils.stringToUnicode('shift')
  224. }
  225. },
  226. 'arguments': []
  227. }
  228. }
  229. },
  230. {
  231. 'type': 'ExpressionStatement',
  232. 'expression': {
  233. 'type': 'CallExpression',
  234. 'callee': {
  235. 'type': 'MemberExpression',
  236. 'computed': true,
  237. 'object': {
  238. 'type': 'Identifier',
  239. 'name': 'array'
  240. },
  241. 'property': {
  242. 'type': 'Literal',
  243. 'name': 'push',
  244. 'x-verbatim-property': Utils_1.Utils.stringToUnicode('push')
  245. }
  246. },
  247. 'arguments': [
  248. {
  249. 'type': 'Identifier',
  250. 'name': 'temp'
  251. }
  252. ]
  253. }
  254. }
  255. ]
  256. }
  257. }
  258. ]
  259. }
  260. }
  261. ]
  262. },
  263. 'generator': false,
  264. 'expression': false
  265. };
  266. }
  267. }
  268. exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;