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-traverseVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use the `traverse` function to navigate a nested object, identify specific keys, and record their paths.
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`.
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;`.
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`.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()`.
No dependency data recorded yet.