UnicodeArrayRotateFunctionNode.ts 14 KB

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