VariablePreserveTransformer.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { inject, injectable, } from 'inversify';
  2. import * as ESTree from 'estree';
  3. import * as eslintScope from 'eslint-scope';
  4. import { TIdentifierObfuscatingReplacerFactory } from '../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory';
  5. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  6. import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  10. import { IScopeIdentifiersTraverser } from '../../interfaces/node/IScopeIdentifiersTraverser';
  11. import { IScopeIdentifiersTraverserCallbackData } from '../../interfaces/node/IScopeIdentifiersTraverserCallbackData';
  12. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  13. import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
  14. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  15. import { IdentifierObfuscatingReplacer } from '../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer';
  16. import { NodeGuards } from '../../node/NodeGuards';
  17. /**
  18. * Preserve non-replaceable variables
  19. */
  20. @injectable()
  21. export class VariablePreserveTransformer extends AbstractNodeTransformer {
  22. /**
  23. * @type {IIdentifierObfuscatingReplacer}
  24. */
  25. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  26. /**
  27. * @type {IScopeIdentifiersTraverser}
  28. */
  29. private readonly scopeIdentifiersTraverser: IScopeIdentifiersTraverser;
  30. /**
  31. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  32. * @param {IRandomGenerator} randomGenerator
  33. * @param {IOptions} options
  34. * @param {IScopeIdentifiersTraverser} scopeIdentifiersTraverser
  35. */
  36. public constructor (
  37. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  38. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  39. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  40. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  41. @inject(ServiceIdentifiers.IScopeIdentifiersTraverser) scopeIdentifiersTraverser: IScopeIdentifiersTraverser
  42. ) {
  43. super(randomGenerator, options);
  44. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  45. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  46. );
  47. this.scopeIdentifiersTraverser = scopeIdentifiersTraverser;
  48. }
  49. /**
  50. * @param {TransformationStage} transformationStage
  51. * @returns {IVisitor | null}
  52. */
  53. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  54. switch (transformationStage) {
  55. case TransformationStage.Preparing:
  56. return {
  57. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
  58. if (parentNode && NodeGuards.isProgramNode(node)) {
  59. return this.transformNode(node, parentNode);
  60. }
  61. }
  62. };
  63. default:
  64. return null;
  65. }
  66. }
  67. /**
  68. * @param {VariableDeclaration} programNode
  69. * @param {NodeGuards} parentNode
  70. * @returns {NodeGuards}
  71. */
  72. public transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node {
  73. this.scopeIdentifiersTraverser.traverse(
  74. programNode,
  75. parentNode,
  76. (data: IScopeIdentifiersTraverserCallbackData) => {
  77. const {
  78. isGlobalDeclaration,
  79. variable,
  80. variableScope
  81. } = data;
  82. this.preserveScopeVariableIdentifiers(
  83. variable,
  84. variableScope,
  85. isGlobalDeclaration
  86. );
  87. }
  88. );
  89. return programNode;
  90. }
  91. /**
  92. * @param {Variable} variable
  93. * @param {Scope} variableScope
  94. * @param {boolean} isGlobalDeclaration
  95. */
  96. private preserveScopeVariableIdentifiers (
  97. variable: eslintScope.Variable,
  98. variableScope: eslintScope.Scope,
  99. isGlobalDeclaration: boolean
  100. ): void {
  101. for (const identifier of variable.identifiers) {
  102. if (isGlobalDeclaration) {
  103. this.preserveIdentifierNameForRootLexicalScope(identifier);
  104. } else {
  105. this.preserveIdentifierNameForLexicalScope(identifier, variableScope);
  106. }
  107. }
  108. }
  109. /**
  110. * @param {Identifier} identifierNode
  111. */
  112. private preserveIdentifierNameForRootLexicalScope (identifierNode: ESTree.Identifier): void {
  113. this.identifierObfuscatingReplacer.preserveName(identifierNode);
  114. }
  115. /**
  116. * @param {Identifier} identifierNode
  117. * @param {Scope} variableScope
  118. */
  119. private preserveIdentifierNameForLexicalScope (
  120. identifierNode: ESTree.Identifier,
  121. variableScope: eslintScope.Scope
  122. ): void {
  123. const lexicalScopeNode: TNodeWithLexicalScope | null = NodeGuards.isNodeWithLexicalScope(variableScope.block)
  124. ? variableScope.block
  125. : null;
  126. if (!lexicalScopeNode) {
  127. return;
  128. }
  129. this.identifierObfuscatingReplacer.preserveNameForLexicalScope(identifierNode, lexicalScopeNode);
  130. }
  131. }