|
@@ -5,23 +5,38 @@ import * as ESTree from 'estree';
|
|
|
|
|
|
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
|
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
|
import { TStringArrayIndexesType } from '../../types/options/TStringArrayIndexesType';
|
|
import { TStringArrayIndexesType } from '../../types/options/TStringArrayIndexesType';
|
|
|
|
+import { TStringArrayIndexNodeFactory } from '../../types/container/custom-nodes/string-array-index-nodes/TStringArrayIndexNodeFactory';
|
|
|
|
|
|
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
|
|
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
|
|
import { IOptions } from '../../interfaces/options/IOptions';
|
|
import { IOptions } from '../../interfaces/options/IOptions';
|
|
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
|
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
|
|
|
|
|
import { StringArrayIndexesType } from '../../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
|
|
import { StringArrayIndexesType } from '../../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
|
|
|
|
+import { StringArrayIndexNode } from '../../enums/custom-nodes/string-array-index-nodes/StringArrayIndexNode';
|
|
|
|
|
|
import { AbstractCustomNode } from '../AbstractCustomNode';
|
|
import { AbstractCustomNode } from '../AbstractCustomNode';
|
|
import { NodeFactory } from '../../node/NodeFactory';
|
|
import { NodeFactory } from '../../node/NodeFactory';
|
|
import { NodeMetadata } from '../../node/NodeMetadata';
|
|
import { NodeMetadata } from '../../node/NodeMetadata';
|
|
import { NodeUtils } from '../../node/NodeUtils';
|
|
import { NodeUtils } from '../../node/NodeUtils';
|
|
-import { NumberUtils } from '../../utils/NumberUtils';
|
|
|
|
|
|
|
|
@injectable()
|
|
@injectable()
|
|
export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
|
|
export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
|
|
|
|
+ /**
|
|
|
|
+ * @type {Map<TStringArrayIndexesType, StringArrayIndexNode>}
|
|
|
|
+ */
|
|
|
|
+ private static readonly stringArrayIndexNodesMap: Map<TStringArrayIndexesType, StringArrayIndexNode> = new Map([
|
|
|
|
+ [StringArrayIndexesType.HexadecimalNumber, StringArrayIndexNode.StringArrayHexadecimalNumberIndexNode],
|
|
|
|
+ [StringArrayIndexesType.HexadecimalNumericString, StringArrayIndexNode.StringArrayHexadecimalNumericStringIndexNode]
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @type {TStringArrayIndexNodeFactory}
|
|
|
|
+ */
|
|
|
|
+ private readonly stringArrayIndexNodeFactory: TStringArrayIndexNodeFactory;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
|
|
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
|
|
|
|
+ * @param {TStringArrayIndexNodeFactory} stringArrayIndexNodeFactory
|
|
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
|
|
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
|
|
* @param {IRandomGenerator} randomGenerator
|
|
* @param {IRandomGenerator} randomGenerator
|
|
* @param {IOptions} options
|
|
* @param {IOptions} options
|
|
@@ -29,6 +44,8 @@ export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
|
|
protected constructor (
|
|
protected constructor (
|
|
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
|
|
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
|
|
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
|
|
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
|
|
|
|
+ @inject(ServiceIdentifiers.Factory__IStringArrayIndexNode)
|
|
|
|
+ stringArrayIndexNodeFactory: TStringArrayIndexNodeFactory,
|
|
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
|
|
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
|
|
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
|
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
|
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
|
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
|
@@ -39,32 +56,30 @@ export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
|
|
randomGenerator,
|
|
randomGenerator,
|
|
options
|
|
options
|
|
);
|
|
);
|
|
|
|
+
|
|
|
|
+ this.stringArrayIndexNodeFactory = stringArrayIndexNodeFactory;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* @param {number} index
|
|
* @param {number} index
|
|
* @returns {Expression}
|
|
* @returns {Expression}
|
|
*/
|
|
*/
|
|
- protected getStringArrayCallIndexNode (index: number): ESTree.Expression {
|
|
|
|
|
|
+ protected getStringArrayIndexNode (index: number): ESTree.Expression {
|
|
const isPositive: boolean = index >= 0;
|
|
const isPositive: boolean = index >= 0;
|
|
const normalizedIndex: number = Math.abs(index);
|
|
const normalizedIndex: number = Math.abs(index);
|
|
|
|
|
|
const stringArrayCallsIndexType: TStringArrayIndexesType = this.randomGenerator
|
|
const stringArrayCallsIndexType: TStringArrayIndexesType = this.randomGenerator
|
|
.getRandomGenerator()
|
|
.getRandomGenerator()
|
|
.pickone(this.options.stringArrayIndexesType);
|
|
.pickone(this.options.stringArrayIndexesType);
|
|
- let stringArrayCallIndexNode: ESTree.Expression;
|
|
|
|
-
|
|
|
|
- switch (stringArrayCallsIndexType) {
|
|
|
|
- case StringArrayIndexesType.HexadecimalNumber:
|
|
|
|
- stringArrayCallIndexNode = this.getHexadecimalNumberCallIndexNode(normalizedIndex);
|
|
|
|
- break;
|
|
|
|
|
|
+ const stringArrayIndexNodeName: StringArrayIndexNode | null = AbstractStringArrayCallNode.stringArrayIndexNodesMap.get(stringArrayCallsIndexType) ?? null;
|
|
|
|
|
|
- case StringArrayIndexesType.HexadecimalNumericString:
|
|
|
|
- default:
|
|
|
|
- stringArrayCallIndexNode = this.getHexadecimalNumericStringCallIndexNode(normalizedIndex);
|
|
|
|
- break;
|
|
|
|
|
|
+ if (!stringArrayIndexNodeName) {
|
|
|
|
+ throw new Error('Invalid string array index node name');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ const stringArrayCallIndexNode: ESTree.Expression = this.stringArrayIndexNodeFactory(stringArrayIndexNodeName)
|
|
|
|
+ .getNode(normalizedIndex);
|
|
|
|
+
|
|
NodeMetadata.set(stringArrayCallIndexNode, { replacedLiteral: true });
|
|
NodeMetadata.set(stringArrayCallIndexNode, { replacedLiteral: true });
|
|
|
|
|
|
const hexadecimalNode: ESTree.Expression = isPositive
|
|
const hexadecimalNode: ESTree.Expression = isPositive
|
|
@@ -90,24 +105,4 @@ export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
|
|
|
|
|
|
return rc4KeyLiteralNode;
|
|
return rc4KeyLiteralNode;
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param {number} index
|
|
|
|
- * @returns {Expression}
|
|
|
|
- */
|
|
|
|
- private getHexadecimalNumberCallIndexNode (index: number): ESTree.Expression {
|
|
|
|
- const hexadecimalIndex: string = NumberUtils.toHex(index);
|
|
|
|
-
|
|
|
|
- return NodeFactory.literalNode(index, hexadecimalIndex);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param {number} index
|
|
|
|
- * @returns {Expression}
|
|
|
|
- */
|
|
|
|
- private getHexadecimalNumericStringCallIndexNode (index: number): ESTree.Expression {
|
|
|
|
- const hexadecimalIndex: string = NumberUtils.toHex(index);
|
|
|
|
-
|
|
|
|
- return NodeFactory.literalNode(hexadecimalIndex);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|