Stryker

parserUtils.js - Stryker report

Summary

File Based on all code Based on code coverage
parserUtils.js
100%
31/31 100% 31/31

Code

"use strict";
var _ = require('lodash');
var esprima = require('esprima');
var escodegen = require('escodegen');
/**
 * Utility class for parsing and generating code.
 * @constructor
 */
var esprimaOptions = {
    comment: true,
    loc: true,
    range: true,
    tokens: true,
};
/**
 * Parses code to generate an Abstract Syntax Tree.
 * @function
 * @param code - The code which has to be parsed.
 * @returns {Object} The generated Abstract Syntax Tree.
 */
function parse(code) 0{
    if (123code === undefined) 4{
        throw new Error('Code parameter cannot be undefined');
    }

    if (567code === '') 8{
        return {};
    }

    var abstractSyntaxTree = esprima.parse(code, esprimaOptions);
    return abstractSyntaxTree;
}

exports.parse = parse;
;
/**
 * Finds all nodes which have a 'type' property and freezes them.
 * @function
 * @param abstractSyntaxTree - The current part of the abstract syntax tree which will be investigated.
 * @returns  All nodes with a type.
 */
function collectFrozenNodes(abstractSyntaxTree, nodes) 9{
    nodes = 10nodes || [];
    if (1112131415!_.isArray(abstractSyntaxTree) && _.isObject(abstractSyntaxTree) && abstractSyntaxTree.type && _.isUndefined(abstractSyntaxTree.nodeID)) 16{
        abstractSyntaxTree.nodeID = nodes.length;
        nodes.push(abstractSyntaxTree);
    }

    Object.freeze(abstractSyntaxTree);
    _.forOwn(abstractSyntaxTree, function (childNode, i) 17{
        if (181920childNode instanceof Object && !(childNode instanceof Array)) 21{
            collectFrozenNodes(childNode, nodes);
        }

        else if (2223childNode instanceof Array) 24{
            _.forEach(childNode, function (arrayChild) 25{
                if (262728arrayChild instanceof Object && !(arrayChild instanceof Array)) 29{
                    collectFrozenNodes(arrayChild, nodes);
                }

            });
        }

    });
    return nodes;
}

exports.collectFrozenNodes = collectFrozenNodes;
;
/**
   * Parses a Node to generate code.
   * @param The Node which has to be transformed into code.
   * @returns The generated code.
   */
function generate(node) 30{
    return escodegen.generate(node);
}

exports.generate = generate;
;
//# sourceMappingURL=parserUtils.js.map