{"id":13482743,"url":"https://github.com/syntax-tree/estree-util-to-js","last_synced_at":"2025-10-08T01:44:26.166Z","repository":{"id":57701139,"uuid":"497704850","full_name":"syntax-tree/estree-util-to-js","owner":"syntax-tree","description":"estree (and esast) utility to serialize as JavaScript","archived":false,"fork":false,"pushed_at":"2023-07-31T10:25:15.000Z","size":49,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-07-12T07:38:02.447Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-29T20:40:45.000Z","updated_at":"2025-06-05T06:17:18.000Z","dependencies_parsed_at":"2024-06-18T17:06:34.082Z","dependency_job_id":"78fce40b-1933-45bf-a95e-58d5d48886f1","html_url":"https://github.com/syntax-tree/estree-util-to-js","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"1ffa93f86d69e9d94e0a82d65d78afa2e6296aaa"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/estree-util-to-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-to-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-to-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-to-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-to-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/estree-util-to-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-to-js/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270352965,"owners_count":24569507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:05.095Z","updated_at":"2025-10-08T01:44:21.131Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# estree-util-to-js\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[estree][] (and [esast][]) utility to serialize estrees as JavaScript.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`toJs(tree[, options])`](#tojstree-options)\n    *   [`jsx`](#jsx)\n    *   [`Handler`](#handler)\n    *   [`Handlers`](#handlers)\n    *   [`Map`](#map)\n    *   [`Options`](#options)\n    *   [`Result`](#result)\n    *   [`State`](#state)\n*   [Examples](#examples)\n    *   [Example: source maps](#example-source-maps)\n    *   [Example: comments](#example-comments)\n    *   [Example: JSX](#example-jsx)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a utility that turns an estree syntax tree into a string of\nJavaScript.\n\n## When should I use this?\n\nYou can use this utility when you want to get the serialized JavaScript that is\nrepresented by the syntax tree, either because you’re done with the syntax tree,\nor because you’re integrating with another tool that does not support syntax\ntrees.\n\nThis utility is particularly useful when integrating with other unified tools,\nsuch as unist and vfile.\n\nThe utility [`esast-util-from-js`][esast-util-from-js] does the inverse of this\nutility.\nIt turns JS into esast.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install estree-util-to-js\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toJs} from 'https://esm.sh/estree-util-to-js@2'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toJs} from 'https://esm.sh/estree-util-to-js@2?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport fs from 'node:fs/promises'\nimport {parse} from 'acorn'\nimport {toJs} from 'estree-util-to-js'\n\nconst file = String(await fs.readFile('index.js'))\n\nconst tree = parse(file, {ecmaVersion: 2022, sourceType: 'module', locations: true})\n\n// @ts-expect-error: acorn is funky but it works fine.\nconsole.log(toJs(tree))\n```\n\nYields:\n\n```js\n{\n  value: \"export {toJs} from './lib/index.js';\\nexport {jsx} from './lib/jsx.js';\\n\",\n  map: undefined\n}\n```\n\n## API\n\nThis package exports the identifiers [`jsx`][api-jsx] and [`toJs`][api-to-js].\nThere is no default export.\n\n### `toJs(tree[, options])`\n\nSerialize an estree as JavaScript.\n\n###### Parameters\n\n*   `tree` ([`Program`][program])\n    — estree\n*   `options` ([`Options`][api-options])\n    — configuration\n\n###### Returns\n\nResult, optionally with source map ([`Result`][api-result]).\n\n### `jsx`\n\nMap of handlers to handle the nodes of JSX extensions in JavaScript\n([`Handlers`][api-handlers]).\n\n### `Handler`\n\nHandle a particular node (TypeScript type).\n\n###### Parameters\n\n*   `this` (`Generator`)\n    — `astring` generator\n*   `node` ([`Node`][node])\n    — node to serialize\n*   `state` ([`State`][api-state])\n    — info passed around\n\n###### Returns\n\nNothing (`undefined`).\n\n### `Handlers`\n\nHandlers of nodes (TypeScript type).\n\n###### Type\n\n```ts\ntype Handlers = Partial\u003cRecord\u003cNode['type'], Handler\u003e\u003e\n```\n\n### `Map`\n\nRaw source map from `source-map` (TypeScript type).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n*   `SourceMapGenerator` ([`SourceMapGenerator`][source-map])\n    — generate a source map with this class\n*   `filePath` (`string`)\n    — path to original input file\n*   `handlers` ([`Handlers`][api-handlers])\n    — extra handlers\n\n### `Result`\n\nResult (TypeScript type).\n\n###### Fields\n\n*   `value` (`string`)\n    — serialized JavaScript\n*   `map` ([`Map`][api-map] or `undefined`)\n    — source map as (parsed) JSON\n\n### `State`\n\nState from `astring` (TypeScript type).\n\n## Examples\n\n### Example: source maps\n\nSource maps are supported when passing the `SourceMapGenerator` class from\n[`source-map`][source-map].\nYou should also pass `filePath`.\nModified example from § Use above:\n\n```diff\n import fs from 'node:fs/promises'\n import {parse} from 'acorn'\n+import {SourceMapGenerator} from 'source-map'\n import {toJs} from 'estree-util-to-js'\n\n-const file = String(await fs.readFile('index.js'))\n+const filePath = 'index.js'\n+const file = String(await fs.readFile(filePath))\n\n const tree = parse(file, {\n   ecmaVersion: 2022,\n@@ -11,4 +13,4 @@ const tree = parse(file, {\n })\n\n // @ts-expect-error: acorn is funky but it works fine.\n-console.log(toJs(tree))\n+console.log(toJs(tree, {filePath, SourceMapGenerator}))\n```\n\nYields:\n\n```js\n{\n  value: \"export {toJs} from './lib/index.js';\\nexport {jsx} from './lib/jsx.js';\\n\",\n  map: {\n    version: 3,\n    sources: [ 'index.js' ],\n    names: [],\n    mappings: 'QAOQ,WAAW;QACX,UAAU',\n    file: 'index.js'\n  }\n}\n```\n\n### Example: comments\n\nTo get comments to work, they have to be inside the tree.\nThis is not done by Acorn.\n[`estree-util-attach-comments`][estree-util-attach-comments] can do that.\nModified example from § Use above:\n\n```diff\n import fs from 'node:fs/promises'\n import {parse} from 'acorn'\n+import {attachComments} from 'estree-util-attach-comments'\n import {toJs} from 'estree-util-to-js'\n\n const file = String(await fs.readFile('index.js'))\n\n+/** @type {Array\u003cimport('estree-jsx').Comment\u003e} */\n+const comments = []\n const tree = parse(file, {\n   ecmaVersion: 2022,\n   sourceType: 'module',\n-  locations: true\n+  locations: true,\n+  // @ts-expect-error: acorn is funky these comments are fine.\n+  onComment: comments\n })\n+attachComments(tree, comments)\n\n // @ts-expect-error: acorn is funky but it works fine.\n console.log(toJs(tree))\n```\n\nYields:\n\n```js\n{\n  value: '/**\\n' +\n    \"* @typedef {import('./lib/index.js').Options} Options\\n\" +\n    \"* @typedef {import('./lib/types.js').Handler} Handler\\n\" +\n    \"* @typedef {import('./lib/types.js').Handlers} Handlers\\n\" +\n    \"* @typedef {import('./lib/types.js').State} State\\n\" +\n    '*/\\n' +\n    \"export {toJs} from './lib/index.js';\\n\" +\n    \"export {jsx} from './lib/jsx.js';\\n\",\n  map: undefined\n}\n```\n\n### Example: JSX\n\nTo get JSX to work, handlers need to be registered.\nThis is not done by default, but they are exported as `jsx` and can be passed.\nModified example from § Use above:\n\n```diff\n import fs from 'node:fs/promises'\n-import {parse} from 'acorn'\n-import {toJs} from 'estree-util-to-js'\n+import {Parser} from 'acorn'\n+import acornJsx from 'acorn-jsx'\n+import {jsx, toJs} from 'estree-util-to-js'\n\n-const file = String(await fs.readFile('index.js'))\n+const file = '\u003c\u003e{1 + 1}\u003c/\u003e'\n\n-const tree = parse(file, {\n+const tree = Parser.extend(acornJsx()).parse(file, {\n   ecmaVersion: 2022,\n   sourceType: 'module',\n   locations: true\n })\n\n // @ts-expect-error: acorn is funky but it works fine.\n-console.log(toJs(tree))\n+console.log(toJs(tree, {handlers: jsx}))\n```\n\nYields:\n\n```js\n{ value: '\u003c\u003e{1 + 1}\u003c/\u003e;\\n', map: undefined }\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Handler`][api-handler],\n[`Handlers`][api-handlers],\n[`Map`][api-map],\n[`Options`][api-options],\n[`Result`][api-result], and\n[`State`][api-state].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `estree-util-to-js@^2`,\ncompatible with Node.js 16.\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/estree-util-to-js/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/estree-util-to-js/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-to-js.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/estree-util-to-js\n\n[downloads-badge]: https://img.shields.io/npm/dm/estree-util-to-js.svg\n\n[downloads]: https://www.npmjs.com/package/estree-util-to-js\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=estree-util-to-js\n\n[size]: https://bundlejs.com/?q=estree-util-to-js\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[esast]: https://github.com/syntax-tree/esast\n\n[esast-util-from-js]: https://github.com/syntax-tree/esast-util-from-js\n\n[estree]: https://github.com/estree/estree\n\n[estree-util-attach-comments]: https://github.com/syntax-tree/estree-util-attach-comments\n\n[program]: https://github.com/estree/estree/blob/master/es2015.md#programs\n\n[node]: https://github.com/estree/estree/blob/master/es5.md#node-objects\n\n[source-map]: https://github.com/mozilla/source-map\n\n[api-jsx]: #jsx\n\n[api-to-js]: #tojstree-options\n\n[api-handler]: #handler\n\n[api-handlers]: #handlers\n\n[api-map]: #map\n\n[api-options]: #options\n\n[api-state]: #state\n\n[api-result]: #result\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["JavaScript","esast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Festree-util-to-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Festree-util-to-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Festree-util-to-js/lists"}