DebugProtectionFunctionIntervalNode.ts 2.9 KB

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