All files / tardis path.js

91.76% Statements 78/85
75% Branches 30/40
92.31% Functions 12/13
91.76% Lines 78/85
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143  175x 175x 175x 175x 175x   798x     798x     798x 774x       798x         164x 164x             426x 426x 112x               732x 732x 732x 732x 732x 732x 732x 732x 426x       42x     42x 42x 42x     76x 76x 52x 52x 51x 28x 26x 24x 26x     44x 43x     73x     68x 82x 14x   66x 51x   21x     21x 21x 21x 21x 21x 21x     21x 21x 21x 79x 198x 140x     479x 479x 79x 140x 21x             479x 79x 198x 198x             551x     476x 476x 265x 241x 36x 10x 27x 48x 8x          
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const lodash_1 = require("lodash");
const createDebug = require("debug");
const debug = createDebug('tardis');
function verifyNode(node) {
    Iif (typeof node.type !== 'string' || !node.type) {
        throw new Error('Node is missing a valid type');
    }
    Iif (typeof node.children !== 'function') {
        throw new Error(`Invalid node of type ${node.type}: missing a valid children function`);
    }
    if (typeof node.toString !== 'function' || node.toString === {}.toString) {
        node.toString = () => {
            throw new Error(`Invalid node of type ${node.type}: missing a valid toString() function - will not generate`);
        };
    }
    Iif (typeof node.validate === 'function') {
        node.validate();
    }
}
function isInteger(str) {
    try {
        return !Number.isNaN(parseInt(str, 10));
    }
    catch (err) {
        return false;
    }
}
function verifyPath(pathToChild) {
    const path = pathToChild.split('.');
    for (let i = 0; i < path.length - 1; ++i) {
        Iif (isInteger(path[i])) {
            throw new Error(`Found an index partway through a child path in "${pathToChild}".` +
                `Indexes are only supported at the end of a child path.`);
        }
    }
}
class Path {
    constructor(node, parentPath, childPath) {
        this.node = node;
        this.parentPath = parentPath;
        this.childPath = childPath;
        this.unshifts = Object.create(null);
        this.shifts = Object.create(null);
        this.removals = Object.create(null);
        verifyNode(node);
        if (childPath) {
            verifyPath(childPath);
        }
    }
    replaceWith(node) {
        Iif (!node) {
            throw new Error('Node removal attempted with .replaceWith() - please use .remove() instead');
        }
        verifyNode(node);
        this.node = node;
        return this;
    }
    remove() {
        this.node = undefined;
        if (this.parentPath !== undefined) {
            const path = (this.childPath || '').split('.');
            const index = path.pop();
            if (isInteger(index)) {
                const subpath = path.join('.');
                debug('marking %s for removal on parent of type %s', this.childPath, this.parentPath.node.type);
                this.parentPath.removals[subpath] = this.parentPath.removals[subpath] || [];
                this.parentPath.removals[subpath].push(parseInt(index, 10));
            }
            else {
                lodash_1.set(this.parentPath.node, this.childPath, undefined);
                verifyNode(this.parentPath.node);
            }
        }
        return this;
    }
    findParent(parentType) {
        debug('checking %s node for parent of %s', this.node.type, parentType);
        Iif (!this.parentPath) {
            return;
        }
        if (this.parentPath.node.type === parentType) {
            return this.parentPath;
        }
        return this.parentPath.findParent(parentType);
    }
    unshiftContainer(property, node) {
        this.unshifts[property] = this.unshifts[property] || [];
        this.unshifts[property].push(node);
        assert_1.ok(Array.isArray(this.node[property]), 'Could not like an Array-like property to insert into');
        assert_1.ok(node, 'node is a required property');
        this.node[property].unshift(node);
        return this;
    }
    pushContainer(property, node) {
        this.shifts[property] = this.shifts[property] || [];
        this.shifts[property].push(node);
        assert_1.ok(Array.isArray(this.node[property]), 'Could not like an Array-like property to insert into');
        assert_1.ok(node, 'node is a required property');
        this.node[property].push(node);
        return this;
    }
    added() {
        const results = [];
        for (const property in this.unshifts) {
            Eif (this.unshifts[property] !== undefined) {
                for (let i = 0; i < this.unshifts[property].length; ++i) {
                    results.push({
                        pathToChild: `${property}.${i}`,
                        child: this.unshifts[property][i],
                    });
                }
            }
        }
        for (const property in this.shifts) {
            Eif (this.shifts[property] !== undefined) {
                for (let i = 0; i < this.shifts[property].length; ++i) {
                    results.push({
                        pathToChild: `${property}.${i}`,
                        child: this.shifts[property][i],
                    });
                }
            }
        }
        return results;
    }
    removed() {
        debug('starting child cleanup at %s =>', this.childPath, this.removals);
        for (const path in this.removals) {
            if (this.removals[path] !== undefined) {
                const removals = this.removals[path];
            E    debug('cleaning up children at %s', path);
                this.node[path] = this.node[path].filter(function filterChildren(_, index) {
                    return removals.indexOf(index) === -1;
                });
            }
        }
    }
}
exports.Path = Path;
//# sourceMappingURL=path.js.map