UnicodeArrayRotateFunctionCallNode.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as estraverse from 'estraverse';
  2. import { IProgramNode } from "../../interfaces/nodes/IProgramNode";
  3. import { ITreeNode } from "../../interfaces/nodes/ITreeNode";
  4. import { NodeType } from "../../enums/NodeType";
  5. import { Node } from '../Node';
  6. import { NodeUtils } from "../../NodeUtils";
  7. export class UnicodeArrayRotateFunctionCallNode extends Node {
  8. /**
  9. * @type {string}
  10. */
  11. private unicodeArrayName: string;
  12. /**
  13. * @type {number}
  14. */
  15. private unicodeArrayRotateValue: number;
  16. /**
  17. * @param {string}
  18. */
  19. private unicodeArrayRotateFunctionName: string;
  20. /**
  21. * @param astTree
  22. * @param unicodeArrayRotateFunctionName
  23. * @param unicodeArrayName
  24. * @param unicodeArrayRotateValue
  25. */
  26. constructor (
  27. astTree: ITreeNode,
  28. unicodeArrayRotateFunctionName: string,
  29. unicodeArrayName: string,
  30. unicodeArrayRotateValue: number
  31. ) {
  32. super();
  33. this.astTree = astTree;
  34. this.unicodeArrayRotateFunctionName = unicodeArrayRotateFunctionName;
  35. this.unicodeArrayName = unicodeArrayName;
  36. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  37. this.node = this.getNodeStructure();
  38. }
  39. public appendNode (): void {
  40. estraverse.replace(this.astTree, {
  41. leave: (node: ITreeNode, parent: ITreeNode): any => {
  42. if (NodeUtils.isProgramNode(node)) {
  43. NodeUtils.prependNode(node.body, this.getNode());
  44. return estraverse.VisitorOption.Break;
  45. }
  46. return estraverse.VisitorOption.Skip;
  47. }
  48. });
  49. }
  50. /**
  51. * @returns any
  52. */
  53. protected getNodeStructure (): any {
  54. return {
  55. 'type': NodeType.ExpressionStatement,
  56. 'expression': {
  57. 'type': NodeType.CallExpression,
  58. 'callee': {
  59. 'type': NodeType.Identifier,
  60. 'name': this.unicodeArrayRotateFunctionName
  61. },
  62. 'arguments': [
  63. {
  64. 'type': NodeType.Identifier,
  65. 'name': this.unicodeArrayName
  66. },
  67. {
  68. 'type': NodeType.Literal,
  69. 'value': this.unicodeArrayRotateValue,
  70. 'raw': `'${this.unicodeArrayRotateValue}'`
  71. },
  72. {
  73. 'type': NodeType.Literal,
  74. 'value': true,
  75. 'raw': 'true'
  76. }
  77. ]
  78. }
  79. };
  80. }
  81. }