Unist Utility: Position from ESTree Node

2.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse JavaScript code using Acorn, and then convert specific ESTree nodes into unist position objects using `positionFromEstree`.

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));

view raw JSON →