Code coverage report for src/node-transformers/converting-transformers/TemplateLiteralTransformer.ts

# All files / src/node-transformers/converting-transformers TemplateLiteralTransformer.ts

100% Statements 31/31
100% Branches 15/15
100% Functions 5/5
100% Lines 28/28

    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
    1011x
    1x

    1x
    1x
    1x

    1x

    6262x

    6x

    6262x

    665151x
    4x

    4x

    4x

    4x
    9x

    9x

    9x
    4x

    5x

    4x
    14x

    4x

    1x

    4x
    3x

    3x
    1x

    1x

     import { injectable, inject } from 'inversify';
    import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';

    import * as ESTree from 'estree';

    import { IOptions } from '../../interfaces/options/IOptions';
    import { IVisitor } from '../../interfaces/IVisitor';

    import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
    import { Node } from '../../node/Node';
    import { Nodes } from '../../node/Nodes';

    /**
     * Transform ES2015 template literals to ES5
     * Thanks to Babel for algorithm
     */
    @injectable()
    export class TemplateLiteralTransformer extends AbstractNodeTransformer {
        /**
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);
        }

        /**
         * @param node
         * @return {boolean}
         */
        private static isLiteralNodeWithStringValue (node: ESTree.Node): boolean {
            return node && Node.isLiteralNode(node) && typeof node.value === 'string';
        }

        /**
         * @return {IVisitor}
         */
        public getVisitor (): IVisitor {
            return {
                enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
                    if (Node.isTemplateLiteralNode(node)) {
                        return this.transformNode(node, parentNode);
                    }
                }
            };
        }

        /**
         * @param templateLiteralNode
         * @param parentNode
         * @returns {ESTree.Node}
         */
        public transformNode (templateLiteralNode: ESTree.TemplateLiteral, parentNode: ESTree.Node): ESTree.Node {
            const templateLiteralExpressions: ESTree.Expression[] = templateLiteralNode.expressions;

            let nodes: (ESTree.Literal | ESTree.Expression)[] = [];

            templateLiteralNode.quasis.forEach((templateElement: ESTree.TemplateElement) => {
                nodes.push(Nodes.getLiteralNode(templateElement.value.cooked));

                const expression: ESTree.Expression | undefined = templateLiteralExpressions.shift();

                if (!expression) {
                    return;
                }

                nodes.push(expression);
            });

            nodes = nodes.filter((node: ESTree.Literal | ESTree.Expression) => {
                return !(Node.isLiteralNode(node) && node.value === '');
            });

            // since `+` is left-to-right associative
            // ensure the first node is a string if first/second isn't
            if (
                !TemplateLiteralTransformer.isLiteralNodeWithStringValue(nodes[0]) &&
                !TemplateLiteralTransformer.isLiteralNodeWithStringValue(nodes[1])
            ) {
                nodes.unshift(Nodes.getLiteralNode(''));
            }

            if (nodes.length > 1) {
                let root: ESTree.BinaryExpression = Nodes.getBinaryExpressionNode(
                    '+',
                    <ESTree.Literal>nodes.shift(),
                    <ESTree.Expression>nodes.shift()
                );

                nodes.forEach((node: ESTree.Literal | ESTree.Expression) => {
                    root = Nodes.getBinaryExpressionNode('+', root, <ESTree.Literal | ESTree.Expression>node);
                });

                return root;
            }

            return nodes[0];
        }
    }