You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

53 lines
998 B

'use strict';
/* Expose. */
module.exports = visit;
/* Visit. */
function visit(tree, type, visitor, reverse) {
if (typeof type === 'function') {
reverse = visitor;
visitor = type;
type = null;
}
one(tree);
/* Visit a single node. */
function one(node, index, parent) {
var result;
index = index || (parent ? 0 : null);
if (!type || node.type === type) {
result = visitor(node, index, parent || null);
}
if (node.children && result !== false) {
return all(node.children, node);
}
return result;
}
/* Visit children in `parent`. */
function all(children, parent) {
var step = reverse ? -1 : 1;
var max = children.length;
var min = -1;
var index = (reverse ? max : min) + step;
var child;
while (index > min && index < max) {
child = children[index];
if (child && one(child, index, parent) === false) {
return false;
}
index += step;
}
return true;
}
}