All files / src/container/modules/node-transformers NodeControlFlowTransformersModule.ts

100% Statements 14/14
100% Branches 2/2
100% Functions 3/3
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x       1x   1x   1x 1819x       1819x   202x   202x 481x 279x     202x         202x   202x        
import { ContainerModule, interfaces } from 'inversify';
import { ServiceIdentifiers } from '../../ServiceIdentifiers';
 
import { IControlFlowReplacer } from '../../../interfaces/node-transformers/IControlFlowReplacer';
 
import { NodeControlFlowReplacers } from '../../../enums/container/NodeControlFlowReplacers';
 
import { BinaryExpressionControlFlowReplacer } from '../../../node-transformers/node-control-flow-transformers/control-flow-replacers/BinaryExpressionControlFlowReplacer';
 
export const nodeControlFlowTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
    bind<IControlFlowReplacer>(ServiceIdentifiers.IControlFlowReplacer)
        .to(BinaryExpressionControlFlowReplacer)
        .whenTargetNamed(NodeControlFlowReplacers.BinaryExpressionControlFlowReplacer);
 
    bind<IControlFlowReplacer>(ServiceIdentifiers['Factory<IControlFlowReplacer>'])
        .toFactory<IControlFlowReplacer>((context: interfaces.Context) => {
            const cache: Map <NodeControlFlowReplacers, IControlFlowReplacer> = new Map();
 
            return (replacerName: NodeControlFlowReplacers) => {
                if (cache.has(replacerName)) {
                    return <IControlFlowReplacer>cache.get(replacerName);
                }
 
                const controlFlowReplacer: IControlFlowReplacer = context.container.getNamed<IControlFlowReplacer>(
                    ServiceIdentifiers.IControlFlowReplacer,
                    replacerName
                );
 
                cache.set(replacerName, controlFlowReplacer);
 
                return controlFlowReplacer;
            };
        });
});