{"id":13717045,"url":"https://github.com/syntax-tree/esast-util-from-js","last_synced_at":"2025-07-12T07:38:03.162Z","repository":{"id":46269011,"uuid":"498046790","full_name":"syntax-tree/esast-util-from-js","owner":"syntax-tree","description":"estree (and esast) utility to parse from JavaScript","archived":false,"fork":false,"pushed_at":"2023-07-31T08:41:44.000Z","size":67,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-06-04T23:20:50.276Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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}},"created_at":"2022-05-30T17:58:13.000Z","updated_at":"2024-11-26T22:32:10.000Z","dependencies_parsed_at":"2024-01-14T22:13:43.405Z","dependency_job_id":null,"html_url":"https://github.com/syntax-tree/esast-util-from-js","commit_stats":{"total_commits":22,"total_committers":1,"mean_commits":22.0,"dds":0.0,"last_synced_commit":"dcdf080ec71ae0298f8096cf55957045a2f7e561"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/esast-util-from-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fesast-util-from-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fesast-util-from-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fesast-util-from-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fesast-util-from-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/esast-util-from-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fesast-util-from-js/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264931658,"owners_count":23685066,"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","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-08-03T00:01:17.293Z","updated_at":"2025-07-12T07:38:03.129Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# esast-util-from-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[esast][] (and [estree][]) utility to parse trees from 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    *   [`fromJs(value[, options])`](#fromjsvalue-options)\n    *   [`Options`](#options)\n    *   [`Plugin`](#plugin)\n    *   [`Value`](#value)\n    *   [`Version`](#version-1)\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 a string of JavaScript into an esast\n(estree with some extra cleanliness) syntax tree.\n\n## When should I use this?\n\nYou can use this utility when you want to deal with ASTs of JavaScript\ncombined with other [unist][] and [`vfile`][vfile] things.\nYou can use [`acorn`][acorn] itself if you don’t care about unified.\n\nThe utility [`estree-util-to-js`][estree-util-to-js] does the inverse of this\nutility.\nIt turns the tree into a string of JavaScript.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install esast-util-from-js\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {fromJs} from 'https://esm.sh/esast-util-from-js@2'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {fromJs} from 'https://esm.sh/esast-util-from-js@2?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport fs from 'node:fs/promises'\nimport {fromJs} from 'esast-util-from-js'\n\nconst tree = fromJs(await fs.readFile('example.js'), {module: true})\n\nconsole.log(tree)\n```\n\nYields:\n\n```js\n{\n  type: 'Program',\n  body: [\n    {\n      type: 'ImportDeclaration',\n      specifiers: [Array],\n      source: [Object],\n      position: [Object]\n    },\n    {\n      type: 'ImportDeclaration',\n      specifiers: [Array],\n      source: [Object],\n      position: [Object]\n    },\n    {\n      type: 'VariableDeclaration',\n      declarations: [Array],\n      kind: 'const',\n      position: [Object]\n    },\n    {\n      type: 'ExpressionStatement',\n      expression: [Object],\n      position: [Object]\n    }\n  ],\n  sourceType: 'module',\n  comments: [],\n  position: {\n    start: {line: 1, column: 1, offset: 0},\n    end: {line: 7, column: 1, offset: 157}\n  }\n}\n```\n\n## API\n\nThis package exports the identifier [`fromJs`][api-from-js].\nThere is no default export.\n\n### `fromJs(value[, options])`\n\nParse JavaScript to an esast.\n\n###### Parameters\n\n*   `value` ([`Value`][api-value])\n    — serialized JavaScript to parse\n*   `options` ([`Options`][api-options], optional)\n    — configuration\n\n###### Returns\n\nTree ([`Node`][node]).\n\n###### Throws\n\nWhen the JavaScript cannot be parsed with `acorn`, a\n[`VFileMessage`][vfile-message] is thrown.\n\nThis can for example happen when passing modern syntax (you could maybe use a\nnewer `version`, or it might be that the syntax is not yet supported), or just\notherwise invalid JavaScript (you might need a plugin).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n###### `version`\n\nJavaScript version ([`Version`][api-version], default: `'latest'`).\n\nWhen a number, must be a year in the range `2015` and `2023` (both including).\n`'latest'` is the same as passing the latest supported year.\n\n\u003e ☢️ **Danger**: `'latest'` is a sliding thing, you could consider it as\n\u003e breaking semver.\n\u003e Pass an actual year to lock that down.\n\n###### `module`\n\nWhether this is a module (ESM) or a script (`boolean`, default: `false`).\n\n###### `allowReturnOutsideFunction`\n\nWhether a return statement is allowed in the top scope (`boolean`, default:\n`false`).\n\n###### `allowImportExportEverywhere`\n\nWhether import/export statements are allowed in the every scope (`boolean`,\ndefault: `false`).\n\n###### `allowAwaitOutsideFunction`\n\nWhether `await` is allowed in the top scope (`boolean`, default: depends).\nDefaults to `version \u003e= 2022`.\n\n###### `allowSuperOutsideMethod`\n\nWhether `super` is allowed outside methods (`boolean`, default: `false`).\n\n###### `allowHashBang`\n\nWhether a shell hasbang is allowed (`boolean`, default: `false`).\n\n###### `plugins`\n\nList of acorn plugins ([`Array\u003cPlugin\u003e`][api-plugin], default: `[]`).\nExamples are [`acorn-jsx`][acorn-jsx] and [`acorn-stage3`][acorn-stage3].\n\n### `Plugin`\n\nAcorn plugin (TypeScript type).\n\n###### Type\n\n```ts\ntype Plugin = (Parser: ParserClass) =\u003e ParserClass\n```\n\n### `Value`\n\nInput value (TypeScript type).\n\nWhen a typed array, must be UTF-8.\n\n###### Type\n\n```ts\ntype Value = Uint8Array | string\n```\n\n### `Version`\n\nJavaScript version (TypeScript type).\n\n`'latest'` is equivalent to the latest supported year.\n\n###### Type\n\n```ts\ntype Version = 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 'latest'\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Options`][api-options],\n[`Plugin`][api-plugin],\n[`Value`][api-value], and\n[`Version`][api-version].\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, `esast-util-from-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/esast-util-from-js/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/esast-util-from-js/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/esast-util-from-js.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/esast-util-from-js\n\n[downloads-badge]: https://img.shields.io/npm/dm/esast-util-from-js.svg\n\n[downloads]: https://www.npmjs.com/package/esast-util-from-js\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=esast-util-from-js\n\n[size]: https://bundlejs.com/?q=esast-util-from-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[node]: https://github.com/syntax-tree/esast#node\n\n[estree]: https://github.com/estree/estree\n\n[unist]: https://github.com/syntax-tree/unist\n\n[vfile]: https://github.com/vfile/vfile\n\n[acorn]: https://github.com/acornjs/acorn\n\n[acorn-jsx]: https://github.com/acornjs/acorn-jsx\n\n[acorn-stage3]: https://github.com/acornjs/acorn-stage3\n\n[estree-util-to-js]: https://github.com/syntax-tree/estree-util-to-js\n\n[vfile-message]: https://github.com/vfile/vfile-message\n\n[api-from-js]: #fromjsvalue-options\n\n[api-options]: #options\n\n[api-plugin]: #plugin\n\n[api-value]: #value\n\n[api-version]: #version-1\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["esast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fesast-util-from-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fesast-util-from-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fesast-util-from-js/lists"}