123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { injectable, inject } from 'inversify';
- import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
- import * as ESTree from 'estree';
- import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
- import { TStatement } from '../../../types/node/TStatement';
- import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
- import { IOptions } from '../../../interfaces/options/IOptions';
- import { IStorage } from '../../../interfaces/storages/IStorage';
- import { CustomNodes } from '../../../enums/container/custom-nodes/CustomNodes';
- import { AbstractControlFlowReplacer } from './AbstractControlFlowReplacer';
- import { Node } from '../../../node/Node';
- @injectable()
- export class StringLiteralControlFlowReplacer extends AbstractControlFlowReplacer {
- /**
- * @type {number}
- */
- private static readonly usingExistingIdentifierChance: number = 1;
- /**
- * @param customNodeFactory
- * @param options
- */
- constructor (
- @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
- @inject(ServiceIdentifiers.IOptions) options: IOptions
- ) {
- super(customNodeFactory, options);
- }
- /**
- * @param literalNode
- * @param parentNode
- * @param controlFlowStorage
- * @returns {ESTree.Node}
- */
- public replace (
- literalNode: ESTree.Literal,
- parentNode: ESTree.Node,
- controlFlowStorage: IStorage <ICustomNode>
- ): ESTree.Node {
- if (Node.isPropertyNode(parentNode) && parentNode.key === literalNode) {
- return literalNode;
- }
- if (typeof literalNode.value !== 'string' || literalNode.value.length < 3) {
- return literalNode;
- }
- const replacerId: string = String(literalNode.value);
- const literalFunctionCustomNode: ICustomNode = this.customNodeFactory(CustomNodes.StringLiteralNode);
- literalFunctionCustomNode.initialize(literalNode.value);
- const storageKey: string = this.insertCustomNodeToControlFlowStorage(
- literalFunctionCustomNode,
- controlFlowStorage,
- replacerId,
- StringLiteralControlFlowReplacer.usingExistingIdentifierChance
- );
- return this.getControlFlowStorageCallNode(controlFlowStorage.getStorageId(), storageKey);
- }
- /**
- * @param controlFlowStorageId
- * @param storageKey
- * @returns {ESTree.Node}
- */
- protected getControlFlowStorageCallNode (
- controlFlowStorageId: string,
- storageKey: string
- ): ESTree.Node {
- const controlFlowStorageCallCustomNode: ICustomNode = this.customNodeFactory(
- CustomNodes.StringLiteralControlFlowStorageCallNode
- );
- controlFlowStorageCallCustomNode.initialize(controlFlowStorageId, storageKey);
- const statementNode: TStatement = controlFlowStorageCallCustomNode.getNode()[0];
- if (!statementNode || !Node.isExpressionStatementNode(statementNode)) {
- throw new Error(`\`controlFlowStorageCallCustomNode.getNode()[0]\` should returns array with \`ExpressionStatement\` node`);
- }
- return statementNode.expression;
- }
- }
|