Registry / serialization / ast-monkey-traverse

ast-monkey-traverse

JSON →
library4.1.3jsnpmunverified

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.

npm install ast-monkey-traverse
INSTALL
IMPORT
SIG · AST-MONKEY-TRAVERS
A
ast-monkey-traverse
serializationjavascriptv4.1.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

traverse
import { traverse } from 'ast-monkey-traverse'
const { traverse } = require('ast-monkey-traverse')
Package is pure ESM since v3.0.0. Use `import` statements.
Callback
import type { Callback } from 'ast-monkey-traverse'
Type import for the `traverse` function's callback signature. Available when using TypeScript.
TraverseResult
import type { TraverseResult } from 'ast-monkey-traverse'
Type import for the result value returned by the `traverse` function. Available when using TypeScript.

Demonstrates how to use the `traverse` function to navigate a nested object, identify specific keys, and record their paths.

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"]);
Debug
Known issues
breakingast-monkey-traverse transitioned to pure ECMAScript Modules (ESM) starting from version 3.0.0. This change removes CommonJS support.
fix
Migrate from CommonJS `require()` to ESM `import` statements. Ensure your project is configured for ESM (e.g., by adding `"type": "module"` to your `package.json` or using `.mjs` file extensions). If migrating to ESM is not feasible, install an older CommonJS-compatible version, such as `ast-monkey-traverse@2.1.0`.
affects: >=3.0.0
gotchaThe `traverse` callback's `key` and `val` arguments behave differently when traversing array elements compared to object properties.
fix
When an array element is being processed, `val` will be `undefined`, and the array element's value will be passed as `key`. For object properties, `key` is the property name, and `val` is its value. Always check `val !== undefined` to correctly differentiate and access the current node's value, for example: `let current = val !== undefined ? val : key;`.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to use `require()` to import `ast-monkey-traverse` version 3.0.0 or later.
fix
For 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
Using an `import` statement for `ast-monkey-traverse` in a JavaScript file that is treated as CommonJS.
fix
Ensure 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()`.
Upgrade
Version history
4.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
1
Resources
ast-monkey-traverse — npm install ast-monkey-traverse · libregistry