@knighted/walk
Version:
Walk an oxc-parser AST with nodes typed correctly.
96 lines (95 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.walk = exports.asyncWalk = exports.asyncAncestorWalk = exports.ancestorWalk = void 0;
const walk = async (ast, opts) => {
const {
walk: _walk
} = await import('estree-walker');
return _walk(ast, {
enter(node, parent, prop, index) {
if (opts.enter) {
opts.enter.call(this, node, parent, prop, index);
}
},
leave(node, parent, prop, index) {
if (opts.leave) {
opts.leave.call(this, node, parent, prop, index);
}
}
});
};
exports.walk = walk;
const ancestorWalk = async (ast, opts) => {
const {
walk: _walk
} = await import('estree-walker');
const ancestors = [];
let isNew = false;
return _walk(ast, {
enter(node, parent, prop, index) {
isNew = node !== ancestors[ancestors.length - 1];
if (isNew) {
ancestors.push(node);
}
if (opts.enter) {
opts.enter.call(this, node, ancestors, prop, index);
}
},
leave(node, parent, prop, index) {
if (opts.leave) {
opts.leave.call(this, node, ancestors, prop, index);
}
if (isNew) {
ancestors.pop();
}
}
});
};
exports.ancestorWalk = ancestorWalk;
const asyncWalk = async (ast, opts) => {
const {
asyncWalk: _asyncWalk
} = await import('estree-walker');
return _asyncWalk(ast, {
async enter(node, parent, prop, index) {
if (opts.enter) {
await opts.enter.call(this, node, parent, prop, index);
}
},
async leave(node, parent, prop, index) {
if (opts.leave) {
await opts.leave.call(this, node, parent, prop, index);
}
}
});
};
exports.asyncWalk = asyncWalk;
const asyncAncestorWalk = async (ast, opts) => {
const {
asyncWalk: _asyncWalk
} = await import('estree-walker');
const ancestors = [];
let isNew = false;
return _asyncWalk(ast, {
async enter(node, parent, prop, index) {
isNew = node !== ancestors[ancestors.length - 1];
if (isNew) {
ancestors.push(node);
}
if (opts.enter) {
await opts.enter.call(this, node, ancestors, prop, index);
}
},
async leave(node, parent, prop, index) {
if (opts.leave) {
await opts.leave.call(this, node, ancestors, prop, index);
}
if (isNew) {
ancestors.pop();
}
}
});
};
exports.asyncAncestorWalk = asyncAncestorWalk;