DebugProtectionFunctionCallNode.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* tslint:disable:max-line-length */
  2. import * as estraverse from 'estraverse';
  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 DebugProtectionFunctionCallNode extends Node {
  8. /**
  9. * @type {ITreeNode}
  10. */
  11. protected node: ITreeNode;
  12. /**
  13. * @type {ITreeNode}
  14. */
  15. private astTree: ITreeNode;
  16. /**
  17. * @type {string}
  18. */
  19. private debugProtectionFunctionName: string;
  20. /**
  21. * @param astTree
  22. * @param debugProtectionFunctionName
  23. */
  24. constructor (
  25. astTree: ITreeNode,
  26. debugProtectionFunctionName: string
  27. ) {
  28. super();
  29. this.astTree = astTree;
  30. this.debugProtectionFunctionName = debugProtectionFunctionName;
  31. this.node = this.getNodeStructure();
  32. }
  33. public appendNode (): void {
  34. estraverse.replace(this.astTree, {
  35. leave: (node: ITreeNode, parent: ITreeNode): any => {
  36. if (NodeUtils.isProgramNode(node)) {
  37. node.body.push(this.getNode());
  38. return estraverse.VisitorOption.Break;
  39. }
  40. return estraverse.VisitorOption.Skip;
  41. }
  42. });
  43. }
  44. /**
  45. * @returns any
  46. */
  47. protected getNodeStructure (): any {
  48. return {
  49. 'type': NodeType.ExpressionStatement,
  50. 'expression': {
  51. 'type': NodeType.CallExpression,
  52. 'callee': {
  53. 'type': NodeType.Identifier,
  54. 'name': this.debugProtectionFunctionName
  55. },
  56. 'arguments': []
  57. }
  58. };
  59. }
  60. }