|
@@ -1,4 +1,4 @@
|
|
-import { injectable, inject } from 'inversify';
|
|
|
|
|
|
+import { inject, injectable, } from 'inversify';
|
|
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
|
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
|
|
|
|
|
import * as estraverse from 'estraverse';
|
|
import * as estraverse from 'estraverse';
|
|
@@ -63,8 +63,8 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
|
*/
|
|
*/
|
|
public getVisitor (): IVisitor {
|
|
public getVisitor (): IVisitor {
|
|
return {
|
|
return {
|
|
- enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
|
|
|
- if (NodeGuards.isFunctionDeclarationNode(node)) {
|
|
|
|
|
|
+ enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
|
|
|
+ if (parentNode && NodeGuards.isFunctionDeclarationNode(node)) {
|
|
return this.transformNode(node, parentNode);
|
|
return this.transformNode(node, parentNode);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -78,20 +78,20 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
|
*/
|
|
*/
|
|
public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
|
|
public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
|
|
const nodeIdentifier: number = this.nodeIdentifier++;
|
|
const nodeIdentifier: number = this.nodeIdentifier++;
|
|
- const blockScopeOfFunctionDeclarationNode: TNodeWithBlockStatement = NodeUtils
|
|
|
|
|
|
+ const blockScopeNode: TNodeWithBlockStatement = NodeUtils
|
|
.getBlockScopesOfNode(functionDeclarationNode)[0];
|
|
.getBlockScopesOfNode(functionDeclarationNode)[0];
|
|
|
|
|
|
- if (!this.options.renameGlobals && blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
|
|
|
|
|
|
+ if (!this.options.renameGlobals && blockScopeNode.type === NodeType.Program) {
|
|
return functionDeclarationNode;
|
|
return functionDeclarationNode;
|
|
}
|
|
}
|
|
|
|
|
|
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
|
|
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
|
|
|
|
|
|
// check for cached identifiers for current scope node. If exist - loop through them.
|
|
// check for cached identifiers for current scope node. If exist - loop through them.
|
|
- if (this.replaceableIdentifiers.has(blockScopeOfFunctionDeclarationNode)) {
|
|
|
|
- this.replaceScopeCachedIdentifiers(blockScopeOfFunctionDeclarationNode, nodeIdentifier);
|
|
|
|
|
|
+ if (this.replaceableIdentifiers.has(blockScopeNode)) {
|
|
|
|
+ this.replaceScopeCachedIdentifiers(blockScopeNode, nodeIdentifier);
|
|
} else {
|
|
} else {
|
|
- this.replaceScopeIdentifiers(blockScopeOfFunctionDeclarationNode, nodeIdentifier);
|
|
|
|
|
|
+ this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
|
|
}
|
|
}
|
|
|
|
|
|
return functionDeclarationNode;
|
|
return functionDeclarationNode;
|
|
@@ -106,30 +106,32 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * @param {NodeGuards} scopeNode
|
|
|
|
|
|
+ * @param {TNodeWithBlockStatement} blockScopeNode
|
|
* @param {number} nodeIdentifier
|
|
* @param {number} nodeIdentifier
|
|
*/
|
|
*/
|
|
- private replaceScopeCachedIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
|
|
|
- const cachedReplaceableIdentifiers: ESTree.Identifier[] = <ESTree.Identifier[]>this.replaceableIdentifiers.get(scopeNode);
|
|
|
|
|
|
+ private replaceScopeCachedIdentifiers (blockScopeNode: TNodeWithBlockStatement, nodeIdentifier: number): void {
|
|
|
|
+ const cachedReplaceableIdentifiers: ESTree.Identifier[] = <ESTree.Identifier[]>this.replaceableIdentifiers.get(blockScopeNode);
|
|
|
|
|
|
cachedReplaceableIdentifiers.forEach((replaceableIdentifier: ESTree.Identifier) => {
|
|
cachedReplaceableIdentifiers.forEach((replaceableIdentifier: ESTree.Identifier) => {
|
|
- const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(replaceableIdentifier.name, nodeIdentifier);
|
|
|
|
|
|
+ const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.
|
|
|
|
+ replace(replaceableIdentifier.name, nodeIdentifier);
|
|
|
|
|
|
replaceableIdentifier.name = newReplaceableIdentifier.name;
|
|
replaceableIdentifier.name = newReplaceableIdentifier.name;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * @param {NodeGuards} scopeNode
|
|
|
|
|
|
+ * @param {TNodeWithBlockStatement} blockScopeNode
|
|
* @param {number} nodeIdentifier
|
|
* @param {number} nodeIdentifier
|
|
*/
|
|
*/
|
|
- private replaceScopeIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
|
|
|
|
|
+ private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockStatement, nodeIdentifier: number): void {
|
|
const storedReplaceableIdentifiers: ESTree.Identifier[] = [];
|
|
const storedReplaceableIdentifiers: ESTree.Identifier[] = [];
|
|
|
|
|
|
- estraverse.replace(scopeNode, {
|
|
|
|
- enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
|
|
|
- if (NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
|
|
|
- const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(node.name, nodeIdentifier);
|
|
|
|
|
|
+ estraverse.replace(blockScopeNode, {
|
|
|
|
+ enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
|
|
|
+ if (parentNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
|
|
|
+ const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
|
|
|
|
+ .replace(node.name, nodeIdentifier);
|
|
const newIdentifierName: string = newIdentifier.name;
|
|
const newIdentifierName: string = newIdentifier.name;
|
|
|
|
|
|
if (node.name !== newIdentifierName) {
|
|
if (node.name !== newIdentifierName) {
|
|
@@ -141,6 +143,6 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
- this.replaceableIdentifiers.set(scopeNode, storedReplaceableIdentifiers);
|
|
|
|
|
|
+ this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiers);
|
|
}
|
|
}
|
|
}
|
|
}
|