All files / src/custom-nodes/control-flow-flattening-nodes BlockStatementControlFlowFlatteningNode.ts

100% Statements 24/24
100% Branches 0/0
100% Functions 2/2
100% Lines 21/21
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 1x               1x   1x 1x 1x 1x     1x         1x           1x           1x               496x                         496x 496x 496x             496x 496x 496x                                                                   2502x                           496x   496x      
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
 
import * as ESTree from 'estree';
 
import { TStatement } from '../../types/node/TStatement';
 
import { IOptions } from '../../interfaces/options/IOptions';
 
import { initializable } from '../../decorators/Initializable';
 
import { AbstractCustomNode } from '../AbstractCustomNode';
import { Nodes } from '../../node/Nodes';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
 
@injectable()
export class BlockStatementControlFlowFlatteningNode extends AbstractCustomNode {
    /**
     * @type {ESTree.Statement[]}
     */
    @initializable()
    private blockStatementBody: ESTree.Statement[];
 
    /**
     * @type {number[]}
     */
    @initializable()
    private originalKeysIndexesInShuffledArray: number[];
 
    /**
     * @type {number[]}
     */
    @initializable()
    private shuffledKeys: number[];
 
    /**
     * @param options
     */
    constructor (
        @inject(ServiceIdentifiers.IOptions) options: IOptions
    ) {
        super(options);
    }
 
    /***
     * @param blockStatementBody
     * @param shuffledKeys
     * @param originalKeysIndexesInShuffledArray
     */
    public initialize (
        blockStatementBody: ESTree.Statement[],
        shuffledKeys: number[],
        originalKeysIndexesInShuffledArray: number[]
    ): void {
        this.blockStatementBody = blockStatementBody;
        this.shuffledKeys = shuffledKeys;
        this.originalKeysIndexesInShuffledArray = originalKeysIndexesInShuffledArray;
    }
 
    /**
     * @returns {TStatement[]}
     */
    protected getNodeStructure (): TStatement[] {
        const controllerIdentifierName: string = RandomGeneratorUtils.getRandomString(3);
        const indexIdentifierName: string = RandomGeneratorUtils.getRandomString(3);
        const structure: ESTree.BlockStatement = Nodes.getBlockStatementNode([
            Nodes.getVariableDeclarationNode([
                Nodes.getVariableDeclaratorNode(
                    Nodes.getIdentifierNode(controllerIdentifierName),
                    Nodes.getCallExpressionNode(
                        Nodes.getMemberExpressionNode(
                            Nodes.getLiteralNode(
                                this.originalKeysIndexesInShuffledArray.join('|')
                            ),
                            Nodes.getIdentifierNode('split')
                        ),
                        [
                            Nodes.getLiteralNode('|')
                        ]
                    )
                ),
                Nodes.getVariableDeclaratorNode(
                    Nodes.getIdentifierNode(indexIdentifierName),
                    Nodes.getLiteralNode(0)
                )
            ]),
            Nodes.getWhileStatementNode(
                Nodes.getLiteralNode(true),
                Nodes.getBlockStatementNode([
                    Nodes.getSwitchStatementNode(
                        Nodes.getMemberExpressionNode(
                            Nodes.getIdentifierNode(controllerIdentifierName),
                            Nodes.getUpdateExpressionNode(
                                '++',
                                Nodes.getIdentifierNode(indexIdentifierName)
                            ),
                            true
                        ),
                        this.shuffledKeys.map((key: number, index: number) => {
                            return Nodes.getSwitchCaseNode(
                                Nodes.getLiteralNode(String(index)),
                                [
                                    this.blockStatementBody[key],
                                    Nodes.getContinueStatement()
                                ]
                            );
                        })
                    ),
                    Nodes.getBreakStatement()
                ])
            )
        ]);
 
        NodeUtils.parentize(structure);
 
        return [structure];
    }
}