8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
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.
13
13
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.
14
31
This is useful because certain estree parsers give you an array (espree and
15
32
acorn) whereas other estree tools expect comments to be embedded on nodes in the
16
33
tree.
17
34
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 ` .
21
38
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.
23
43
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
26
45
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] [ ] :
28
48
29
49
``` sh
30
50
npm install estree-util-attach-comments
31
51
```
32
52
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
+
33
67
## Use
34
68
35
- Say we have this weird ` code ` :
69
+ Say our document ` index.js ` contains :
36
70
37
71
``` js
38
72
/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */ b ) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }
39
73
```
40
74
41
- And our script, ` example.js ` , looks as follows:
75
+ …and our module ` example.js ` looks as follows:
42
76
43
77
``` js
78
+ import fs from ' node:fs/promises'
44
79
import * as acorn from ' acorn'
45
80
import recast from ' recast'
46
81
import {attachComments } from ' estree-util-attach-comments'
47
82
83
+ const code = String (await fs .readFile (' index.js' ))
48
84
const comments = []
49
85
const tree = acorn .parse (code, {ecmaVersion: 2020 , onComment: comments})
50
86
73
109
}/* 9 */
74
110
```
75
111
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.
78
114
79
115
## API
80
116
81
- This package exports the following identifiers: ` attachComments ` .
117
+ This package exports the identifier ` attachComments ` .
82
118
There is no default export.
83
119
84
120
### ` attachComments(tree, comments) `
@@ -105,7 +141,29 @@ and direct `start` / `end` fields.
105
141
106
142
###### Returns
107
143
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.
109
167
110
168
## License
111
169
@@ -141,10 +199,24 @@ and direct `start` / `end` fields.
141
199
142
200
[ npm ] : https://docs.npmjs.com/cli/install
143
201
202
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
203
+
204
+ [ esmsh ] : https://esm.sh
205
+
206
+ [ typescript ] : https://www.typescriptlang.org
207
+
144
208
[ license ] : license
145
209
146
210
[ author ] : https://wooorm.com
147
211
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
+
148
220
[ acorn ] : https://github.com/acornjs/acorn
149
221
150
222
[ estree ] : https://github.com/estree/estree
0 commit comments