Skip to content

Commit 9b68f81

Browse files
committed
Add improved docs
1 parent 201bb32 commit 9b68f81

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

readme.md

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,79 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
Attach semistandard [estree][] comment nodes (such as from [acorn][] with a
12-
couple lines of code) to the nodes in that tree.
11+
[estree][] utility attach semistandard comment nodes (such as from [acorn][]) to
12+
the nodes in that tree.
1313

14+
## Contents
15+
16+
* [What is this?](#what-is-this)
17+
* [When should I use this?](#when-should-i-use-this)
18+
* [Install](#install)
19+
* [Use](#use)
20+
* [API](#api)
21+
* [`attachComments(tree, comments)`](#attachcommentstree-comments)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Contribute](#contribute)
25+
* [License](#license)
26+
27+
## What is this?
28+
29+
This package is a utility that you can use to embed comment nodes *inside* a
30+
tree.
1431
This is useful because certain estree parsers give you an array (espree and
1532
acorn) whereas other estree tools expect comments to be embedded on nodes in the
1633
tree.
1734

18-
This package uses one `comments` array where each comment has `leading`
19-
and `trailing` fields.
20-
Note that espree uses slightly different non-standard comments.
35+
This package uses one `comments` array where each comment has `leading` and
36+
`trailing` fields, as applied by `acorn`, but does not support the slightly
37+
different non-standard comments made by `espree`.
2138

22-
## Install
39+
## When should I use this?
40+
41+
You can use this package when working with comments from Acorn and later working
42+
with a tool such as recast or Babel.
2343

24-
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
25-
instead of `require`d.
44+
## Install
2645

27-
[npm][]:
46+
This package is [ESM only][esm].
47+
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
2848

2949
```sh
3050
npm install estree-util-attach-comments
3151
```
3252

53+
In Deno with [`esm.sh`][esmsh]:
54+
55+
```js
56+
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@2'
57+
```
58+
59+
In browsers with [`esm.sh`][esmsh]:
60+
61+
```html
62+
<script type="module">
63+
import {attachComments} from 'https://esm.sh/estree-util-attach-comments@2?bundle'
64+
</script>
65+
```
66+
3367
## Use
3468

35-
Say we have this weird `code`:
69+
Say our document `index.js` contains:
3670

3771
```js
3872
/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }
3973
```
4074

41-
And our script, `example.js`, looks as follows:
75+
…and our module `example.js` looks as follows:
4276

4377
```js
78+
import fs from 'node:fs/promises'
4479
import * as acorn from 'acorn'
4580
import recast from 'recast'
4681
import {attachComments} from 'estree-util-attach-comments'
4782

83+
const code = String(await fs.readFile('index.js'))
4884
const comments = []
4985
const tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
5086

@@ -73,12 +109,12 @@ a(
73109
}/* 9 */
74110
```
75111

76-
Note that the lines are added by `recast` in this case.
77-
And, some of these weird comments are off, but they’re pretty close.
112+
> 👉 **Note**: the lines are added by `recast` in this case.
113+
> And, some of these weird comments are off, but they’re pretty close.
78114
79115
## API
80116

81-
This package exports the following identifiers: `attachComments`.
117+
This package exports the identifier `attachComments`.
82118
There is no default export.
83119

84120
### `attachComments(tree, comments)`
@@ -105,7 +141,29 @@ and direct `start` / `end` fields.
105141

106142
###### Returns
107143

108-
`Node` — The given `tree`.
144+
The given `tree` (`Node`).
145+
146+
## Types
147+
148+
This package is fully typed with [TypeScript][].
149+
It exports no additional types.
150+
151+
## Compatibility
152+
153+
Projects maintained by the unified collective are compatible with all maintained
154+
versions of Node.js.
155+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
156+
Our projects sometimes work with older versions, but this is not guaranteed.
157+
158+
## Contribute
159+
160+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
161+
ways to get started.
162+
See [`support.md`][support] for ways to get help.
163+
164+
This project has a [code of conduct][coc].
165+
By interacting with this repository, organization, or community you agree to
166+
abide by its terms.
109167

110168
## License
111169

@@ -141,10 +199,24 @@ and direct `start` / `end` fields.
141199

142200
[npm]: https://docs.npmjs.com/cli/install
143201

202+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
203+
204+
[esmsh]: https://esm.sh
205+
206+
[typescript]: https://www.typescriptlang.org
207+
144208
[license]: license
145209

146210
[author]: https://wooorm.com
147211

212+
[health]: https://github.com/syntax-tree/.github
213+
214+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
215+
216+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
217+
218+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
219+
148220
[acorn]: https://github.com/acornjs/acorn
149221

150222
[estree]: https://github.com/estree/estree

0 commit comments

Comments
 (0)