ESTree to ESAST Transformer

2.0.0 · active · verified Sun Apr 19

esast-util-from-estree is a utility within the unified collective's ecosystem, specifically designed to convert an ESTree-compliant Abstract Syntax Tree (AST) into an esast, which is a unist-compliant AST for ECMAScript. Currently stable at version 2.0.0, this package plays a crucial role in enabling unist utilities to process JavaScript ASTs. It ensures that the transformed nodes are plain JSON objects, incorporates unist positional information, normalizes specific fields like `.bigint`, and prunes discouraged or redundant fields such as `attributes` and `selfClosing` from JSXOpeningFragment nodes to ensure strict unist compatibility. The package maintains the common release cadence of the unified collective, with major versions like v2.0.0 introducing breaking changes such as the shift to ESM-only distribution and requiring Node.js 16 or newer. Its primary differentiator is providing a robust, standard-compliant bridge between the ESTree format, typically generated by parsers like Acorn, and the unist specification.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a JavaScript code string with Acorn to create an ESTree, then transforming that ESTree into an esast using `esast-util-from-estree`.

import { parse } from 'acorn';
import { fromEstree } from 'esast-util-from-estree';

// Make acorn support comments and positional info.
/** @type {Array<import('acorn').Comment>} */
const comments = [];
/** @type {import('estree').Program} */
// @ts-expect-error: acorn looks like estree.
const estree = parse(
  'export function x() { /* Something senseless */ console.log(/(?:)/ + 1n) }',
  {
    sourceType: 'module',
    ecmaVersion: 'latest',
    locations: true,
    onComment: comments
  }
);
estree.comments = comments;

// Transform the estree to an esast. Note: v2.0.0+ requires using the return value.
const esast = fromEstree(estree);

console.log(JSON.stringify(esast, null, 2));

view raw JSON →