estree-util-to-js

Serializes estree (and esast) syntax trees back into JavaScript source code

Library
npm
v2.0.0
17stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
25/100Needs Attention
Development Activity0
Maintenance20
Community12
Maturity56
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture70
Code Quality82
Innovation55
Learning Curve85

estree-util-to-js is a utility that turns an estree (or esast) abstract syntax tree into a string of JavaScript source code. It is the inverse operation of parsing: instead of turning JS into a tree, it turns a tree back into JS, making it useful when a syntax-tree-based tool is done transforming code and needs to hand off a plain string to something that doesn’t understand ASTs.

Part of the syntax-tree/unified ecosystem, it is commonly used by tools like MDX and other remark/unified-based compilers that build or transform JavaScript ASTs and need to emit the final source. It supports source maps and JSX serialization out of the box, built on top of astring for the core code-generation work.

What You Get

  • A toJs() function that serializes any estree or esast tree into JavaScript source text
  • Optional source map generation alongside the serialized output
  • Built-in JSX node serialization via the exported jsx handler set
  • Extensible Handler/Handlers/Map types for serializing custom or non-standard AST node types
  • ESM-only package with full TypeScript type definitions and 100% type coverage enforced in CI

Common Use Cases

  • Emitting final JavaScript output from MDX or other unified/remark-based compilers after AST transformation
  • Converting a syntax tree produced by tools like esast-util-from-js back into a JS string for bundlers or runtimes that expect source text
  • Building custom code-generation tools that need to hand off generated JavaScript with accurate source maps
  • Serializing programmatically-constructed or transformed JSX/JS ASTs as part of a build pipeline

Under The Hood

Architecture: The package is deliberately tiny, split into two files under lib/index.js (145 lines) implements the core toJs() entry point, handler dispatch, and source-map wiring, while jsx.js (364 lines) adds JSX-specific serialization handlers layered on top of the base estree handlers, so JSX support is additive rather than baked into the core walker. Tech Stack: Pure JavaScript (ESM-only, Node.js 16+), delegating the actual code-generation work to astring and using source-map for source-map construction and @types/estree-jsx for types; it has no other runtime dependencies, keeping the dependency surface minimal for a package with extremely high weekly download volume. Code Quality: The test/index.js suite is run with c8 --100 coverage enforcement and the build pipeline additionally runs type-coverage at 100% strictness plus xo/prettier linting and formatting, reflecting the syntax-tree/unified organization’s typically rigorous quality bar even for small utility packages. API Design: The API is a single function (toJs) with an options object and typed Handler/Handlers extension points, following the same low-surface-area convention as sibling estree-util-* and unist-ecosystem packages, which keeps the learning curve low for anyone already familiar with the unified toolchain.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search