AST Monkey Traverse
ast-monkey-traverse is a utility library designed for traversing Abstract Syntax Trees (ASTs) and general JavaScript object/array structures recursively. The current stable version is 4.1.3. As part of the Codsen monorepo, it receives regular updates and maintenance. Its core functionality offers a simple API, `traverse`, which allows users to visit each node in a data structure, providing access to the current key, value, and an internal object containing contextual information like the current path in object-path notation. A key differentiator is its clear distinction in the callback for object keys/values versus array indices, and its explicit focus on pure ESM since version 3.0.0, which streamlined its module architecture. It also provides built-in TypeScript types, enhancing developer experience for type-safe applications.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `ast-monkey-traverse` version 3.0.0 or later.fixFor versions `>=3.0.0`, ensure your project uses ESM and change `require('ast-monkey-traverse')` to `import { traverse } from 'ast-monkey-traverse'`. If CommonJS is required, install `ast-monkey-traverse@2.1.0`. -
SyntaxError: Cannot use import statement outside a module
cause Using an `import` statement for `ast-monkey-traverse` in a JavaScript file that is treated as CommonJS.fixEnsure your `package.json` contains `"type": "module"` for your project, or rename the file to have a `.mjs` extension to signal it as an ESM module. Alternatively, for CommonJS environments, install `ast-monkey-traverse@2.1.0` and use `require()`.
Warnings
- breaking ast-monkey-traverse transitioned to pure ECMAScript Modules (ESM) starting from version 3.0.0. This change removes CommonJS support.
- gotcha The `traverse` callback's `key` and `val` arguments behave differently when traversing array elements compared to object properties.
Install
-
npm install ast-monkey-traverse -
yarn add ast-monkey-traverse -
pnpm add ast-monkey-traverse
Imports
- traverse
const { traverse } = require('ast-monkey-traverse')import { traverse } from 'ast-monkey-traverse' - Callback
import type { Callback } from 'ast-monkey-traverse' - TraverseResult
import type { TraverseResult } from 'ast-monkey-traverse'
Quickstart
import { strict as assert } from "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) => {
// if currently an object is traversed, you get both "key" and "val"
// if it's array, only "key" is present, "val" is undefined
let current = val !== undefined ? val : key;
if (
// it's object (not array)
val !== undefined &&
// 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"]);