DeadCodeInjectionIdentifiersTransformer.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as eslintScope from 'eslint-scope';
  4. import * as ESTree from 'estree';
  5. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  6. import { IIdentifierReplacer } from '../../interfaces/node-transformers/rename-identifiers-transformers/replacer/IIdentifierReplacer';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { IScopeIdentifiersTraverser } from '../../interfaces/node/IScopeIdentifiersTraverser';
  10. import { IScopeThroughIdentifiersTraverserCallbackData } from '../../interfaces/node/IScopeThroughIdentifiersTraverserCallbackData';
  11. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  12. import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
  13. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  14. import { NodeGuards } from '../../node/NodeGuards';
  15. /**
  16. * Renames all scope through identifiers for Dead Code Injection
  17. */
  18. @injectable()
  19. export class DeadCodeInjectionIdentifiersTransformer extends AbstractNodeTransformer {
  20. /**
  21. * @type {IIdentifierReplacer}
  22. */
  23. private readonly identifierReplacer: IIdentifierReplacer;
  24. /**
  25. * @type {IScopeIdentifiersTraverser}
  26. */
  27. private readonly scopeIdentifiersTraverser: IScopeIdentifiersTraverser;
  28. /**
  29. * @param {IIdentifierReplacer} identifierReplacer
  30. * @param {IRandomGenerator} randomGenerator
  31. * @param {IOptions} options
  32. * @param {IScopeIdentifiersTraverser} scopeIdentifiersTraverser
  33. */
  34. public constructor (
  35. @inject(ServiceIdentifiers.IIdentifierReplacer) identifierReplacer: IIdentifierReplacer,
  36. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  37. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  38. @inject(ServiceIdentifiers.IScopeIdentifiersTraverser) scopeIdentifiersTraverser: IScopeIdentifiersTraverser
  39. ) {
  40. super(randomGenerator, options);
  41. this.identifierReplacer = identifierReplacer;
  42. this.scopeIdentifiersTraverser = scopeIdentifiersTraverser;
  43. }
  44. /**
  45. * @param {NodeTransformationStage} nodeTransformationStage
  46. * @returns {IVisitor | null}
  47. */
  48. public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
  49. switch (nodeTransformationStage) {
  50. case NodeTransformationStage.RenameIdentifiers:
  51. return {
  52. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
  53. if (parentNode && NodeGuards.isProgramNode(node)) {
  54. return this.transformNode(node, parentNode);
  55. }
  56. }
  57. };
  58. default:
  59. return null;
  60. }
  61. }
  62. /**
  63. * @param {VariableDeclaration} programNode
  64. * @param {NodeGuards} parentNode
  65. * @returns {NodeGuards}
  66. */
  67. public transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node {
  68. this.scopeIdentifiersTraverser.traverseScopeThroughIdentifiers(
  69. programNode,
  70. parentNode,
  71. (data: IScopeThroughIdentifiersTraverserCallbackData) => {
  72. const {
  73. reference,
  74. variableLexicalScopeNode
  75. } = data;
  76. this.transformScopeThroughIdentifiers(reference, variableLexicalScopeNode);
  77. }
  78. );
  79. return programNode;
  80. }
  81. /**
  82. * @param {Reference} reference
  83. * @param {TNodeWithLexicalScope} lexicalScopeNode
  84. */
  85. private transformScopeThroughIdentifiers (
  86. reference: eslintScope.Reference,
  87. lexicalScopeNode: TNodeWithLexicalScope,
  88. ): void {
  89. if (reference.resolved) {
  90. return;
  91. }
  92. const identifier: ESTree.Identifier = reference.identifier;
  93. this.storeIdentifierName(identifier, lexicalScopeNode);
  94. this.replaceIdentifierName(identifier, lexicalScopeNode, reference);
  95. }
  96. /**
  97. * @param {Identifier} identifierNode
  98. * @param {TNodeWithLexicalScope} lexicalScopeNode
  99. */
  100. private storeIdentifierName (
  101. identifierNode: ESTree.Identifier,
  102. lexicalScopeNode: TNodeWithLexicalScope
  103. ): void {
  104. this.identifierReplacer.storeLocalName(identifierNode, lexicalScopeNode);
  105. }
  106. /**
  107. * @param {Identifier} identifierNode
  108. * @param {TNodeWithLexicalScope} lexicalScopeNode
  109. * @param {Variable} reference
  110. */
  111. private replaceIdentifierName (
  112. identifierNode: ESTree.Identifier,
  113. lexicalScopeNode: TNodeWithLexicalScope,
  114. reference: eslintScope.Reference
  115. ): void {
  116. const newIdentifier: ESTree.Identifier = this.identifierReplacer
  117. .replace(identifierNode, lexicalScopeNode);
  118. // rename of identifier
  119. reference.identifier.name = newIdentifier.name;
  120. }
  121. }