unist-util-visit-parents is a robust utility within the unist (Universal Syntax Tree) ecosystem designed for deeply traversing ASTs (Abstract Syntax Trees) while providing a full lineage of parent nodes for each visited node. This functionality is crucial for transformations or analyses that require contextual information about a node's position within the tree. The current stable version is 6.0.2, with active development evidenced by frequent minor and major releases, particularly focusing on TypeScript type improvements and ESM compatibility. It differentiates itself from `unist-util-visit` by offering an array of parent nodes, making it indispensable for scenarios where ancestral context is necessary, such as scope analysis or complex rewrite operations. The library is ESM-only and requires Node.js 16 or higher, adhering to modern JavaScript module standards.
npm install unist-util-visit-parentsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `visitParents` to traverse a Markdown AST, log node types with ancestral paths, modify nodes, and control traversal flow using `SKIP`.
Migrate your project to use ES modules (`import`/`export`) or ensure your build tools correctly transpile if targeting older environments. Update your Node.js runtime to version 16 or newer. Do not use `require()` for this package.
Ensure your tooling supports `exports` maps. Always import symbols directly from the main package entrypoint (`unist-util-visit-parents`) rather than relying on private or deep paths.
Update your TypeScript imports to use types directly from the main `unist-util-visit-parents` export. TypeScript's inference capabilities have also improved, often making explicit type imports less necessary for visitor arguments.
Review your `visitor` function signatures. TypeScript should now correctly infer types, but you might need to adjust explicit type annotations to align with the new base typing logic. Consider using type parameters with `visitParents<SpecificNodeType>(...)` for better type inference.
Carefully consider the implications of traversal order for your specific task. Preorder is generally suitable for most transformations; `reverse` is for specific cases where children need to be processed before their parents, but still in a depth-first manner.
Structure your visitors to handle multiple conditions within a single traversal loop. For example: `visitParents(tree, (node, parents) => { if (is(node, 'paragraph')) { ... } if (is(node, 'strong')) { ... } })`Change `const { visitParents } = require('unist-util-visit-parents');` to `import { visitParents } from 'unist-util-visit-parents';` and ensure your environment supports ES modules (e.g., Node.js 16+ or a bundler).Ensure you are running Node.js 16 or newer. If using a bundler (e.g., Webpack, Rollup), update it to a version that fully supports ESM and `exports` maps. Verify your `tsconfig.json` (if applicable) is configured for a modern module system (e.g., `"module": "Node16"` or `"ES2022"`).
Confirm your import statement is `import { visitParents } from 'unist-util-visit-parents';`. If you're in a CommonJS context, you cannot use this package directly; you must transition to ESM or use an older version if available (though not recommended).Ensure you are using a compatible version of `@types/unist` for your unist package. For specific node types, use type parameters with `visitParents<SpecificNodeType>(tree, 'type', visitor)` or `is()` from `unist-util-is` for more robust type checking within the visitor. Update `@types/unist` if needed, as `unist-util-visit-parents` v6.0.0 updated its dependency.
No dependency data recorded yet.