"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 |