@cspell/eslint-plugin
Version:
71 lines • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.walkTree = walkTree;
// const logger = getDefaultLogger();
// const log = logger.log;
const debugMode = false;
function walkTree(node, enter) {
const visited = new Set();
let pathNode = undefined;
function adjustPath(n) {
if (!n.parent || !pathNode) {
pathNode = n;
n.prev = undefined;
return n;
}
if (pathNode.node === n.parent) {
n.prev = pathNode;
pathNode = n;
return n;
}
while (pathNode && pathNode.node !== n.parent) {
pathNode = pathNode.prev;
}
n.prev = pathNode;
pathNode = n;
return n;
}
walk(node, ({ node, parent, key, index }) => {
if (key === 'tokens' || key === 'parent' || visited.has(node)) {
return false;
}
visited.add(node);
const path = adjustPath({ node, parent, key, index, prev: undefined });
enter(path);
return true;
});
}
function walk(root, enter) {
const useFx = debugMode ? '' : 'walkNodes';
function walkNodes(pfx, node, parent, key, index) {
const goIn = enter({ node, parent, key, index });
// log('walk: %o', { pfx, type: node.type, key, index, goIn });
if (!goIn)
return;
const n = node;
for (const key of Object.keys(n)) {
const v = n[key];
const fx = useFx || pfx + `.${node.type}[${key}]`;
if (Array.isArray(v)) {
for (let i = 0; i < v.length; ++i) {
const vv = v[i];
if (!isNode(vv))
continue;
walkNodes(fx, vv, node, key, i);
}
}
else if (isNode(v)) {
walkNodes(fx, v, node, key, undefined);
}
}
return true;
}
walkNodes('root', root, undefined, undefined, undefined);
}
function isNode(node) {
if (!node)
return false;
const n = node;
return (typeof n === 'object' && typeof n['type'] === 'string') || false;
}
//# sourceMappingURL=walkTree.cjs.map