mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Update to typescript 2.6.0
This commit is contained in:
+2
-1
@@ -49,9 +49,10 @@ export class FunctionExpressionCalleeDataExtractor extends AbstractCalleeDataExt
|
||||
let calleeBlockStatement: ESTree.BlockStatement | null = null;
|
||||
|
||||
estraverse.traverse(targetNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
||||
if (
|
||||
NodeGuards.isFunctionExpressionNode(node) &&
|
||||
parentNode &&
|
||||
NodeGuards.isVariableDeclaratorNode(parentNode) &&
|
||||
NodeGuards.isIdentifierNode(parentNode.id) &&
|
||||
parentNode.id.name === name
|
||||
|
||||
+1
-1
@@ -101,7 +101,7 @@ export class ObjectExpressionCalleeDataExtractor extends AbstractCalleeDataExtra
|
||||
let calleeBlockStatement: ESTree.BlockStatement | null = null;
|
||||
|
||||
estraverse.traverse(targetNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
enter: (node: ESTree.Node): any => {
|
||||
if (
|
||||
NodeGuards.isVariableDeclaratorNode(node) &&
|
||||
NodeGuards.isIdentifierNode(node.id) &&
|
||||
|
||||
@@ -5,7 +5,6 @@ import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCusto
|
||||
import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
|
||||
import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
|
||||
@@ -36,27 +35,19 @@ export class ConsoleOutputCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
*/
|
||||
private readonly customNodeFactory: TCustomNodeFactory;
|
||||
|
||||
/**
|
||||
* @type {IObfuscationEventEmitter}
|
||||
*/
|
||||
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeFactory} customNodeFactory
|
||||
* @param {IObfuscationEventEmitter} obfuscationEventEmitter
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
|
||||
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.customNodeFactory = customNodeFactory;
|
||||
this.obfuscationEventEmitter = obfuscationEventEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ export class ControlFlowStorageNode extends AbstractCustomNode {
|
||||
Nodes.getIdentifierNode(this.controlFlowStorage.getStorageId()),
|
||||
Nodes.getObjectExpressionNode(
|
||||
Array
|
||||
.from(this.controlFlowStorage.getStorage())
|
||||
.from<[string, ICustomNode]>(this.controlFlowStorage.getStorage())
|
||||
.map(([key, value]: [string, ICustomNode]) => {
|
||||
return Nodes.getPropertyNode(
|
||||
Nodes.getIdentifierNode(key),
|
||||
|
||||
@@ -5,7 +5,6 @@ import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCusto
|
||||
import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
|
||||
import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
|
||||
@@ -36,27 +35,19 @@ export class DebugProtectionCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
*/
|
||||
private readonly customNodeFactory: TCustomNodeFactory;
|
||||
|
||||
/**
|
||||
* @type {IObfuscationEventEmitter}
|
||||
*/
|
||||
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeFactory} customNodeFactory
|
||||
* @param {IObfuscationEventEmitter} obfuscationEventEmitter
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
|
||||
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.customNodeFactory = customNodeFactory;
|
||||
this.obfuscationEventEmitter = obfuscationEventEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@ import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCusto
|
||||
import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
|
||||
import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
|
||||
@@ -36,27 +35,19 @@ export class DomainLockCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
*/
|
||||
private readonly customNodeFactory: TCustomNodeFactory;
|
||||
|
||||
/**
|
||||
* @type {IObfuscationEventEmitter}
|
||||
*/
|
||||
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeFactory} customNodeFactory
|
||||
* @param {IObfuscationEventEmitter} obfuscationEventEmitter
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
|
||||
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.customNodeFactory = customNodeFactory;
|
||||
this.obfuscationEventEmitter = obfuscationEventEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@ import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCusto
|
||||
import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
|
||||
import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
|
||||
@@ -36,27 +35,19 @@ export class SelfDefendingCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
*/
|
||||
private readonly customNodeFactory: TCustomNodeFactory;
|
||||
|
||||
/**
|
||||
* @type {IObfuscationEventEmitter}
|
||||
*/
|
||||
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeFactory} customNodeFactory
|
||||
* @param {IObfuscationEventEmitter} obfuscationEventEmitter
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
|
||||
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.customNodeFactory = customNodeFactory;
|
||||
this.obfuscationEventEmitter = obfuscationEventEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ import { TStatement } from '../../types/node/TStatement';
|
||||
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
import { IStorage } from '../../interfaces/storages/IStorage';
|
||||
|
||||
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
|
||||
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
|
||||
@@ -36,12 +35,6 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
|
||||
*/
|
||||
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
|
||||
|
||||
/**
|
||||
* @type {IStorage <string>}
|
||||
*/
|
||||
@initializable()
|
||||
private stringArrayStorage: IStorage <string>;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
@@ -70,16 +63,13 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {IStorage<string>} stringArrayStorage
|
||||
* @param {string} stringArrayName
|
||||
* @param {string} stringArrayCallsWrapperName
|
||||
*/
|
||||
public initialize (
|
||||
stringArrayStorage: IStorage <string>,
|
||||
stringArrayName: string,
|
||||
stringArrayCallsWrapperName: string
|
||||
): void {
|
||||
this.stringArrayStorage = stringArrayStorage;
|
||||
this.stringArrayName = stringArrayName;
|
||||
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import { TStatement } from '../../types/node/TStatement';
|
||||
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
import { IStorage } from '../../interfaces/storages/IStorage';
|
||||
|
||||
import { initializable } from '../../decorators/Initializable';
|
||||
|
||||
@@ -29,12 +28,6 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
|
||||
*/
|
||||
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
|
||||
|
||||
/**
|
||||
* @type {IStorage <string>}
|
||||
*/
|
||||
@initializable()
|
||||
private stringArrayStorage: IStorage <string>;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
@@ -63,16 +56,13 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {IStorage<string>} stringArrayStorage
|
||||
* @param {string} stringArrayName
|
||||
* @param {number} stringArrayRotateValue
|
||||
*/
|
||||
public initialize (
|
||||
stringArrayStorage: IStorage <string>,
|
||||
stringArrayName: string,
|
||||
stringArrayRotateValue: number
|
||||
): void {
|
||||
this.stringArrayStorage = stringArrayStorage;
|
||||
this.stringArrayName = stringArrayName;
|
||||
this.stringArrayRotateValue = stringArrayRotateValue;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCusto
|
||||
import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
|
||||
|
||||
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
|
||||
import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
|
||||
@@ -38,11 +37,6 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
*/
|
||||
private readonly customNodeFactory: TCustomNodeFactory;
|
||||
|
||||
/**
|
||||
* @type {IObfuscationEventEmitter}
|
||||
*/
|
||||
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
|
||||
|
||||
/**
|
||||
* @type {IStorage <string>}
|
||||
*/
|
||||
@@ -51,14 +45,12 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeFactory} customNodeFactory
|
||||
* @param {IObfuscationEventEmitter} obfuscationEventEmitter
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IStorage<string>} stringArrayStorage
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
|
||||
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStorage<string>,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
@@ -66,7 +58,6 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.customNodeFactory = customNodeFactory;
|
||||
this.obfuscationEventEmitter = obfuscationEventEmitter;
|
||||
this.stringArrayStorage = stringArrayStorage;
|
||||
}
|
||||
|
||||
@@ -120,8 +111,8 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
|
||||
}
|
||||
|
||||
stringArrayNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
|
||||
stringArrayCallsWrapper.initialize(this.stringArrayStorage, stringArrayName, stringArrayCallsWrapperName);
|
||||
stringArrayRotateFunctionNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
|
||||
stringArrayCallsWrapper.initialize(stringArrayName, stringArrayCallsWrapperName);
|
||||
stringArrayRotateFunctionNode.initialize(stringArrayName, stringArrayRotateValue);
|
||||
|
||||
this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
|
||||
this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);
|
||||
|
||||
+4
-4
@@ -11,14 +11,14 @@ export interface INodeTransformer {
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @param {Node} parentNode
|
||||
* @param {Node | null} parentNode
|
||||
*/
|
||||
analyzeNode ? (node: ESTree.Node, parentNode: ESTree.Node): void;
|
||||
analyzeNode ? (node: ESTree.Node, parentNode: ESTree.Node | null): void;
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @param {Node} parentNode
|
||||
* @param {Node | null} parentNode
|
||||
* @returns {Node | VisitorOption}
|
||||
*/
|
||||
transformNode (node: ESTree.Node, parentNode: ESTree.Node): ESTree.Node | estraverse.VisitorOption;
|
||||
transformNode (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | estraverse.VisitorOption;
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
import * as estraverse from 'estraverse';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
export interface IVisitor {
|
||||
enter?: (node: ESTree.Node, parentNode: ESTree.Node | null) => ESTree.Node | estraverse.VisitorOption | void;
|
||||
leave?: (node: ESTree.Node, parentNode: ESTree.Node | null) => ESTree.Node | estraverse.VisitorOption | void;
|
||||
export interface IVisitor <T extends ESTree.Node = ESTree.Node> {
|
||||
enter?: (node: T, parentNode: ESTree.Node | null) => ESTree.Node | estraverse.VisitorOption | void;
|
||||
leave?: (node: T, parentNode: ESTree.Node | null) => ESTree.Node | estraverse.VisitorOption | void;
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import { IObfuscatingReplacer } from './IObfuscatingReplacer';
|
||||
export interface IIdentifierObfuscatingReplacer extends IObfuscatingReplacer <ESTree.Identifier> {
|
||||
/**
|
||||
* @param nodeValue
|
||||
* @param nodeIdentifier
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
storeNames (nodeValue: any, nodeIdentifier: number): void;
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,7 +3,8 @@ import * as ESTree from 'estree';
|
||||
export interface IObfuscatingReplacer <T = ESTree.Node> {
|
||||
/**
|
||||
* @param nodeValue
|
||||
* @param nodeIdentifier
|
||||
* @param {number} nodeIdentifier
|
||||
* @returns {T}
|
||||
*/
|
||||
replace (nodeValue: any, nodeIdentifier?: number): T;
|
||||
}
|
||||
|
||||
@@ -81,10 +81,10 @@ export class TransformersRunner implements ITransformersRunner {
|
||||
const visitorsLength: number = visitors.length;
|
||||
|
||||
if (!visitorsLength) {
|
||||
return (node: ESTree.Node, parentNode: ESTree.Node) => node;
|
||||
return (node: ESTree.Node, parentNode: ESTree.Node | null) => node;
|
||||
}
|
||||
|
||||
return (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
return (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (node.ignoredNode) {
|
||||
return estraverse.VisitorOption.Skip;
|
||||
}
|
||||
|
||||
+2
-2
@@ -96,8 +96,8 @@ export class BlockStatementControlFlowTransformer extends AbstractNodeTransforme
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isBlockStatementNode(node)) {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isBlockStatementNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,11 +105,13 @@ export class FunctionControlFlowTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (
|
||||
NodeGuards.isFunctionDeclarationNode(node) ||
|
||||
NodeGuards.isFunctionExpressionNode(node) ||
|
||||
NodeGuards.isArrowFunctionExpressionNode(node)
|
||||
parentNode && (
|
||||
NodeGuards.isFunctionDeclarationNode(node) ||
|
||||
NodeGuards.isFunctionExpressionNode(node) ||
|
||||
NodeGuards.isArrowFunctionExpressionNode(node)
|
||||
)
|
||||
) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
@@ -212,8 +214,8 @@ export class FunctionControlFlowTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
private transformFunctionBody (functionNodeBody: ESTree.BlockStatement, controlFlowStorage: IStorage<ICustomNode>): void {
|
||||
estraverse.replace(functionNodeBody, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (this.isVisitedFunctionNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
||||
if (this.isVisitedFunctionNode(node) || !parentNode) {
|
||||
return estraverse.VisitorOption.Skip;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ export class MemberExpressionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isMemberExpressionNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isMemberExpressionNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,8 +44,8 @@ export class MethodDefinitionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isMethodDefinitionNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isMethodDefinitionNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isTemplateLiteralNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isTemplateLiteralNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
+11
-10
@@ -55,15 +55,15 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isProgramNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isProgramNode(node)) {
|
||||
this.analyzeNode(node, parentNode);
|
||||
|
||||
return node;
|
||||
}
|
||||
},
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isBlockStatementNode(node)) {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isBlockStatementNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -91,10 +91,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
* @param {NodeGuards} parentNode
|
||||
* @returns {NodeGuards | VisitorOption}
|
||||
*/
|
||||
public transformNode (
|
||||
blockStatementNode: ESTree.BlockStatement,
|
||||
parentNode: ESTree.Node
|
||||
): ESTree.Node | estraverse.VisitorOption {
|
||||
public transformNode (blockStatementNode: ESTree.BlockStatement, parentNode: ESTree.Node): ESTree.Node | estraverse.VisitorOption {
|
||||
if (this.collectedBlockStatementsLength < DeadCodeInjectionTransformer.minCollectedBlockStatementsCount) {
|
||||
return estraverse.VisitorOption.Break;
|
||||
}
|
||||
@@ -140,7 +137,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
isValidBlockStatementNode: boolean = true;
|
||||
|
||||
estraverse.replace(clonedBlockStatementNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
||||
/**
|
||||
* First step: count nested block statements in current block statement
|
||||
*/
|
||||
@@ -166,7 +163,11 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|
||||
* Second step: rename all identifiers (except identifiers in member expressions)
|
||||
* in current block statement
|
||||
*/
|
||||
if (NodeGuards.isIdentifierNode(node) && !NodeGuards.isMemberExpressionNode(parentNode)) {
|
||||
if (
|
||||
NodeGuards.isIdentifierNode(node) &&
|
||||
parentNode &&
|
||||
!NodeGuards.isMemberExpressionNode(parentNode)
|
||||
) {
|
||||
node.name = this.randomGenerator.getRandomVariableName(6);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ export class CatchClauseTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isCatchClauseNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isCatchClauseNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -92,9 +92,10 @@ export class CatchClauseTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
|
||||
estraverse.replace(catchClauseNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(node.name, nodeIdentifier);
|
||||
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;
|
||||
|
||||
if (node.name !== newIdentifierName) {
|
||||
|
||||
@@ -63,8 +63,8 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isClassDeclarationNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isClassDeclarationNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -78,20 +78,20 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public transformNode (classDeclarationNode: ESTree.ClassDeclaration, parentNode: ESTree.Node): ESTree.Node {
|
||||
const nodeIdentifier: number = this.nodeIdentifier++;
|
||||
const blockScopeOfClassDeclarationNode: TNodeWithBlockStatement = NodeUtils
|
||||
const blockScopeNode: TNodeWithBlockStatement = NodeUtils
|
||||
.getBlockScopesOfNode(classDeclarationNode)[0];
|
||||
|
||||
if (!this.options.renameGlobals && blockScopeOfClassDeclarationNode.type === NodeType.Program) {
|
||||
if (!this.options.renameGlobals && blockScopeNode.type === NodeType.Program) {
|
||||
return classDeclarationNode;
|
||||
}
|
||||
|
||||
this.storeClassName(classDeclarationNode, nodeIdentifier);
|
||||
|
||||
// check for cached identifiers for current scope node. If exist - loop through them.
|
||||
if (this.replaceableIdentifiers.has(blockScopeOfClassDeclarationNode)) {
|
||||
this.replaceScopeCachedIdentifiers(blockScopeOfClassDeclarationNode, nodeIdentifier);
|
||||
if (this.replaceableIdentifiers.has(blockScopeNode)) {
|
||||
this.replaceScopeCachedIdentifiers(blockScopeNode, nodeIdentifier);
|
||||
} else {
|
||||
this.replaceScopeIdentifiers(blockScopeOfClassDeclarationNode, nodeIdentifier);
|
||||
this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
|
||||
}
|
||||
|
||||
return classDeclarationNode;
|
||||
@@ -106,30 +106,32 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {TNodeWithBlockStatement} blockScopeNode
|
||||
* @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) => {
|
||||
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(replaceableIdentifier.name, nodeIdentifier);
|
||||
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
|
||||
.replace(replaceableIdentifier.name, nodeIdentifier);
|
||||
|
||||
replaceableIdentifier.name = newReplaceableIdentifier.name;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {TNodeWithBlockStatement} blockScopeNode
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
private replaceScopeIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
||||
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockStatement, nodeIdentifier: number): void {
|
||||
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;
|
||||
|
||||
if (node.name !== newIdentifierName) {
|
||||
@@ -141,6 +143,6 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
});
|
||||
|
||||
this.replaceableIdentifiers.set(scopeNode, storedReplaceableIdentifiers);
|
||||
this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,8 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -78,20 +78,20 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
|
||||
const nodeIdentifier: number = this.nodeIdentifier++;
|
||||
const blockScopeOfFunctionDeclarationNode: TNodeWithBlockStatement = NodeUtils
|
||||
const blockScopeNode: TNodeWithBlockStatement = NodeUtils
|
||||
.getBlockScopesOfNode(functionDeclarationNode)[0];
|
||||
|
||||
if (!this.options.renameGlobals && blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
|
||||
if (!this.options.renameGlobals && blockScopeNode.type === NodeType.Program) {
|
||||
return functionDeclarationNode;
|
||||
}
|
||||
|
||||
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
|
||||
|
||||
// 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 {
|
||||
this.replaceScopeIdentifiers(blockScopeOfFunctionDeclarationNode, nodeIdentifier);
|
||||
this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
|
||||
}
|
||||
|
||||
return functionDeclarationNode;
|
||||
@@ -106,30 +106,32 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {TNodeWithBlockStatement} blockScopeNode
|
||||
* @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) => {
|
||||
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(replaceableIdentifier.name, nodeIdentifier);
|
||||
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.
|
||||
replace(replaceableIdentifier.name, nodeIdentifier);
|
||||
|
||||
replaceableIdentifier.name = newReplaceableIdentifier.name;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {TNodeWithBlockStatement} blockScopeNode
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
private replaceScopeIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
||||
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockStatement, nodeIdentifier: number): void {
|
||||
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;
|
||||
|
||||
if (node.name !== newIdentifierName) {
|
||||
@@ -141,6 +143,6 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
});
|
||||
|
||||
this.replaceableIdentifiers.set(scopeNode, storedReplaceableIdentifiers);
|
||||
this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,11 +54,13 @@ export class FunctionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (
|
||||
NodeGuards.isFunctionDeclarationNode(node) ||
|
||||
NodeGuards.isFunctionExpressionNode(node) ||
|
||||
NodeGuards.isArrowFunctionExpressionNode(node)
|
||||
parentNode && (
|
||||
NodeGuards.isFunctionDeclarationNode(node) ||
|
||||
NodeGuards.isFunctionExpressionNode(node) ||
|
||||
NodeGuards.isArrowFunctionExpressionNode(node)
|
||||
)
|
||||
) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
@@ -113,8 +115,8 @@ export class FunctionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
private replaceFunctionParams (functionNode: ESTree.Function, nodeIdentifier: number): void {
|
||||
const replaceVisitor: estraverse.Visitor = {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
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;
|
||||
|
||||
|
||||
@@ -62,8 +62,8 @@ export class LabeledStatementTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isLabeledStatementNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isLabeledStatementNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,8 @@ export class LabeledStatementTransformer extends AbstractNodeTransformer {
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
private storeLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement, nodeIdentifier: number): void {
|
||||
this.identifierObfuscatingReplacer.storeNames(labeledStatementNode.label.name, nodeIdentifier);
|
||||
this.identifierObfuscatingReplacer
|
||||
.storeNames(labeledStatementNode.label.name, nodeIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,9 +99,10 @@ export class LabeledStatementTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
private replaceLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement, nodeIdentifier: number): void {
|
||||
estraverse.replace(labeledStatementNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (NodeGuards.isLabelIdentifierNode(node, parentNode)) {
|
||||
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(node.name, nodeIdentifier);
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
||||
if (parentNode && NodeGuards.isLabelIdentifierNode(node, parentNode)) {
|
||||
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
|
||||
.replace(node.name, nodeIdentifier);
|
||||
|
||||
node.name = newIdentifier.name;
|
||||
}
|
||||
|
||||
@@ -42,8 +42,8 @@ export class LiteralTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isLiteralNode(node) && !node.obfuscatedNode) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isLiteralNode(node) && !node.obfuscatedNode) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ export class LiteralTransformer extends AbstractNodeTransformer {
|
||||
* @returns {NodeGuards}
|
||||
*/
|
||||
public transformNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): ESTree.Node {
|
||||
if (NodeGuards.isPropertyNode(parentNode) && parentNode.key === literalNode) {
|
||||
if (parentNode && NodeGuards.isPropertyNode(parentNode) && parentNode.key === literalNode) {
|
||||
return literalNode;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,8 @@ export class ObjectExpressionTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isObjectExpressionNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isObjectExpressionNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,8 +64,8 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
if (NodeGuards.isVariableDeclarationNode(node)) {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isVariableDeclarationNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
@@ -78,16 +78,16 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
|
||||
* @returns {NodeGuards}
|
||||
*/
|
||||
public transformNode (variableDeclarationNode: ESTree.VariableDeclaration, parentNode: ESTree.Node): ESTree.Node {
|
||||
const blockScopeOfVariableDeclarationNode: TNodeWithBlockStatement = NodeUtils
|
||||
const blockScopeNode: TNodeWithBlockStatement = NodeUtils
|
||||
.getBlockScopesOfNode(variableDeclarationNode)[0];
|
||||
|
||||
if (!this.options.renameGlobals && blockScopeOfVariableDeclarationNode.type === NodeType.Program) {
|
||||
if (!this.options.renameGlobals && blockScopeNode.type === NodeType.Program) {
|
||||
return variableDeclarationNode;
|
||||
}
|
||||
|
||||
const nodeIdentifier: number = this.nodeIdentifier++;
|
||||
const scopeNode: ESTree.Node = variableDeclarationNode.kind === 'var'
|
||||
? blockScopeOfVariableDeclarationNode
|
||||
? blockScopeNode
|
||||
: parentNode;
|
||||
|
||||
this.storeVariableNames(variableDeclarationNode, nodeIdentifier);
|
||||
@@ -113,36 +113,41 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
|
||||
return estraverse.VisitorOption.Skip;
|
||||
}
|
||||
|
||||
NodeUtils.typedTraverse(declarationNode.id, NodeType.Identifier, {
|
||||
enter: (node: ESTree.Identifier) => this.identifierObfuscatingReplacer.storeNames(node.name, nodeIdentifier)
|
||||
estraverse.traverse(declarationNode.id, {
|
||||
enter: (node: ESTree.Node) => {
|
||||
if (NodeGuards.isIdentifierNode(node)) {
|
||||
this.identifierObfuscatingReplacer.storeNames(node.name, nodeIdentifier);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {Node} scopeNode
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
private replaceScopeCachedIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
||||
const cachedReplaceableIdentifiers: ESTree.Identifier[] = <ESTree.Identifier[]>this.replaceableIdentifiers.get(scopeNode);
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} scopeNode
|
||||
* @param {NodeGuards} blockScopeNode
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
private replaceScopeIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
|
||||
private replaceScopeIdentifiers (blockScopeNode: ESTree.Node, nodeIdentifier: number): void {
|
||||
const storedReplaceableIdentifiers: ESTree.Identifier[] = [];
|
||||
|
||||
estraverse.replace(scopeNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (!node.obfuscatedNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
estraverse.replace(blockScopeNode, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
|
||||
if (parentNode && !node.obfuscatedNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer.replace(node.name, nodeIdentifier);
|
||||
const newIdentifierName: string = newIdentifier.name;
|
||||
|
||||
@@ -155,6 +160,6 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
|
||||
}
|
||||
});
|
||||
|
||||
this.replaceableIdentifiers.set(scopeNode, storedReplaceableIdentifiers);
|
||||
this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiers);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -58,9 +58,11 @@ export class BaseIdentifierObfuscatingReplacer extends AbstractObfuscatingReplac
|
||||
* @param {number} nodeIdentifier
|
||||
*/
|
||||
public storeNames (nodeName: string, nodeIdentifier: number): void {
|
||||
if (!this.isReservedName(nodeName)) {
|
||||
this.namesMap.set(`${nodeName}-${String(nodeIdentifier)}`, this.randomGenerator.getRandomVariableName(6));
|
||||
if (this.isReservedName(nodeName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.namesMap.set(`${nodeName}-${String(nodeIdentifier)}`, this.randomGenerator.getRandomVariableName(6));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
-9
@@ -4,7 +4,6 @@ import { ServiceIdentifiers } from '../../../../container/ServiceIdentifiers';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { ICryptUtils } from '../../../../interfaces/utils/ICryptUtils';
|
||||
import { ICustomNodeGroup } from '../../../../interfaces/custom-nodes/ICustomNodeGroup';
|
||||
import { IEncodedValue } from '../../../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/literal-obfuscating-replacers/IEncodedValue';
|
||||
import { IEscapeSequenceEncoder } from '../../../../interfaces/utils/IEscapeSequenceEncoder';
|
||||
import { IOptions } from '../../../../interfaces/options/IOptions';
|
||||
@@ -40,11 +39,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
|
||||
*/
|
||||
private readonly cryptUtils: ICryptUtils;
|
||||
|
||||
/**
|
||||
* @type {IStorage<ICustomNodeGroup>}
|
||||
*/
|
||||
private readonly customNodeGroupStorage: IStorage<ICustomNodeGroup>;
|
||||
|
||||
/**
|
||||
* @type {IEscapeSequenceEncoder}
|
||||
*/
|
||||
@@ -76,7 +70,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
|
||||
private readonly stringArrayStorage: IStorage<string>;
|
||||
|
||||
/**
|
||||
* @param {IStorage<ICustomNodeGroup>} customNodeGroupStorage
|
||||
* @param {IStorage<string>} stringArrayStorage
|
||||
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
@@ -84,7 +77,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.TCustomNodeGroupStorage) customNodeGroupStorage: IStorage<ICustomNodeGroup>,
|
||||
@inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStorage<string>,
|
||||
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@@ -95,7 +87,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
|
||||
options
|
||||
);
|
||||
|
||||
this.customNodeGroupStorage = customNodeGroupStorage;
|
||||
this.stringArrayStorage = stringArrayStorage;
|
||||
this.escapeSequenceEncoder = escapeSequenceEncoder;
|
||||
this.randomGenerator = randomGenerator;
|
||||
|
||||
@@ -52,7 +52,7 @@ export class ObfuscatingGuardsTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
};
|
||||
@@ -63,7 +63,7 @@ export class ObfuscatingGuardsTransformer extends AbstractNodeTransformer {
|
||||
* @param {Node} parentNode
|
||||
* @returns {Node}
|
||||
*/
|
||||
public transformNode (node: ESTree.Node, parentNode: ESTree.Node): ESTree.Node {
|
||||
public transformNode (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node {
|
||||
const obfuscationAllowed: boolean = this.obfuscatingGuards.every((nodeGuard: IObfuscatingGuard) => nodeGuard.check(node));
|
||||
|
||||
node.ignoredNode = !obfuscationAllowed;
|
||||
|
||||
@@ -31,7 +31,7 @@ export class ParentificationTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (): IVisitor {
|
||||
return {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
};
|
||||
@@ -42,7 +42,7 @@ export class ParentificationTransformer extends AbstractNodeTransformer {
|
||||
* @param {Node} parentNode
|
||||
* @returns {Node}
|
||||
*/
|
||||
public transformNode (node: ESTree.Node, parentNode: ESTree.Node): ESTree.Node {
|
||||
public transformNode (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node {
|
||||
return NodeUtils.parentizeNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-47
@@ -26,13 +26,15 @@ export class NodeUtils {
|
||||
* @param {T} astTree
|
||||
* @returns {T}
|
||||
*/
|
||||
public static addXVerbatimPropertyToLiterals <T extends ESTree.Node> (astTree: T): T {
|
||||
NodeUtils.typedReplace(astTree, NodeType.Literal, {
|
||||
leave: (literalNode: ESTree.Literal) => {
|
||||
literalNode['x-verbatim-property'] = {
|
||||
content : literalNode.raw,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
public static addXVerbatimPropertyToLiterals <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
|
||||
estraverse.replace(astTree, {
|
||||
leave: (node: ESTree.Node) => {
|
||||
if (NodeGuards.isLiteralNode(node)) {
|
||||
node['x-verbatim-property'] = {
|
||||
content: node.raw,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -213,49 +215,10 @@ export class NodeUtils {
|
||||
* @param {Node} parentNode
|
||||
* @returns {T}
|
||||
*/
|
||||
public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node): T {
|
||||
public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node | null): T {
|
||||
node.parentNode = parentNode || node;
|
||||
node.obfuscatedNode = false;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} astTree
|
||||
* @param {string} nodeType
|
||||
* @param {visitor} visitor
|
||||
*/
|
||||
public static typedReplace (
|
||||
astTree: ESTree.Node,
|
||||
nodeType: string,
|
||||
visitor: {enter?: (node: ESTree.Node) => void, leave?: (node: ESTree.Node) => void},
|
||||
): void {
|
||||
NodeUtils.typedTraverse(astTree, nodeType, visitor, 'replace');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeGuards} astTree
|
||||
* @param {string} nodeType
|
||||
* @param {Visitor} visitor
|
||||
* @param {string} traverseType
|
||||
*/
|
||||
public static typedTraverse (
|
||||
astTree: ESTree.Node,
|
||||
nodeType: string,
|
||||
visitor: estraverse.Visitor,
|
||||
traverseType: string = 'traverse'
|
||||
): void {
|
||||
(<any>estraverse)[traverseType](astTree, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (node.type === nodeType && visitor.enter) {
|
||||
return visitor.enter(node, parentNode);
|
||||
}
|
||||
},
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (node.type === nodeType && visitor.leave) {
|
||||
return visitor.leave(node, parentNode);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -343,7 +343,7 @@ export class Nodes {
|
||||
/**
|
||||
* @param {UnaryOperator} operator
|
||||
* @param {Expression} argument
|
||||
* @param {boolean} prefix
|
||||
* @param {true} prefix
|
||||
* @returns {UnaryExpression}
|
||||
*/
|
||||
public static getUnaryExpressionNode (
|
||||
|
||||
@@ -4,7 +4,6 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
import { TCustomNodeGroupFactory } from '../../types/container/custom-nodes/TCustomNodeGroupFactory';
|
||||
|
||||
import { ICustomNodeGroup } from '../../interfaces/custom-nodes/ICustomNodeGroup';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { CustomNodeGroup } from '../../enums/custom-nodes/CustomNodeGroup';
|
||||
@@ -29,25 +28,17 @@ export class CustomNodeGroupStorage extends MapStorage <ICustomNodeGroup> {
|
||||
*/
|
||||
private readonly customNodeGroupFactory: TCustomNodeGroupFactory;
|
||||
|
||||
/**
|
||||
* @type {IOptions}
|
||||
*/
|
||||
private readonly options: IOptions;
|
||||
|
||||
/**
|
||||
* @param {TCustomNodeGroupFactory} customNodeGroupFactory
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__ICustomNodeGroup) customNodeGroupFactory: TCustomNodeGroupFactory,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator);
|
||||
|
||||
this.customNodeGroupFactory = customNodeGroupFactory;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
|
||||
Reference in New Issue
Block a user