UnicodeArrayRotateFunctionCallNode.ts 2.6 KB

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