Unist Utility: Position from ESTree Node
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+.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ESM-only package.fixConvert 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`. -
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/index.js' is not exported from package
cause Trying to import from an internal path that is no longer exposed due to `exports` map.fixRemove any deep imports. Import only from the main package entry point: `import { positionFromEstree } from 'unist-util-position-from-estree';` -
TypeError: Cannot read properties of undefined (reading 'start')
cause Calling `positionFromEstree` with an ESTree node that lacks location information or is invalid, and then attempting to access properties on the `undefined` return value.fixEnsure 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); }`
Warnings
- breaking Version 2.0.0 changes the return type of `positionFromEstree` for invalid points or positions. It now explicitly returns `undefined` instead of potentially incomplete or malformed position objects.
- breaking Version 2.0.0 drops support for Node.js versions prior to 16. It is now ESM-only.
- breaking The package now uses `export` maps, which means direct access to internal, non-exported paths is no longer supported and will break.
- breaking The `@types/unist` dependency has been updated in v2.0.0. Incompatible versions of `@types/unist` can lead to type errors.
- gotcha For `positionFromEstree` to return meaningful position data, the ESTree node must have `loc` and `range` properties. These are typically generated by parsers like Acorn when configured with `locations: true` and/or `ranges: true`.
Install
-
npm install unist-util-position-from-estree -
yarn add unist-util-position-from-estree -
pnpm add unist-util-position-from-estree
Imports
- positionFromEstree
const { positionFromEstree } = require('unist-util-position-from-estree')import { positionFromEstree } from 'unist-util-position-from-estree' - positionFromEstree (TypeScript)
import type { Position } from 'unist'; import { positionFromEstree } from 'unist-util-position-from-estree'
Quickstart
import { parse } from 'acorn';
import { positionFromEstree } from 'unist-util-position-from-estree';
// Acorn requires `locations: true` to generate position data.
const code = 'function example() { console.log("Hello"); }';
const node = parse(code, {
ecmaVersion: 2020,
locations: true, // Crucial for position information
sourceType: 'module'
});
console.log('Position for entire program:', positionFromEstree(node));
console.log('Position for function name:', positionFromEstree(node.body[0].id));
console.log('Position for console.log call:', positionFromEstree(node.body[0].body.body[0].expression));