ScopeIdentifiersTransformer.ts 15 KB

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