ast-monkey-traverse
v4.3.1
Published
Utility library to traverse AST
Maintainers
Readme
No dependencies whatsoever. This package declares no dependencies or devDependencies.
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 2.1.0 (npm i [email protected]).
npm i ast-monkey-traverseQuick Take
import { strict as assert } from "node:assert";
import { traverse } from "ast-monkey-traverse";
const paths = [];
const source = {
a: {
foo: {
bar: [
{
foo: "c",
},
],
d: {
e: {
foo: "f",
},
},
},
},
};
traverse(source, (key, val, innerObj) => {
// Objects supply a property key and value; arrays supply the element as key.
let current = innerObj.parentType === "object" ? val : key;
if (
// it's object (not array)
innerObj.parentType === "object" &&
// and has the key we need
key === "foo"
) {
// push the path to array in the outer scope
paths.push(innerObj.path);
}
return current;
});
// notice object-path notation "a.foo.bar.0.foo" - array segments use dots too:
assert.deepEqual(paths, ["a.foo", "a.foo.bar.0.foo", "a.foo.d.e.foo"]);Documentation
Please visit codsen.com for a full description of the API. For Changelog, see raw md on GitHub or the website.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License
Copyright © 2010-2026 Roy Revelt and other contributors
