All files / tardis traverse.js

81.03% Statements 47/58
75.61% Branches 31/41
90% Functions 9/10
83.02% Lines 44/53
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  175x 2120x 2326x   4426x 2120x     175x 175x 175x 175x 175x 175x   732x 732x     732x 732x 732x 732x       1388x 1388x 1388x 426x     426x     426x     426x 426x       426x 73x 21x     52x   73x         732x 732x 732x 732x 522x 512x 97x   628x 31x       625x 656x 636x     636x     175x  
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("./path");
const types_1 = require("./types");
const lodash_1 = require("lodash");
const createDebug = require("debug");
const debug = createDebug('tardis');
function traverse(tree, options) {
    return __awaiter(this, void 0, void 0, function* () {
        Iif (!tree || !tree.type) {
            throw new Error('Tried to traverse invalid node');
        }
        const validate = options.validate || types_1.DefaultNodeValidator;
        const visitAll = options.visitor['*'];
        const treePath = new path_1.Path(tree, options.parentPath, options.pathToNode);
        Iif (typeof tree.children !== 'function') {
            throw new Error(`Node of type ${tree.type} is missing implementation for .children() - please provide one`);
        }
        function subtraverse(list) {
            return __awaiter(this, void 0, void 0, function* () {
                debug('subtraversing: %j', list);
                for (const { pathToChild, child } of list) {
                    Iif (!child) {
                        throw new Error(`Missing 'child' when subtraversing children of ${tree.type}`);
                    }
                    Iif (!pathToChild) {
                        throw new Error(`Missing 'pathToChild' when subtraversing child ${child.type} of ${tree.type}`);
                    }
                    Iif (!child.type || !validate(child)) {
                        throw new Error(`Node is using an unsupported type: "${child.type}"`);
                    }
                    debug('traversing subtree: %s', child.type);
                    const newChild = yield traverse(child, Object.assign({}, options, {
                        pathToNode: pathToChild,
                        parentPath: treePath,
                    }));
                    if (newChild !== child) {
                        if (newChild) {
                            debug('replacing subtree of %s in %s.%s', newChild.type, tree.type, pathToChild);
                        }
                        else {
                            debug('removing node of type %s from %s', child.type, tree.type);
                        }
                        lodash_1.set(tree, pathToChild, newChild);
                    }
                }
            });
        }
        yield subtraverse(tree.children());
        treePath.removed();
        const visit = options.visitor[tree.type];
        debug('visiting: %s (%s)', tree.type, !!visit);
        if (visit) {
            yield visit(treePath, options.state);
            if (!treePath.node)
                return;
        I}
        if (visitAll) {
            yield visitAll(treePath, options.state);
            if (!treePath.node)
                return;
        }
        yield subtraverse(treePath.added());
        Iif (!treePath.node.type || !validate(treePath.node)) {
            throw new Error(`Node is using an unsupported type: "${treePath.node.type}"`);
        }
        return treePath.node;
    });
}
exports.traverse = traverse;
//# sourceMappingURL=traverse.js.map