DirectivePlacementTransformer.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from '@javascript-obfuscator/estraverse';
  4. import * as ESTree from 'estree';
  5. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  6. import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  10. import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
  11. import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
  12. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  13. import { NodeAppender } from '../../node/NodeAppender';
  14. import { NodeGuards } from '../../node/NodeGuards';
  15. import { NodeUtils } from '../../node/NodeUtils';
  16. /**
  17. * It's easier to fix "use strict"; placement after obfuscation as a separate stage
  18. * than ignore this directive in other transformers like control flow and dead code injection transformers
  19. */
  20. @injectable()
  21. export class DirectivePlacementTransformer extends AbstractNodeTransformer {
  22. /**
  23. * @type {NodeTransformer[]}
  24. */
  25. public readonly runAfter: NodeTransformer[] = [
  26. NodeTransformer.CustomCodeHelpersTransformer
  27. ];
  28. /**
  29. * @type {WeakMap<TNodeWithLexicalScope, Directive>}
  30. */
  31. private readonly lexicalScopeDirectives: WeakMap<
  32. TNodeWithLexicalScope,
  33. ESTree.Directive
  34. > = new WeakMap<TNodeWithLexicalScope, ESTree.Directive>();
  35. /**
  36. * @param {IRandomGenerator} randomGenerator
  37. * @param {IOptions} options
  38. */
  39. public constructor (
  40. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  42. ) {
  43. super(randomGenerator, options);
  44. }
  45. /**
  46. * @param {NodeTransformationStage} nodeTransformationStage
  47. * @returns {IVisitor | null}
  48. */
  49. public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
  50. switch (nodeTransformationStage) {
  51. case NodeTransformationStage.Preparing:
  52. return {
  53. enter: (
  54. node: ESTree.Node,
  55. parentNode: ESTree.Node | null
  56. ): ESTree.Node | estraverse.VisitorOption | undefined => {
  57. if (parentNode && NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode)) {
  58. return this.analyzeNode(node, parentNode);
  59. }
  60. }
  61. };
  62. case NodeTransformationStage.Finalizing:
  63. return {
  64. enter: (
  65. node: ESTree.Node,
  66. parentNode: ESTree.Node | null
  67. ): ESTree.Node | estraverse.VisitorOption | undefined => {
  68. if (parentNode && NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode)) {
  69. return this.transformNode(node, parentNode);
  70. }
  71. }
  72. };
  73. default:
  74. return null;
  75. }
  76. }
  77. /**
  78. * @param {TNodeWithLexicalScopeStatements} nodeWithLexicalScopeStatements
  79. * @param {Node} parentNode
  80. * @returns {TNodeWithLexicalScopeStatements}
  81. */
  82. public analyzeNode (
  83. nodeWithLexicalScopeStatements: TNodeWithLexicalScopeStatements,
  84. parentNode: ESTree.Node
  85. ): TNodeWithLexicalScopeStatements {
  86. if (!NodeGuards.isNodeWithLexicalScope(parentNode)) {
  87. return nodeWithLexicalScopeStatements;
  88. }
  89. const firstStatementNode = nodeWithLexicalScopeStatements.body[0] ?? null;
  90. if (firstStatementNode && NodeGuards.isDirectiveNode(firstStatementNode)) {
  91. this.lexicalScopeDirectives.set(parentNode, firstStatementNode);
  92. }
  93. return nodeWithLexicalScopeStatements;
  94. }
  95. /**
  96. * @param {TNodeWithLexicalScopeStatements} nodeWithLexicalScopeStatements
  97. * @param {Node | null} parentNode
  98. * @returns {TNodeWithLexicalScope}
  99. */
  100. public transformNode (
  101. nodeWithLexicalScopeStatements: TNodeWithLexicalScopeStatements,
  102. parentNode: ESTree.Node
  103. ): TNodeWithLexicalScopeStatements {
  104. if (!NodeGuards.isNodeWithLexicalScope(parentNode)) {
  105. return nodeWithLexicalScopeStatements;
  106. }
  107. const directiveNode: ESTree.Directive | undefined = this.lexicalScopeDirectives.get(parentNode);
  108. if (directiveNode) {
  109. const newDirectiveNode: ESTree.Directive = NodeUtils.clone(directiveNode);
  110. // append new directive node at the top of lexical scope statements
  111. NodeAppender.prepend(nodeWithLexicalScopeStatements, [newDirectiveNode]);
  112. // remove found directive node
  113. let isDirectiveNodeRemoved: boolean = false;
  114. estraverse.replace(nodeWithLexicalScopeStatements, {
  115. enter: (node: ESTree.Node): estraverse.VisitorOption | undefined => {
  116. if (isDirectiveNodeRemoved) {
  117. return estraverse.VisitorOption.Break;
  118. }
  119. if (node === directiveNode) {
  120. isDirectiveNodeRemoved = true;
  121. return estraverse.VisitorOption.Remove;
  122. }
  123. }
  124. });
  125. }
  126. return nodeWithLexicalScopeStatements;
  127. }
  128. }