BlockStatementControlFlowTransformer.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TControlFlowCustomNodeFactory } from '../../types/container/custom-nodes/TControlFlowCustomNodeFactory';
  6. import { TStatement } from '../../types/node/TStatement';
  7. import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
  8. import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
  9. import { IOptions } from '../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  11. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  12. import { ControlFlowCustomNode } from '../../enums/custom-nodes/ControlFlowCustomNode';
  13. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  14. import { NodeGuards } from '../../node/NodeGuards';
  15. import { NodeUtils } from '../../node/NodeUtils';
  16. @injectable()
  17. export class BlockStatementControlFlowTransformer extends AbstractNodeTransformer {
  18. /**
  19. * @type {IArrayUtils}
  20. */
  21. private readonly arrayUtils: IArrayUtils;
  22. /**
  23. * @type {TControlFlowCustomNodeFactory}
  24. */
  25. private readonly controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory;
  26. /**
  27. * @param {TControlFlowCustomNodeFactory} controlFlowCustomNodeFactory
  28. * @param {IArrayUtils} arrayUtils
  29. * @param {IRandomGenerator} randomGenerator
  30. * @param {IOptions} options
  31. */
  32. constructor (
  33. @inject(ServiceIdentifiers.Factory__IControlFlowCustomNode)
  34. controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory,
  35. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  36. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  37. @inject(ServiceIdentifiers.IOptions) options: IOptions
  38. ) {
  39. super(randomGenerator, options);
  40. this.controlFlowCustomNodeFactory = controlFlowCustomNodeFactory;
  41. this.arrayUtils = arrayUtils;
  42. }
  43. /**
  44. * @param {BlockStatement} blockStatementNode
  45. * @returns {boolean}
  46. */
  47. private static blockStatementHasProhibitedStatements (blockStatementNode: ESTree.BlockStatement): boolean {
  48. return blockStatementNode.body.some((statement: ESTree.Statement) => {
  49. const isBreakOrContinueStatement: boolean = NodeGuards.isBreakStatementNode(statement)
  50. || NodeGuards.isContinueStatementNode(statement);
  51. const isVariableDeclarationWithLetOrConstKind: boolean = NodeGuards.isVariableDeclarationNode(statement)
  52. && (statement.kind === 'const' || statement.kind === 'let');
  53. return NodeGuards.isFunctionDeclarationNode(statement)
  54. || isBreakOrContinueStatement
  55. || isVariableDeclarationWithLetOrConstKind;
  56. });
  57. }
  58. /**
  59. * @param {BlockStatement} blockStatementNode
  60. * @returns {boolean}
  61. */
  62. private static canTransformBlockStatementNode (blockStatementNode: ESTree.BlockStatement): boolean {
  63. let canTransform: boolean = true;
  64. estraverse.traverse(blockStatementNode, {
  65. enter: (node: ESTree.Node): any => {
  66. if (NodeGuards.isWhileStatementNode(node)) {
  67. return estraverse.VisitorOption.Skip;
  68. }
  69. if (
  70. NodeGuards.isBlockStatementNode(node)
  71. && BlockStatementControlFlowTransformer.blockStatementHasProhibitedStatements(node)
  72. ) {
  73. canTransform = false;
  74. }
  75. }
  76. });
  77. if (blockStatementNode.body.length <= 4) {
  78. canTransform = false;
  79. }
  80. return canTransform;
  81. }
  82. /**
  83. * @return {IVisitor}
  84. */
  85. public getVisitor (): IVisitor {
  86. return {
  87. leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
  88. if (parentNode && NodeGuards.isBlockStatementNode(node)) {
  89. return this.transformNode(node, parentNode);
  90. }
  91. }
  92. };
  93. }
  94. /**
  95. * @param {BlockStatement} blockStatementNode
  96. * @param {NodeGuards} parentNode
  97. * @returns {NodeGuards}
  98. */
  99. public transformNode (blockStatementNode: ESTree.BlockStatement, parentNode: ESTree.Node): ESTree.Node {
  100. if (
  101. this.randomGenerator.getMathRandom() > this.options.controlFlowFlatteningThreshold ||
  102. !BlockStatementControlFlowTransformer.canTransformBlockStatementNode(blockStatementNode)
  103. ) {
  104. return blockStatementNode;
  105. }
  106. const blockStatementBody: ESTree.Statement[] = blockStatementNode.body;
  107. const originalKeys: number[] = this.arrayUtils.arrayRange(blockStatementBody.length);
  108. const shuffledKeys: number[] = this.arrayUtils.arrayShuffle(originalKeys);
  109. const originalKeysIndexesInShuffledArray: number[] = originalKeys.map((key: number) => shuffledKeys.indexOf(key));
  110. const blockStatementControlFlowFlatteningCustomNode: ICustomNode = this.controlFlowCustomNodeFactory(
  111. ControlFlowCustomNode.BlockStatementControlFlowFlatteningNode
  112. );
  113. blockStatementControlFlowFlatteningCustomNode.initialize(
  114. blockStatementBody,
  115. shuffledKeys,
  116. originalKeysIndexesInShuffledArray
  117. );
  118. const newBlockStatementNode: TStatement = blockStatementControlFlowFlatteningCustomNode.getNode()[0];
  119. NodeUtils.parentizeNode(newBlockStatementNode, parentNode);
  120. return newBlockStatementNode;
  121. }
  122. }