Added StringArrayIntermediateCallsWrapperNode custom node

This commit is contained in:
sanex
2020-09-10 20:06:20 +03:00
parent f00c32d28b
commit b483ef17cf
13 changed files with 247 additions and 91 deletions
+1
View File
@@ -12,6 +12,7 @@ export enum ServiceIdentifiers {
Factory__IObfuscatedCode = 'Factory<IObfuscatedCode>',
Factory__IObjectExpressionKeysTransformerCustomNode = 'Factory<IObjectExpressionKeysTransformerCustomNode>',
Factory__IObjectExpressionExtractor = 'Factory<IObjectExpressionExtractor>',
Factory__IStringArrayTransformerCustomNode = 'Factory<IStringArrayTransformerCustomNode>',
Factory__TControlFlowStorage = 'Factory<TControlFlowStorage>',
IArrayUtils = 'IArrayUtils',
ICalleeDataExtractor = 'ICalleeDataExtractor',
@@ -7,6 +7,7 @@ import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { ControlFlowCustomNode } from '../../../enums/custom-nodes/ControlFlowCustomNode';
import { DeadCodeInjectionCustomNode } from '../../../enums/custom-nodes/DeadCodeInjectionCustomNode';
import { ObjectExpressionKeysTransformerCustomNode } from '../../../enums/custom-nodes/ObjectExpressionKeysTransformerCustomNode';
import { StringArrayTransformerCustomNode } from '../../../enums/custom-nodes/StringArrayTransformerCustomNode';
import { ObjectExpressionVariableDeclarationHostNode } from '../../../custom-nodes/object-expression-keys-transformer-nodes/ObjectExpressionVariableDeclarationHostNode';
import { BinaryExpressionFunctionNode } from '../../../custom-nodes/control-flow-flattening-nodes/BinaryExpressionFunctionNode';
@@ -17,6 +18,7 @@ import { CallExpressionFunctionNode } from '../../../custom-nodes/control-flow-f
import { ControlFlowStorageNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ControlFlowStorageNode';
import { ExpressionWithOperatorControlFlowStorageCallNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ExpressionWithOperatorControlFlowStorageCallNode';
import { LogicalExpressionFunctionNode } from '../../../custom-nodes/control-flow-flattening-nodes/LogicalExpressionFunctionNode';
import { StringArrayIntermediateCallsWrapperNode } from '../../../custom-nodes/string-array-nodes/StringArrayIntermediateCallsWrapperNode';
import { StringLiteralControlFlowStorageCallNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/StringLiteralControlFlowStorageCallNode';
import { StringLiteralNode } from '../../../custom-nodes/control-flow-flattening-nodes/StringLiteralNode';
@@ -68,6 +70,11 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
.toConstructor(ObjectExpressionVariableDeclarationHostNode)
.whenTargetNamed(ObjectExpressionKeysTransformerCustomNode.ObjectExpressionVariableDeclarationHostNode);
// string array transformer nodes
bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.Newable__ICustomNode)
.toConstructor(StringArrayIntermediateCallsWrapperNode)
.whenTargetNamed(StringArrayTransformerCustomNode.StringArrayIntermediateCallsWrapperNode);
// control flow customNode constructor factory
bind<ICustomNode>(ServiceIdentifiers.Factory__IControlFlowCustomNode)
.toFactory<ICustomNode>(InversifyContainerFacade
@@ -76,8 +83,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ICustomCodeHelperFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions,
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
ServiceIdentifiers.IOptions
));
// dead code injection customNode constructor factory
@@ -99,7 +105,17 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ICustomCodeHelperFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions,
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
ServiceIdentifiers.IOptions
));
// string array transformer customNode constructor factory
bind<ICustomNode>(ServiceIdentifiers.Factory__IStringArrayTransformerCustomNode)
.toFactory<ICustomNode>(InversifyContainerFacade
.getConstructorFactory<StringArrayTransformerCustomNode, ICustomNode>(
ServiceIdentifiers.Newable__ICustomNode,
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ICustomCodeHelperFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions
));
});
@@ -1,8 +0,0 @@
/**
* @returns {string}
*/
export function StringArrayCallsWrapperIntermediateTemplate (): string {
return `
const {intermediateName} = {stringArrayCallsWrapperName};
`;
}
@@ -0,0 +1,83 @@
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { TStatement } from '../../types/node/TStatement';
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { initializable } from '../../decorators/Initializable';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class StringArrayIntermediateCallsWrapperNode extends AbstractCustomNode {
/**
* @type {string}
*/
@initializable()
private stringArrayCallsWrapperName!: string;
/**
* @type {string}
*/
@initializable()
private stringArrayIntermediateCallsWrapperName!: string;
/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
public constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(
identifierNamesGeneratorFactory,
customCodeHelperFormatter,
randomGenerator,
options
);
}
/**
* @param {string} stringArrayIntermediateCallsWrapperName
* @param {string} stringArrayCallsWrapperName
*/
public initialize (
stringArrayIntermediateCallsWrapperName: string,
stringArrayCallsWrapperName: string
): void {
this.stringArrayIntermediateCallsWrapperName = stringArrayIntermediateCallsWrapperName;
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
}
/**
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
const structure: TStatement = NodeFactory.variableDeclarationNode(
[
NodeFactory.variableDeclaratorNode(
NodeFactory.identifierNode(this.stringArrayIntermediateCallsWrapperName),
NodeFactory.identifierNode(this.stringArrayCallsWrapperName)
)
],
'const',
);
NodeUtils.parentizeAst(structure);
return [structure];
}
}
@@ -0,0 +1,3 @@
export enum StringArrayTransformerCustomNode {
StringArrayIntermediateCallsWrapperNode = 'StringArrayIntermediateCallsWrapperNode'
}
@@ -3,11 +3,14 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { TInitialData } from '../../types/TInitialData';
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
import { TStringArrayIntermediateCallsWrapperDataByEncoding } from '../../types/node-transformers/string-array-transformers/TStringArrayIntermediateCallsWrapperDataByEncoding';
import { TStringArrayTransformerCustomNodeFactory } from '../../types/container/custom-nodes/TStringArrayTransformerCustomNodeFactory';
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
@@ -20,6 +23,7 @@ import { IStringArrayStorageItemData } from '../../interfaces/storages/string-ar
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
import { StringArrayTransformerCustomNode } from '../../enums/custom-nodes/StringArrayTransformerCustomNode';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeAppender } from '../../node/NodeAppender';
@@ -29,6 +33,7 @@ import { NodeLiteralUtils } from '../../node/NodeLiteralUtils';
import { NodeMetadata } from '../../node/NodeMetadata';
import { NodeUtils } from '../../node/NodeUtils';
import { NumberUtils } from '../../utils/NumberUtils';
import { StringArrayIntermediateCallsWrapperNode } from '../../custom-nodes/string-array-nodes/StringArrayIntermediateCallsWrapperNode';
@injectable()
export class StringArrayTransformer extends AbstractNodeTransformer {
@@ -70,6 +75,11 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
*/
private readonly stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer;
/**
* @type {TStringArrayTransformerCustomNodeFactory}
*/
private readonly stringArrayTransformerCustomNodeFactory: TStringArrayTransformerCustomNodeFactory;
/**
* @type {TNodeWithLexicalScope[]}
*/
@@ -78,32 +88,36 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
/**
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
* @param {IArrayUtils} arrayUtils
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
* @param {ILiteralNodesCacheStorage} literalNodesCacheStorage
* @param {IStringArrayStorage} stringArrayStorage
* @param {IStringArrayStorageAnalyzer} stringArrayStorageAnalyzer
* @param {IArrayUtils} arrayUtils
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {TStringArrayTransformerCustomNodeFactory} stringArrayTransformerCustomNodeFactory
*/
public constructor (
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions,
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
@inject(ServiceIdentifiers.ILiteralNodesCacheStorage) literalNodesCacheStorage: ILiteralNodesCacheStorage,
@inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IStringArrayStorageAnalyzer) stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer,
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.Factory__IStringArrayTransformerCustomNode)
stringArrayTransformerCustomNodeFactory: TStringArrayTransformerCustomNodeFactory
) {
super(randomGenerator, options);
this.arrayUtils = arrayUtils;
this.escapeSequenceEncoder = escapeSequenceEncoder;
this.literalNodesCacheStorage = literalNodesCacheStorage;
this.stringArrayStorage = stringArrayStorage;
this.stringArrayStorageAnalyzer = stringArrayStorageAnalyzer;
this.arrayUtils = arrayUtils;
this.escapeSequenceEncoder = escapeSequenceEncoder;
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
this.stringArrayTransformerCustomNodeFactory = stringArrayTransformerCustomNodeFactory;
}
/**
@@ -360,20 +374,19 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
for (const stringArrayIntermediateCallsWrapperName of names) {
const stringArrayRootCallsWrapperName: string = this.getStringArrayRootCallsWrapperName(encoding);
const stringArrayIntermediateCallsWrapperNode: ICustomNode<TInitialData<StringArrayIntermediateCallsWrapperNode>> =
this.stringArrayTransformerCustomNodeFactory(
StringArrayTransformerCustomNode.StringArrayIntermediateCallsWrapperNode
);
stringArrayIntermediateCallsWrapperNode.initialize(
stringArrayIntermediateCallsWrapperName,
stringArrayRootCallsWrapperName
);
NodeAppender.prepend(
lexicalScopeBodyNode,
[
NodeFactory.variableDeclarationNode(
[
NodeFactory.variableDeclaratorNode(
NodeFactory.identifierNode(stringArrayIntermediateCallsWrapperName),
NodeFactory.identifierNode(stringArrayRootCallsWrapperName)
)
],
'var',
)
]
stringArrayIntermediateCallsWrapperNode.getNode()
);
}
}
@@ -0,0 +1,7 @@
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { StringArrayTransformerCustomNode } from '../../../enums/custom-nodes/StringArrayTransformerCustomNode';
export type TStringArrayTransformerCustomNodeFactory = <
TInitialData extends unknown[] = unknown[]
> (stringArrayTransformerCustomNodeName: StringArrayTransformerCustomNode) => ICustomNode <TInitialData>;