unist-util-position-from-estree is a specialized utility that converts an ESTree (ECMAScript Abstract Syntax Tree) node into a unist (Universal Syntax Tree) position object. This package is crucial for projects that need to bridge the gap between JavaScript parsing tools (like Acorn) which produce ESTree, and the broader unist ecosystem used by tools like remark, rehype, and retext. The current stable version is 2.0.0. As part of the unified collective, it follows a release cadence tied to breaking changes in its dependencies or Node.js compatibility. Its key differentiator is providing a standardized way to represent code locations from ESTree within the unist data model, enabling interoperability with other unist utilities and plugins. It is ESM-only and requires Node.js 16+.
npm install unist-util-position-from-estreeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse JavaScript code using Acorn, and then convert specific ESTree nodes into unist position objects using `positionFromEstree`.
Update your code to expect and handle `undefined` when an invalid ESTree node or node without location information is passed. E.g., `const pos = positionFromEstree(node); if (pos) { /* use pos */ }`Ensure your project runs on Node.js 16 or higher. Migrate your import statements from CommonJS `require()` to ESM `import` statements.
Only use the public API as exposed through the main entry point. Avoid importing from deep paths (e.g., `unist-util-position-from-estree/lib/some-internal-module`).
Ensure your project's `@types/unist` dependency is also updated to a compatible version, ideally the latest stable version.
When using a parser to generate ESTree nodes, ensure you pass the necessary options (e.g., `{ locations: true }` for Acorn) to include position information in the nodes.Convert your file and import statements to use ES Modules syntax: `import { positionFromEstree } from 'unist-util-position-from-estree';` and ensure your `package.json` has `"type": "module"` or your file ends with `.mjs`.Remove any deep imports. Import only from the main package entry point: `import { positionFromEstree } from 'unist-util-position-from-estree';`Ensure the ESTree node passed to `positionFromEstree` contains location data (e.g., from a parser configured with `locations: true`). Always check if the return value is `undefined` before accessing its properties: `const pos = positionFromEstree(node); if (pos) { console.log(pos.start); }`No dependency data recorded yet.