All files / tardis/parsers babylon.js

23.08% Statements 6/26
0% Branches 0/8
0% Functions 0/6
25% Lines 6/24
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  175x 175x 175x 175x 175x                                                                             175x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const babel_types_1 = require("babel-types");
const lodash_1 = require("lodash");
const createDebug = require("debug");
const debug = createDebug('tardis');
function getChildren(node) {
    function $(...paths) {
        return lodash_1.flatten(paths.map(pathToChild => {
            const child = lodash_1.get(node, pathToChild);
            if (Array.isArray(child)) {
                return child.map((c, index) => ({
                    child: c,
                    pathToChild: `${pathToChild}.${index}`,
                }));
            }
            if (!child) {
                return [];
            }
            return {
                child,
                pathToChild,
            };
        }));
    }
    if (!babel_types_1.VISITOR_KEYS[node.type]) {
        return [];
    }
    return $(...babel_types_1.VISITOR_KEYS[node.type]);
}
function fromBabylon(tree) {
    const node = Object.assign({
        children: null,
    }, tree);
    debug('looking to inject into: %s', tree.type);
    if (!node.children) {
        node.children = () => getChildren(node);
        for (const { child, pathToChild } of node.children()) {
            debug('injecting children finder into %s.%s => %s', node.type, pathToChild, child.type);
            lodash_1.set(node, pathToChild, fromBabylon(child));
        }
    }
    return node;
}
exports.fromBabylon = fromBabylon;
//# sourceMappingURL=babylon.js.map