ScopeIdentifiersTransformer.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 * as estraverse from 'estraverse';
  6. import { TIdentifierObfuscatingReplacerFactory } from '../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory';
  7. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  8. import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
  9. import { IOptions } from '../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  11. import { IScopeIdentifiersTraverser } from '../../interfaces/node/IScopeIdentifiersTraverser';
  12. import { IScopeIdentifiersTraverserCallbackData } from '../../interfaces/node/IScopeIdentifiersTraverserCallbackData';
  13. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  14. import { IdentifierObfuscatingReplacer } from '../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer';
  15. import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
  16. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  17. import { NodeGuards } from '../../node/NodeGuards';
  18. import { NodeMetadata } from '../../node/NodeMetadata';
  19. /**
  20. * Replaces all replaceable identifiers in scope
  21. */
  22. @injectable()
  23. export class ScopeIdentifiersTransformer extends AbstractNodeTransformer {
  24. /**
  25. * @type {IIdentifierObfuscatingReplacer}
  26. */
  27. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  28. /**
  29. * @type {Map<TNodeWithLexicalScope, boolean>}
  30. */
  31. private readonly lexicalScopesWithObjectPatternWithoutDeclarationMap: Map<TNodeWithLexicalScope, boolean> = new Map();
  32. /**
  33. * @type {IScopeIdentifiersTraverser}
  34. */
  35. private readonly scopeIdentifiersTraverser: IScopeIdentifiersTraverser;
  36. /**
  37. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  38. * @param {IRandomGenerator} randomGenerator
  39. * @param {IOptions} options
  40. * @param {IScopeIdentifiersTraverser} scopeIdentifiersTraverser
  41. */
  42. public constructor (
  43. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  44. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  45. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  46. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  47. @inject(ServiceIdentifiers.IScopeIdentifiersTraverser) scopeIdentifiersTraverser: IScopeIdentifiersTraverser
  48. ) {
  49. super(randomGenerator, options);
  50. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  51. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  52. );
  53. this.scopeIdentifiersTraverser = scopeIdentifiersTraverser;
  54. }
  55. /**
  56. * @param {NodeTransformationStage} nodeTransformationStage
  57. * @returns {IVisitor | null}
  58. */
  59. public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
  60. switch (nodeTransformationStage) {
  61. case NodeTransformationStage.Obfuscating:
  62. return {
  63. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
  64. if (parentNode && NodeGuards.isProgramNode(node)) {
  65. return this.transformNode(node, parentNode);
  66. }
  67. }
  68. };
  69. default:
  70. return null;
  71. }
  72. }
  73. /**
  74. * @param {VariableDeclaration} programNode
  75. * @param {NodeGuards} parentNode
  76. * @returns {NodeGuards}
  77. */
  78. public transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node {
  79. this.scopeIdentifiersTraverser.traverse(
  80. programNode,
  81. parentNode,
  82. (data: IScopeIdentifiersTraverserCallbackData) => {
  83. const {
  84. isGlobalDeclaration,
  85. variable,
  86. variableLexicalScopeNode
  87. } = data;
  88. if (!this.options.renameGlobals && isGlobalDeclaration) {
  89. const isImportBindingOrCatchClauseIdentifier: boolean = variable.defs
  90. .every((definition: eslintScope.Definition) =>
  91. definition.type === 'ImportBinding'
  92. || definition.type === 'CatchClause'
  93. );
  94. // skip all global identifiers except import statement and catch clause parameter identifiers
  95. if (!isImportBindingOrCatchClauseIdentifier) {
  96. return;
  97. }
  98. }
  99. this.transformScopeVariableIdentifiers(
  100. variable,
  101. variableLexicalScopeNode,
  102. isGlobalDeclaration
  103. );
  104. }
  105. );
  106. return programNode;
  107. }
  108. /**
  109. * @param {Variable} variable
  110. * @param {TNodeWithLexicalScope} lexicalScopeNode
  111. * @param {boolean} isGlobalDeclaration
  112. */
  113. private transformScopeVariableIdentifiers (
  114. variable: eslintScope.Variable,
  115. lexicalScopeNode: TNodeWithLexicalScope,
  116. isGlobalDeclaration: boolean
  117. ): void {
  118. const firstIdentifier: ESTree.Identifier | null = variable.identifiers[0] ?? null;
  119. if (!firstIdentifier) {
  120. return;
  121. }
  122. if (!this.isReplaceableIdentifierNode(firstIdentifier, lexicalScopeNode, variable)) {
  123. return;
  124. }
  125. this.storeIdentifierName(firstIdentifier, lexicalScopeNode, isGlobalDeclaration);
  126. this.replaceIdentifierName(firstIdentifier, lexicalScopeNode, variable);
  127. }
  128. /**
  129. * @param {Identifier} identifierNode
  130. * @param {TNodeWithLexicalScope} lexicalScopeNode
  131. * @param {boolean} isGlobalDeclaration
  132. */
  133. private storeIdentifierName (
  134. identifierNode: ESTree.Identifier,
  135. lexicalScopeNode: TNodeWithLexicalScope,
  136. isGlobalDeclaration: boolean
  137. ): void {
  138. if (isGlobalDeclaration) {
  139. this.identifierObfuscatingReplacer.storeGlobalName(identifierNode, lexicalScopeNode);
  140. } else {
  141. this.identifierObfuscatingReplacer.storeLocalName(identifierNode, lexicalScopeNode);
  142. }
  143. }
  144. /**
  145. * @param {Identifier} identifierNode
  146. * @param {TNodeWithLexicalScope} lexicalScopeNode
  147. * @param {Variable} variable
  148. */
  149. private replaceIdentifierName (
  150. identifierNode: ESTree.Identifier,
  151. lexicalScopeNode: TNodeWithLexicalScope,
  152. variable: eslintScope.Variable
  153. ): void {
  154. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  155. .replace(identifierNode, lexicalScopeNode);
  156. // rename of identifiers
  157. variable.identifiers.forEach((identifier: ESTree.Identifier) => {
  158. identifier.name = newIdentifier.name;
  159. });
  160. // rename of references
  161. variable.references.forEach((reference: eslintScope.Reference) => {
  162. reference.identifier.name = identifierNode.name;
  163. });
  164. }
  165. /**
  166. * @param {Identifier} identifierNode
  167. * @param {TNodeWithLexicalScope} lexicalScopeNode
  168. * @param {Variable} variable
  169. * @returns {boolean}
  170. */
  171. private isReplaceableIdentifierNode (
  172. identifierNode: ESTree.Identifier,
  173. lexicalScopeNode: TNodeWithLexicalScope,
  174. variable: eslintScope.Variable
  175. ): identifierNode is ESTree.Identifier & { parentNode: ESTree.Node } {
  176. const parentNode: ESTree.Node | undefined = identifierNode.parentNode;
  177. return !!parentNode
  178. && !NodeMetadata.isIgnoredNode(identifierNode)
  179. && !this.isProhibitedPropertyNode(identifierNode, parentNode)
  180. && !this.isProhibitedClassDeclarationNameIdentifierNode(variable, identifierNode, parentNode)
  181. && !this.isProhibitedExportNamedClassDeclarationIdentifierNode(identifierNode, parentNode)
  182. && !this.isProhibitedExportNamedFunctionDeclarationIdentifierNode(identifierNode, parentNode)
  183. && !this.isProhibitedExportNamedVariableDeclarationIdentifierNode(identifierNode, parentNode)
  184. && !this.isProhibitedImportSpecifierNode(identifierNode, parentNode)
  185. && !this.isProhibitedVariableNameUsedInObjectPatternNode(variable, identifierNode, lexicalScopeNode)
  186. && !NodeGuards.isLabelIdentifierNode(identifierNode, parentNode);
  187. }
  188. /**
  189. * @param {Variable} variable
  190. * @param {Identifier} identifierNode
  191. * @param {Node} parentNode
  192. * @returns {identifierNode is Identifier}
  193. */
  194. private isProhibitedClassDeclarationNameIdentifierNode (
  195. variable: eslintScope.Variable,
  196. identifierNode: ESTree.Identifier,
  197. parentNode: ESTree.Node
  198. ): identifierNode is ESTree.Identifier {
  199. return NodeGuards.isClassDeclarationNode(variable.scope.block)
  200. && NodeGuards.isClassDeclarationNode(parentNode)
  201. && parentNode.id === identifierNode;
  202. }
  203. /**
  204. * @param {Identifier} identifierNode
  205. * @param {Node} parentNode
  206. * @returns {identifierNode is Identifier}
  207. */
  208. private isProhibitedExportNamedClassDeclarationIdentifierNode (
  209. identifierNode: ESTree.Identifier,
  210. parentNode: ESTree.Node
  211. ): identifierNode is ESTree.Identifier {
  212. return NodeGuards.isClassDeclarationNode(parentNode)
  213. && parentNode.id === identifierNode
  214. && !!parentNode.parentNode
  215. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode);
  216. }
  217. /**
  218. * @param {Identifier} identifierNode
  219. * @param {Node} parentNode
  220. * @returns {identifierNode is Identifier}
  221. */
  222. private isProhibitedExportNamedFunctionDeclarationIdentifierNode (
  223. identifierNode: ESTree.Identifier,
  224. parentNode: ESTree.Node
  225. ): identifierNode is ESTree.Identifier {
  226. return NodeGuards.isFunctionDeclarationNode(parentNode)
  227. && parentNode.id === identifierNode
  228. && !!parentNode.parentNode
  229. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode);
  230. }
  231. /**
  232. * @param {Identifier} identifierNode
  233. * @param {Node} parentNode
  234. * @returns {identifierNode is Identifier}
  235. */
  236. private isProhibitedExportNamedVariableDeclarationIdentifierNode (
  237. identifierNode: ESTree.Identifier,
  238. parentNode: ESTree.Node
  239. ): identifierNode is ESTree.Identifier {
  240. return NodeGuards.isVariableDeclaratorNode(parentNode)
  241. && parentNode.id === identifierNode
  242. && !!parentNode.parentNode
  243. && NodeGuards.isVariableDeclarationNode(parentNode.parentNode)
  244. && !!parentNode.parentNode.parentNode
  245. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode.parentNode);
  246. }
  247. /**
  248. * @param {Identifier} identifierNode
  249. * @param {Node} parentNode
  250. * @returns {boolean}
  251. */
  252. private isProhibitedImportSpecifierNode (identifierNode: ESTree.Identifier, parentNode: ESTree.Node): boolean {
  253. return NodeGuards.isImportSpecifierNode(parentNode)
  254. && parentNode.imported.name === parentNode.local.name;
  255. }
  256. /**
  257. * @param {Node} node
  258. * @param {Node} parentNode
  259. * @returns {boolean}
  260. */
  261. private isProhibitedPropertyNode (node: ESTree.Node, parentNode: ESTree.Node): node is ESTree.Identifier {
  262. const isProhibitedPropertyIdentifier = NodeGuards.isPropertyNode(parentNode)
  263. && !parentNode.computed
  264. && parentNode.key === node;
  265. const isProhibitedPropertyAssignmentPatternIdentifier = NodeGuards.isAssignmentPatternNode(parentNode)
  266. && parentNode.left === node
  267. && !!parentNode.parentNode
  268. && NodeGuards.isPropertyNode(parentNode.parentNode)
  269. && parentNode.left === parentNode.parentNode.key;
  270. return isProhibitedPropertyIdentifier
  271. || isProhibitedPropertyAssignmentPatternIdentifier;
  272. }
  273. /**
  274. * Should not rename identifiers that used inside destructing assignment without declaration
  275. *
  276. * var a, b; // should not be renamed
  277. * ({a, b} = {a: 1, b: 2});
  278. *
  279. * @param {Variable} variable
  280. * @param {Identifier} identifierNode
  281. * @param {TNodeWithLexicalScope} lexicalScopeNode
  282. * @returns {boolean}
  283. */
  284. private isProhibitedVariableNameUsedInObjectPatternNode (
  285. variable: eslintScope.Variable,
  286. identifierNode: ESTree.Identifier,
  287. lexicalScopeNode: TNodeWithLexicalScope
  288. ): boolean {
  289. let isLexicalScopeHasObjectPatternWithoutDeclaration: boolean | undefined =
  290. this.lexicalScopesWithObjectPatternWithoutDeclarationMap.get(lexicalScopeNode);
  291. // lexical scope was traversed before and object pattern without declaration was not found
  292. if (isLexicalScopeHasObjectPatternWithoutDeclaration === false) {
  293. return false;
  294. }
  295. const hasVarDefinitions: boolean = variable.defs.some((definition: eslintScope.Definition) => (<any>definition).kind === 'var');
  296. if (!hasVarDefinitions) {
  297. return false;
  298. }
  299. let isProhibitedVariableDeclaration: boolean = false;
  300. estraverse.traverse(lexicalScopeNode, {
  301. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
  302. if (
  303. NodeGuards.isObjectPatternNode(node)
  304. && parentNode
  305. && NodeGuards.isAssignmentExpressionNode(parentNode)
  306. ) {
  307. isLexicalScopeHasObjectPatternWithoutDeclaration = true;
  308. const properties: (ESTree.Property | ESTree.RestElement)[] = node.properties;
  309. for (const property of properties) {
  310. if (!NodeGuards.isPropertyNode(property)) {
  311. continue;
  312. }
  313. if (property.computed || !property.shorthand) {
  314. continue;
  315. }
  316. if (!NodeGuards.isIdentifierNode(property.key)) {
  317. continue;
  318. }
  319. if (identifierNode.name !== property.key.name) {
  320. continue;
  321. }
  322. isProhibitedVariableDeclaration = true;
  323. return estraverse.VisitorOption.Break;
  324. }
  325. }
  326. }
  327. });
  328. this.lexicalScopesWithObjectPatternWithoutDeclarationMap.set(
  329. lexicalScopeNode,
  330. isLexicalScopeHasObjectPatternWithoutDeclaration ?? false
  331. );
  332. return isProhibitedVariableDeclaration;
  333. }
  334. }