All files / dist/src match.js

0% Statements 0/42
0% Branches 0/30
0% Functions 0/8
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                           
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var matchers_1 = require("./matchers");
var traverse_1 = require("./traverse");
function match(ast, selector) {
    var ancestry = [];
    var results = [];
    if (!selector) {
        return results;
    }
    var altSubjects = subjects(selector);
    traverse_1.traverse(ast, {
        enter: function (node, parent) {
            if (parent != null) {
                ancestry.unshift(parent);
            }
            if (findMatches(node, selector, ancestry)) {
                if (altSubjects.length) {
                    altSubjects.forEach(function (subject) {
                        if (findMatches(node, subject, ancestry)) {
                            results.push(node);
                        }
                        ancestry.forEach(function (ancestor, index) {
                            if (findMatches(ancestor, subject, ancestry.slice(index + 1))) {
                                results.push(ancestor);
                            }
                        });
                    });
                }
                else {
                    results.push(node);
                }
            }
        },
        leave: function () {
            ancestry.shift();
        }
    });
    return results;
}
exports.match = match;
function findMatches(node, selector, ancestry) {
    if (ancestry === void 0) { ancestry = []; }
    if (!selector) {
        return true;
    }
    if (!node) {
        return false;
    }
    var matcher = matchers_1.MATCHERS[selector.type];
    if (matcher) {
        return matcher(node, selector, ancestry);
    }
    throw new Error("Unknown selector type: " + selector.type);
}
exports.findMatches = findMatches;
function subjects(selector, ancestor) {
    if (selector == null || typeof selector !== 'object') {
        return [];
    }
    if (ancestor == null) {
        ancestor = selector;
    }
    var results = selector.subject ? [ancestor] : [];
    Object.keys(selector).forEach(function (key) {
        results = results.concat(subjects(selector[key], key === 'left' ? selector[key] : ancestor));
    });
    return results;
}
//# sourceMappingURL=match.js.map