trying to increase performance

This commit is contained in:
sanex3339
2016-12-20 19:02:17 +03:00
parent 55f0601bae
commit e95602eeb2
25 changed files with 1696 additions and 1312 deletions
+1303 -1077
View File
File diff suppressed because it is too large Load Diff
+21 -10
View File
@@ -9,6 +9,16 @@ import { NodeUtils } from '../node/NodeUtils';
@injectable()
export abstract class AbstractCustomNode implements ICustomNode {
/**
* @type {string}
*/
protected cachedCode: string;
/**
* @type {TStatement[]}
*/
protected cachedNode: TStatement[];
/**
* @type {IOptions}
*/
@@ -32,25 +42,26 @@ export abstract class AbstractCustomNode implements ICustomNode {
* @returns {string}
*/
public getCode (): string {
return NodeUtils.convertStructureToCode(this.getNode());
if (!this.cachedCode) {
this.cachedCode = NodeUtils.convertStructureToCode(this.getNode());
}
return this.cachedCode;
}
/**
* @returns {TStatement[]}
*/
public getNode (): TStatement[] {
return this.getNodeStructure();
if (!this.cachedNode) {
this.cachedNode = this.getNodeStructure();
}
return this.cachedNode;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
protected abstract getTemplate (): string;
protected abstract getNodeStructure (): TStatement[];
}
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { ConsoleOutputDisableExpressionTemplate } from '../../templates/custom-nodes/console-output-nodes/console-output-disable-expression-node/ConsoleOutputDisableExpressionTemplate';
@@ -10,6 +12,7 @@ import { ConsoleOutputDisableExpressionTemplate } from '../../templates/custom-n
import { initializable } from '../../decorators/Initializable';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
@@ -36,6 +39,13 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
this.callsControllerFunctionName = callsControllerFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -1,15 +1,14 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../../types/node/TStatement';
import { IOptions } from '../../../interfaces/options/IOptions';
import { initializable } from '../../../decorators/Initializable';
import { BinaryExpressionFunctionTemplate } from '../../../templates/custom-nodes/control-flow-replacers-nodes/binary-expression-control-flow-replacer-nodes/BinaryExpressionFunctionTemplate';
import { AbstractCustomNode } from '../../AbstractCustomNode';
import { Nodes } from '../../../node/Nodes';
import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
@injectable()
@@ -37,12 +36,26 @@ export class BinaryExpressionFunctionNode extends AbstractCustomNode {
}
/**
* @returns {string}
* @returns {TStatement[]}
*/
protected getTemplate (): string {
return format(BinaryExpressionFunctionTemplate(), {
functionName: RandomGeneratorUtils.getRandomVariableName(1),
operator: this.operator
});
protected getNodeStructure (): TStatement[] {
return [
Nodes.getFunctionDeclarationNode(
RandomGeneratorUtils.getRandomVariableName(1),
[
Nodes.getIdentifierNode('x'),
Nodes.getIdentifierNode('y')
],
Nodes.getBlockStatementNode([
Nodes.getReturnStatementNode(
Nodes.getBinaryExpressionNode(
<any>this.operator,
Nodes.getIdentifierNode('x'),
Nodes.getIdentifierNode('y')
)
)
])
)
];
}
}
@@ -1,15 +1,14 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../../types/node/TStatement';
import { IOptions } from '../../../interfaces/options/IOptions';
import { initializable } from '../../../decorators/Initializable';
import { ControlFlowStorageCallTemplate } from '../../../templates/custom-nodes/control-flow-replacers-nodes/binary-expression-control-flow-replacer-nodes/ControlFlowStorageCallTemplate';
import { AbstractCustomNode } from '../../AbstractCustomNode';
import { Nodes } from '../../../node/Nodes';
@injectable()
export class ControlFlowStorageCallNode extends AbstractCustomNode {
@@ -64,15 +63,20 @@ export class ControlFlowStorageCallNode extends AbstractCustomNode {
this.rightValue = rightValue;
}
/**
* @returns {string}
*/
protected getTemplate (): string {
return format(ControlFlowStorageCallTemplate(), {
controlFlowStorageKey: this.controlFlowStorageKey,
controlFlowStorageName: this.controlFlowStorageName,
leftValue: this.leftValue,
rightValue: this.rightValue
});
protected getNodeStructure (): TStatement[] {
return [
Nodes.getExpressionStatementNode(
Nodes.getCallExpressionNode(
Nodes.getMemberExpressionNode(
Nodes.getIdentifierNode(this.controlFlowStorageName),
Nodes.getIdentifierNode(this.controlFlowStorageKey)
),
[
<any>this.leftValue,
<any>this.rightValue
]
)
)
];
}
}
@@ -1,7 +1,9 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import * as ESTree from 'estree';
import { TStatement } from '../../types/node/TStatement';
import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
import { IOptions } from '../../interfaces/options/IOptions';
@@ -9,9 +11,9 @@ import { IStorage } from '../../interfaces/storages/IStorage';
import { initializable } from '../../decorators/Initializable';
import { ControlFlowStorageTemplate } from '../../templates/custom-nodes/control-flow-storage-nodes/ControlFlowStorageTemplate';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { Nodes } from '../../node/Nodes';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class ControlFlowStorageNode extends AbstractCustomNode {
@@ -38,12 +40,27 @@ export class ControlFlowStorageNode extends AbstractCustomNode {
}
/**
* @returns {string}
* @returns {TStatement[]}
*/
protected getTemplate (): string {
return format(ControlFlowStorageTemplate(), {
controlFlowStorage: this.controlFlowStorage.toString(),
controlFlowStorageName: this.controlFlowStorage.getStorageId()
});
protected getNodeStructure (): TStatement[] {
const structure: ESTree.Node = Nodes.getVariableDeclarationNode([
Nodes.getVariableDeclaratorNode(
Nodes.getIdentifierNode(this.controlFlowStorage.getStorageId()),
Nodes.getObjectExpressionNode(
Array
.from(this.controlFlowStorage.getStorage())
.map(([key, value]: [string, ICustomNode]) => {
return Nodes.getPropertyNode(
Nodes.getIdentifierNode(key),
<any>value.getNode()[0]
)
})
)
)
]);
NodeUtils.parentize(structure);
return [structure];
}
}
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { initializable } from '../../decorators/Initializable';
@@ -10,6 +12,7 @@ import { initializable } from '../../decorators/Initializable';
import { DebugProtectionFunctionCallTemplate } from '../../templates/custom-nodes/debug-protection-nodes/debug-protection-function-call-node/DebufProtectionFunctionCallTemplate';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class DebugProtectionFunctionCallNode extends AbstractCustomNode {
@@ -35,6 +38,13 @@ export class DebugProtectionFunctionCallNode extends AbstractCustomNode {
this.debugProtectionFunctionName = debugProtectionFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { initializable } from '../../decorators/Initializable';
@@ -10,6 +12,7 @@ import { initializable } from '../../decorators/Initializable';
import { DebugProtectionFunctionIntervalTemplate } from '../../templates/custom-nodes/debug-protection-nodes/debug-protection-function-interval-node/DebugProtectionFunctionIntervalTemplate';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class DebugProtectionFunctionIntervalNode extends AbstractCustomNode {
@@ -35,6 +38,13 @@ export class DebugProtectionFunctionIntervalNode extends AbstractCustomNode {
this.debugProtectionFunctionName = debugProtectionFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { initializable } from '../../decorators/Initializable';
@@ -10,6 +12,7 @@ import { initializable } from '../../decorators/Initializable';
import { DebugProtectionFunctionTemplate } from '../../templates/custom-nodes/debug-protection-nodes/debug-protection-function-node/DebugProtectionFunctionTemplate';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class DebugProtectionFunctionNode extends AbstractCustomNode {
@@ -35,6 +38,13 @@ export class DebugProtectionFunctionNode extends AbstractCustomNode {
this.debugProtectionFunctionName = debugProtectionFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { initializable } from '../../decorators/Initializable';
@@ -11,6 +13,7 @@ import { DomainLockNodeTemplate } from '../../templates/custom-nodes/domain-lock
import { AbstractCustomNode } from '../AbstractCustomNode';
import { CryptUtils } from '../../utils/CryptUtils';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
@@ -37,6 +40,13 @@ export class DomainLockNode extends AbstractCustomNode {
this.callsControllerFunctionName = callsControllerFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -4,6 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
@@ -17,6 +18,7 @@ import { NO_CUSTOM_NODES_PRESET } from '../../options/presets/NoCustomNodes';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
@@ -50,6 +52,13 @@ export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
this.callsControllerFunctionName = callsControllerFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { initializable } from '../../decorators/Initializable';
@@ -13,6 +15,7 @@ import { SelfDefendingTemplate } from '../../templates/custom-nodes/self-defendi
import { AbstractCustomNode } from '../AbstractCustomNode';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
@@ -39,6 +42,13 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
this.callsControllerFunctionName = callsControllerFunctionName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { IStorage } from '../../interfaces/storages/IStorage';
@@ -21,6 +23,7 @@ import { StringArrayRc4DecodeNodeTemplate } from '../../templates/custom-nodes/s
import { AbstractCustomNode } from '../AbstractCustomNode';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class StringArrayCallsWrapper extends AbstractCustomNode {
@@ -104,6 +107,13 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
return decodeStringArrayTemplate;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -13,6 +13,7 @@ import { initializable } from '../../decorators/Initializable';
import { StringArrayTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-node/StringArrayTemplate';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeUtils } from '../../node/NodeUtils';
import { StringArrayStorage } from '../../storages/string-array/StringArrayStorage';
@injectable()
@@ -68,6 +69,13 @@ export class StringArrayNode extends AbstractCustomNode {
return super.getNode();
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as format from 'string-template';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { IStorage } from '../../interfaces/storages/IStorage';
@@ -15,6 +17,7 @@ import { StringArrayRotateFunctionTemplate } from '../../templates/custom-nodes/
import { AbstractCustomNode } from '../AbstractCustomNode';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
import { Utils } from '../../utils/Utils';
@@ -62,6 +65,13 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
this.stringArrayRotateValue = stringArrayRotateValue;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
}
/**
* @returns {string}
*/
@@ -14,7 +14,6 @@ import { CustomNodes } from '../../../enums/container/CustomNodes';
import { AbstractControlFlowReplacer } from './AbstractControlFlowReplacer';
import { Node } from '../../../node/Node';
import { NodeUtils } from '../../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
@injectable()
@@ -110,8 +109,8 @@ export class BinaryExpressionControlFlowReplacer extends AbstractControlFlowRepl
controlFlowStorageCallCustomNode.initialize(
controlFlowStorageId,
storageKey,
NodeUtils.convertStructureToCode([binaryExpressionNode.left]),
NodeUtils.convertStructureToCode([binaryExpressionNode.right])
binaryExpressionNode.left,
binaryExpressionNode.right
);
const statementNode: TStatement = controlFlowStorageCallCustomNode.getNode()[0];
+122 -40
View File
@@ -1,11 +1,11 @@
import * as escodegen from 'escodegen';
import * as ESTree from 'estree';
import { TStatement } from '../../src/types/node/TStatement';
import { TStatement } from '../types/node/TStatement';
import { NodeType } from '../../src/enums/NodeType';
import { NodeType } from '../enums/NodeType';
export class NodeMocks {
export class Nodes {
/**
* @param bodyNodes
* @returns {ESTree.Program}
@@ -19,6 +19,26 @@ export class NodeMocks {
};
}
/**
* @param operator
* @param left
* @param right
* @returns {ESTree.BinaryExpression}
*/
public static getBinaryExpressionNode (
operator: ESTree.BinaryOperator,
left: ESTree.Expression,
right: ESTree.Expression,
): ESTree.BinaryExpression {
return {
type: NodeType.BinaryExpression,
operator,
left,
right,
obfuscated: false
};
}
/**
* @param bodyNodes
* @returns {ESTree.BlockStatement}
@@ -38,8 +58,8 @@ export class NodeMocks {
public static getCatchClauseNode (bodyNodes: ESTree.Statement[] = []): ESTree.CatchClause {
return {
type: NodeType.CatchClause,
param: NodeMocks.getIdentifierNode('err'),
body: NodeMocks.getBlockStatementNode(bodyNodes),
param: Nodes.getIdentifierNode('err'),
body: Nodes.getBlockStatementNode(bodyNodes),
obfuscated: false
};
}
@@ -55,7 +75,7 @@ export class NodeMocks {
): ESTree.CallExpression {
return {
type: NodeType.CallExpression,
callee: callee,
callee,
arguments: args,
obfuscated: false
};
@@ -65,63 +85,78 @@ export class NodeMocks {
* @param expression
* @returns {ESTree.ExpressionStatement}
*/
public static getExpressionStatementNode (
expression: ESTree.Expression = NodeMocks.getIdentifierNode()
): ESTree.ExpressionStatement {
public static getExpressionStatementNode (expression: ESTree.Expression): ESTree.ExpressionStatement {
return {
type: NodeType.ExpressionStatement,
expression: expression,
expression,
obfuscated: false
};
}
/**
* @param functionName
* @param blockStatementNode
* @param params
* @param body
* @returns {ESTree.FunctionDeclaration}
*/
public static getFunctionDeclarationNode (
functionName: string,
blockStatementNode: ESTree.BlockStatement,
params: ESTree.Identifier[] = []
params: ESTree.Identifier[],
body: ESTree.BlockStatement
): ESTree.FunctionDeclaration {
return {
type: NodeType.FunctionDeclaration,
id: NodeMocks.getIdentifierNode(functionName),
params: params,
body: blockStatementNode,
id: Nodes.getIdentifierNode(functionName),
params,
body,
generator: false,
obfuscated: false
};
}
/**
* @param blockStatementNode
* @returns {ESTree.IfStatement}
* @param params
* @param body
* @returns {ESTree.FunctionExpression}
*/
public static getIfStatementNode (blockStatementNode: ESTree.BlockStatement): ESTree.IfStatement {
public static getFunctionExpressionNode (
params: ESTree.Identifier[],
body: ESTree.BlockStatement
): ESTree.FunctionExpression {
return {
type: 'IfStatement',
test: {
type: 'Literal',
value: true,
raw: 'true',
obfuscated: false
},
consequent: blockStatementNode,
type: NodeType.FunctionExpression,
params,
body,
generator: false,
obfuscated: false
};
}
/**
* @param identifierName
* @param test
* @param consequent
* @returns {ESTree.IfStatement}
*/
public static getIfStatementNode (
test: ESTree.Expression,
consequent: ESTree.BlockStatement,
): ESTree.IfStatement {
return {
type: NodeType.IfStatement,
test,
consequent,
obfuscated: false
};
}
/**
* @param name
* @returns {ESTree.Identifier}
*/
public static getIdentifierNode (identifierName: string = 'identifier'): ESTree.Identifier {
public static getIdentifierNode (name: string): ESTree.Identifier {
return {
type: NodeType.Identifier,
name: identifierName,
name,
obfuscated: false
};
}
@@ -130,10 +165,10 @@ export class NodeMocks {
* @param value
* @returns {ESTree.Literal}
*/
public static getLiteralNode (value: boolean|number|string = 'value'): ESTree.Literal {
public static getLiteralNode (value: boolean|number|string): ESTree.Literal {
return {
type: NodeType.Literal,
value: value,
value,
raw: `'${value}'`,
'x-verbatim-property': {
content: `'${value}'`,
@@ -146,17 +181,64 @@ export class NodeMocks {
/**
* @param object
* @param property
* @param computed
* @return {ESTree.MemberExpression}
*/
public static getMemberExpressionNode (
object: ESTree.Identifier,
property: ESTree.Identifier|ESTree.Literal
property: ESTree.Identifier|ESTree.Literal,
computed: boolean = false
): ESTree.MemberExpression {
return {
type: NodeType.MemberExpression,
computed: false,
object: object,
property: property,
computed,
object,
property,
obfuscated: false
};
}
/**
* @param properties
* @return {ESTree.ObjectExpression}
*/
public static getObjectExpressionNode (properties: ESTree.Property[]): ESTree.ObjectExpression {
return {
type: NodeType.ObjectExpression,
properties,
obfuscated: false
};
}
/**
* @return {ESTree.Property}
*/
public static getPropertyNode (
key: ESTree.Expression,
value: ESTree.Expression | ESTree.Pattern,
computed: boolean = false
): ESTree.Property {
return {
type: NodeType.Property,
key,
value,
kind: 'init',
method: false,
shorthand: false,
computed,
obfuscated: false
};
}
/**
* @param argument
* @return {ReturnStatement}
*/
public static getReturnStatementNode (argument: ESTree.Expression): ESTree.ReturnStatement {
return {
type: NodeType.ReturnStatement,
argument,
obfuscated: false
};
}
@@ -172,8 +254,8 @@ export class NodeMocks {
): ESTree.VariableDeclaration {
return {
type: NodeType.VariableDeclaration,
declarations: declarations,
kind: kind,
declarations,
kind,
obfuscated: false
};
}
@@ -186,8 +268,8 @@ export class NodeMocks {
public static getVariableDeclaratorNode (id: ESTree.Identifier, init: any): ESTree.VariableDeclarator {
return {
type: NodeType.VariableDeclarator,
id: id,
init: init,
id,
init,
obfuscated: false
};
}
@@ -8,18 +8,4 @@ export class ControlFlowStorage extends MapStorage <ICustomNode> {
this.initialize();
}
/**
* @returns {string}
*/
public toString (): string {
return Array
.from(this.storage)
.reduce((controlFlowStorageItems: string[], [key, value]: [string, ICustomNode]) => {
controlFlowStorageItems.push(`${key}: ${value.getCode()}`);
return controlFlowStorageItems;
}, [])
.join(',');
}
}
@@ -1,10 +0,0 @@
/**
* @returns {string}
*/
export function BinaryExpressionFunctionTemplate (): string {
return `
function {functionName} (x, y) {
return x {operator} y;
}
`;
}
@@ -1,6 +0,0 @@
/**
* @returns {string}
*/
export function ControlFlowStorageCallTemplate (): string {
return '{controlFlowStorageName}.{controlFlowStorageKey}({leftValue}, {rightValue})';
}
@@ -1,8 +0,0 @@
/**
* @returns {string}
*/
export function ControlFlowStorageTemplate (): string {
return `
var {controlFlowStorageName} = { {controlFlowStorage} };
`;
}
@@ -15,7 +15,7 @@ import { readFileAsString } from '../../helpers/readFileAsString';
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
import { Node } from '../../../src/node/Node';
import { NodeMocks } from '../../mocks/NodeMocks';
import { Nodes } from '../../../src/node/Nodes';
import { NodeUtils } from '../../../src/node/NodeUtils';
/**
@@ -157,7 +157,7 @@ describe('StackTraceAnalyzer', () => {
expectedStackTraceData: IStackTraceData[];
it('should returns correct IStackTraceData - variant #1: basic-1', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/basic-1.js')
)
@@ -204,7 +204,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #2: basic-2', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/basic-2.js')
)
@@ -240,7 +240,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #3: deep conditions nesting', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/deep-conditions-nesting.js')
)
@@ -276,7 +276,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #4: call before declaration', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/call-before-declaration.js')
)
@@ -296,7 +296,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #5: call expression of object member #1', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/call-expression-of-object-member-1.js')
)
@@ -352,7 +352,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #5: call expression of object member #2', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/call-expression-of-object-member-2.js')
)
@@ -377,7 +377,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #6: no call expressions', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/no-call-expressions.js')
)
@@ -391,7 +391,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #7: only call expression', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/only-call-expression.js')
)
@@ -405,7 +405,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #8: self-invoking functions', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/self-invoking-functions.js')
)
@@ -437,7 +437,7 @@ describe('StackTraceAnalyzer', () => {
});
it('should returns correct IStackTraceData - variant #9: no recursion', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/stack-trace-analyzer/no-recursion.js')
)
+16 -16
View File
@@ -14,7 +14,7 @@ import { readFileAsString } from '../../helpers/readFileAsString';
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
import { NodeAppender } from '../../../src/node/NodeAppender';
import { NodeMocks } from '../../mocks/NodeMocks';
import { Nodes } from '../../../src/node/Nodes';
import { NodeUtils } from '../../../src/node/NodeUtils';
describe('NodeAppender', () => {
@@ -28,13 +28,13 @@ describe('NodeAppender', () => {
var test = 1;
`);
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-expected.js')
)
@@ -68,13 +68,13 @@ describe('NodeAppender', () => {
});
it('should append node into first and deepest function call in calls trace - variant #1', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/variant-1.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/variant-1-expected.js')
)
@@ -87,13 +87,13 @@ describe('NodeAppender', () => {
});
it('should append node into first and deepest function call in calls trace - variant #2', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/variant-2.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/variant-2-expected.js')
)
@@ -109,7 +109,7 @@ describe('NodeAppender', () => {
let astTree: ESTree.Program;
beforeEach(() => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/by-index.js')
@@ -118,7 +118,7 @@ describe('NodeAppender', () => {
});
it('should append node into deepest function call by specified index in calls trace - variant #1', () => {
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/by-index-variant-1-expected.js')
@@ -132,7 +132,7 @@ describe('NodeAppender', () => {
});
it('should append node into deepest function call by specified index in calls trace - variant #2', () => {
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/by-index-variant-2-expected.js')
@@ -146,12 +146,12 @@ describe('NodeAppender', () => {
});
it('should append node into deepest function call by specified index in calls trace - variant #3', () => {
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/by-index-variant-3.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/append-node-to-optimal-block-scope/by-index-variant-3-expected.js')
)
@@ -193,13 +193,13 @@ describe('NodeAppender', () => {
var test = 1;
`);
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/insert-node-at-index.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/insert-node-at-index-expected.js')
)
@@ -226,13 +226,13 @@ describe('NodeAppender', () => {
var test = 1;
`);
astTree = NodeMocks.getProgramNode(
astTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/prepend-node.js')
)
);
expectedAstTree = NodeMocks.getProgramNode(
expectedAstTree = Nodes.getProgramNode(
NodeUtils.convertCodeToStructure(
readFileAsString('./test/fixtures/node-appender/prepend-node-expected.js')
)
+59 -44
View File
@@ -2,7 +2,7 @@ import * as ESTree from 'estree';
import { assert } from 'chai';
import { NodeMocks } from '../../mocks/NodeMocks';
import { Nodes } from '../../../src/node/Nodes';
import { NodeUtils } from '../../../src/node/NodeUtils';
describe('NodeUtils', () => {
@@ -11,10 +11,10 @@ describe('NodeUtils', () => {
expectedLiteralNode: any;
beforeEach(() => {
literalNode = NodeMocks.getLiteralNode();
literalNode = Nodes.getLiteralNode('value');
delete literalNode['x-verbatim-property'];
expectedLiteralNode = NodeMocks.getLiteralNode();
expectedLiteralNode = Nodes.getLiteralNode('value');
NodeUtils.addXVerbatimPropertyToLiterals(literalNode);
});
@@ -37,17 +37,17 @@ describe('NodeUtils', () => {
var abc = 'cde';
`;
identifierNode = NodeMocks.getIdentifierNode('abc');
identifierNode = Nodes.getIdentifierNode('abc');
literalNode = NodeMocks.getLiteralNode('cde');
literalNode = Nodes.getLiteralNode('cde');
variableDeclaratorNode = NodeMocks.getVariableDeclaratorNode(identifierNode, literalNode);
variableDeclaratorNode = Nodes.getVariableDeclaratorNode(identifierNode, literalNode);
variableDeclarationNode = NodeMocks.getVariableDeclarationNode([
variableDeclarationNode = Nodes.getVariableDeclarationNode([
variableDeclaratorNode
]);
programNode = NodeMocks.getProgramNode([
programNode = Nodes.getProgramNode([
variableDeclarationNode
]);
@@ -69,11 +69,11 @@ describe('NodeUtils', () => {
beforeEach(() => {
structure = [
NodeMocks.getProgramNode([
NodeMocks.getVariableDeclarationNode([
NodeMocks.getVariableDeclaratorNode(
NodeMocks.getIdentifierNode('abc'),
NodeMocks.getLiteralNode('cde')
Nodes.getProgramNode([
Nodes.getVariableDeclarationNode([
Nodes.getVariableDeclaratorNode(
Nodes.getIdentifierNode('abc'),
Nodes.getLiteralNode('cde')
)
])
])
@@ -92,10 +92,10 @@ describe('NodeUtils', () => {
expressionStatementNode2: ESTree.ExpressionStatement;
beforeEach(() => {
expressionStatementNode1 = NodeMocks.getExpressionStatementNode();
expressionStatementNode2 = NodeMocks.getExpressionStatementNode();
expressionStatementNode1 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode2 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
blockStatementNode = NodeMocks.getBlockStatementNode([
blockStatementNode = Nodes.getBlockStatementNode([
expressionStatementNode1,
expressionStatementNode2
]);
@@ -128,31 +128,37 @@ describe('NodeUtils', () => {
programNode: ESTree.Program;
beforeEach(() => {
expressionStatementNode1 = NodeMocks.getExpressionStatementNode();
expressionStatementNode2 = NodeMocks.getExpressionStatementNode();
expressionStatementNode3 = NodeMocks.getExpressionStatementNode();
expressionStatementNode1 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode2 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode3 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
ifStatementBlockStatementNode2 = NodeMocks.getBlockStatementNode([
ifStatementBlockStatementNode2 = Nodes.getBlockStatementNode([
expressionStatementNode2,
expressionStatementNode3
]);
ifStatementNode2 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode2);
ifStatementNode2 = Nodes.getIfStatementNode(
Nodes.getLiteralNode(true),
ifStatementBlockStatementNode2
);
ifStatementBlockStatementNode1 = NodeMocks.getBlockStatementNode([
ifStatementBlockStatementNode1 = Nodes.getBlockStatementNode([
ifStatementNode2
]);
ifStatementNode1 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode1);
ifStatementNode1 = Nodes.getIfStatementNode(
Nodes.getLiteralNode(true),
ifStatementBlockStatementNode1
);
functionDeclarationBlockStatementNode = NodeMocks.getBlockStatementNode([
functionDeclarationBlockStatementNode = Nodes.getBlockStatementNode([
expressionStatementNode1,
ifStatementNode1
]);
functionDeclarationNode = NodeMocks.getFunctionDeclarationNode('test', functionDeclarationBlockStatementNode);
functionDeclarationNode = Nodes.getFunctionDeclarationNode('test', [], functionDeclarationBlockStatementNode);
programNode = NodeMocks.getProgramNode([
programNode = Nodes.getProgramNode([
functionDeclarationNode
]);
@@ -203,37 +209,43 @@ describe('NodeUtils', () => {
programNode: ESTree.Program;
beforeEach(() => {
expressionStatementNode1 = NodeMocks.getExpressionStatementNode();
expressionStatementNode2 = NodeMocks.getExpressionStatementNode();
expressionStatementNode3 = NodeMocks.getExpressionStatementNode();
expressionStatementNode1 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode2 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode3 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
ifStatementBlockStatementNode2 = NodeMocks.getBlockStatementNode([
ifStatementBlockStatementNode2 = Nodes.getBlockStatementNode([
expressionStatementNode3
]);
ifStatementNode2 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode2);
ifStatementNode2 = Nodes.getIfStatementNode(
Nodes.getLiteralNode(true),
ifStatementBlockStatementNode2
);
functionDeclarationBlockStatementNode2 = NodeMocks.getBlockStatementNode([
functionDeclarationBlockStatementNode2 = Nodes.getBlockStatementNode([
ifStatementNode2,
expressionStatementNode2
]);
functionDeclarationNode2 = NodeMocks.getFunctionDeclarationNode('test', functionDeclarationBlockStatementNode2);
functionDeclarationNode2 = Nodes.getFunctionDeclarationNode('test', [], functionDeclarationBlockStatementNode2);
ifStatementBlockStatementNode1 = NodeMocks.getBlockStatementNode([
ifStatementBlockStatementNode1 = Nodes.getBlockStatementNode([
functionDeclarationNode2
]);
ifStatementNode1 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode1);
ifStatementNode1 = Nodes.getIfStatementNode(
Nodes.getLiteralNode(true),
ifStatementBlockStatementNode1
);
functionDeclarationBlockStatementNode1 = NodeMocks.getBlockStatementNode([
functionDeclarationBlockStatementNode1 = Nodes.getBlockStatementNode([
expressionStatementNode1,
ifStatementNode1
]);
functionDeclarationNode1 = NodeMocks.getFunctionDeclarationNode('test', functionDeclarationBlockStatementNode1);
functionDeclarationNode1 = Nodes.getFunctionDeclarationNode('test', [], functionDeclarationBlockStatementNode1);
programNode = NodeMocks.getProgramNode([
programNode = Nodes.getProgramNode([
functionDeclarationNode1
]);
@@ -277,19 +289,22 @@ describe('NodeUtils', () => {
programNode: ESTree.Program;
beforeEach(() => {
expressionStatementNode1 = NodeMocks.getExpressionStatementNode();
expressionStatementNode2 = NodeMocks.getExpressionStatementNode();
expressionStatementNode1 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
expressionStatementNode2 = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
ifStatementBlockStatementNode = NodeMocks.getBlockStatementNode([
ifStatementBlockStatementNode = Nodes.getBlockStatementNode([
expressionStatementNode1,
expressionStatementNode2
]);
ifStatementNode = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode);
ifStatementNode = Nodes.getIfStatementNode(
Nodes.getLiteralNode(true),
ifStatementBlockStatementNode
);
});
it('should parentize given AST-tree with `ProgramNode` as root node', () => {
programNode = NodeMocks.getProgramNode([
programNode = Nodes.getProgramNode([
ifStatementNode
]);
@@ -303,7 +318,7 @@ describe('NodeUtils', () => {
});
it('should parentize given AST-tree', () => {
programNode = NodeMocks.getProgramNode([
programNode = Nodes.getProgramNode([
ifStatementNode
]);
programNode['parentNode'] = programNode;
@@ -1,42 +0,0 @@
import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
import { assert } from 'chai';
import { TCustomNodeFactory } from '../../../src/types/container/TCustomNodeFactory';
import { ICustomNode } from '../../../src/interfaces/custom-nodes/ICustomNode';
import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
import { IStorage } from '../../../src/interfaces/storages/IStorage';
import { CustomNodes } from '../../../src/enums/container/CustomNodes';
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
describe('ControlFlowStorage', () => {
describe('toString (): string', () => {
it('should return correct ControlFlowStorage data after `.toString()` call', () => {
const key1: string = 'key1';
const key2: string = 'key2';
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade({
controlFlowFlattening: true
});
const customNodeFactory: TCustomNodeFactory = inversifyContainerFacade
.get<TCustomNodeFactory>(ServiceIdentifiers['Factory<ICustomNode>']);
const controlFlowStorage: IStorage <ICustomNode> = inversifyContainerFacade
.get<IStorage<ICustomNode>>(ServiceIdentifiers['IStorage<ICustomNode>']);
const controlFlowStorageCallNode1: ICustomNode = customNodeFactory(CustomNodes.ControlFlowStorageCallNode);
const controlFlowStorageCallNode2: ICustomNode = customNodeFactory(CustomNodes.ControlFlowStorageCallNode);
controlFlowStorageCallNode1.initialize('controlFlowStorageName', key1, 1, 2);
controlFlowStorageCallNode2.initialize('controlFlowStorageName', key2, 3, 4);
controlFlowStorage.set(key1, controlFlowStorageCallNode1);
controlFlowStorage.set(key2, controlFlowStorageCallNode2);
assert.equal(
controlFlowStorage.toString(),
`${key1}: ${controlFlowStorageCallNode1.getCode()},${key2}: ${controlFlowStorageCallNode2.getCode()}`
);
});
});
});