{"id":13717030,"url":"https://github.com/syntax-tree/estree-util-attach-comments","last_synced_at":"2025-04-11T05:34:51.917Z","repository":{"id":43396606,"uuid":"323391785","full_name":"syntax-tree/estree-util-attach-comments","owner":"syntax-tree","description":"utility to attach comments to estree nodes","archived":false,"fork":false,"pushed_at":"2023-07-31T09:11:56.000Z","size":92,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-13T15:45:55.468Z","etag":null,"topics":["ast","ecmascript","estree","javascript"],"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":{"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},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2020-12-21T16:33:41.000Z","updated_at":"2024-02-22T23:45:38.000Z","dependencies_parsed_at":"2024-01-14T22:13:43.420Z","dependency_job_id":null,"html_url":"https://github.com/syntax-tree/estree-util-attach-comments","commit_stats":{"total_commits":39,"total_committers":3,"mean_commits":13.0,"dds":0.05128205128205132,"last_synced_commit":"d8fd933bc1ce4f8c7d0e8661a3ad1ffc62c2ed14"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-attach-comments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-attach-comments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-attach-comments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-attach-comments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/estree-util-attach-comments/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248348365,"owners_count":21088858,"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":["ast","ecmascript","estree","javascript"],"created_at":"2024-08-03T00:01:17.052Z","updated_at":"2025-04-11T05:34:51.891Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# estree-util-attach-comments\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][] utility attach semistandard comment nodes (such as from [acorn][]) to\nthe nodes in that tree.\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    *   [`attachComments(tree, comments)`](#attachcommentstree-comments)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a utility that you can use to embed comment nodes *inside* a\ntree.\nThis is useful because certain estree parsers give you an array (espree and\nacorn) whereas other estree tools expect comments to be embedded on nodes in the\ntree.\n\nThis package uses one `comments` array where each comment has `leading` and\n`trailing` fields, as applied by `acorn`, but does not support the slightly\ndifferent non-standard comments made by `espree`.\n\n## When should I use this?\n\nYou can use this package when working with comments from Acorn and later working\nwith a tool such as recast or Babel.\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-attach-comments\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {attachComments} from 'https://esm.sh/estree-util-attach-comments@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {attachComments} from 'https://esm.sh/estree-util-attach-comments@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay our document `x.js` contains:\n\n```js\n/* 1 */ function /* 2 */ a /* 3 */(/* 4 */ b) /* 5 */ {\n  /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */\n}\n```\n\n…and our module `example.js` looks as follows:\n\n```js\nimport fs from 'node:fs/promises'\nimport {parse} from 'acorn'\nimport {attachComments} from 'estree-util-attach-comments'\nimport recast from 'recast'\n\nconst code = String(await fs.readFile('x.js'))\nconst comments = []\nconst tree = parse(code, {\n  sourceType: 'module',\n  ecmaVersion: 'latest',\n  onComment: comments\n})\n\nattachComments(tree, comments)\n\nconsole.log(recast.print(tree).code)\n```\n\nYields:\n\n```js\n/* 1 */\nfunction /* 2 */\na(\n    /* 3 */\n    /* 4 */\n    b\n) /* 5 */\n{\n    /* 6 */\n    return (\n        /* 7 */\n        b + /* 8 */\n        1\n    );\n}/* 9 */\n```\n\n\u003e 👉 **Note**: the lines are added by `recast` in this case.\n\u003e And, some of these weird comments are off, but they’re pretty close.\n\n## API\n\nThis package exports the identifier [`attachComments`][api-attach-comments].\nThere is no default export.\n\n### `attachComments(tree, comments)`\n\nAttach semistandard estree comment nodes to the tree.\n\nThis mutates the given [`tree`][estree].\nIt takes `comments`, walks the tree, and adds comments as close as possible\nto where they originated.\n\nComment nodes are given two boolean fields: `leading` (`true` for `/* a */ b`)\nand `trailing` (`true` for `a /* b */`).\nBoth fields are `false` for dangling comments: `[/* a */]`.\nThis is what `recast` uses too, and is somewhat similar to Babel, which is not\nestree but instead uses `leadingComments`, `trailingComments`, and\n`innerComments` arrays on nodes.\n\nThe algorithm checks any node: even recent (or future) proposals or nonstandard\nsyntax such as JSX, because it ducktypes to find nodes instead of having a list\nof visitor keys.\n\nThe algorithm supports `loc` fields (line/column), `range` fields (offsets),\nand direct `start` / `end` fields.\n\n###### Parameters\n\n*   `tree` ([`Program`][program])\n    — tree to attach to\n*   `comments` (`Array\u003cEstreeComment\u003e`)\n    — list of comments\n\n###### Returns\n\nNothing (`undefined`).\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports no additional types.\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,\n`estree-util-attach-comments@^3`, compatible 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-attach-comments/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/estree-util-attach-comments/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-attach-comments.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/estree-util-attach-comments\n\n[downloads-badge]: https://img.shields.io/npm/dm/estree-util-attach-comments.svg\n\n[downloads]: https://www.npmjs.com/package/estree-util-attach-comments\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-attach-comments\n\n[size]: https://bundlejs.com/?q=estree-util-attach-comments\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[acorn]: https://github.com/acornjs/acorn\n\n[estree]: https://github.com/estree/estree\n\n[program]: https://github.com/estree/estree/blob/master/es5.md#programs\n\n[api-attach-comments]: #attachcommentstree-comments\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%2Festree-util-attach-comments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Festree-util-attach-comments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Festree-util-attach-comments/lists"}