Skip to content

Commit e4fe868

Browse files
committed
Change to yield undefined
1 parent 8aeea56 commit e4fe868

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

lib/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ const emptyComments = []
5151
* Tree to attach to.
5252
* @param {Array<Comment> | null | undefined} [comments]
5353
* List of comments (optional).
54-
* @returns {Tree}
55-
* Given tree.
54+
* @returns {undefined}
55+
* Nothing.
5656
*/
5757
// To do: next major: don’t return given `tree`.
5858
export function attachComments(tree, comments) {
5959
const list = comments ? [...comments].sort(compare) : emptyComments
6060
if (list.length > 0) walk(tree, {comments: list, index: 0})
61-
return tree
6261
}
6362

6463
/**

readme.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,29 @@ In browsers with [`esm.sh`][esmsh]:
6666

6767
## Use
6868

69-
Say our document `index.js` contains:
69+
Say our document `x.js` contains:
7070

7171
```js
72-
/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }
72+
/* 1 */ function /* 2 */ a /* 3 */(/* 4 */ b) /* 5 */ {
73+
/* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */
74+
}
7375
```
7476

7577
…and our module `example.js` looks as follows:
7678

7779
```js
7880
import fs from 'node:fs/promises'
79-
import * as acorn from 'acorn'
80-
import recast from 'recast'
81+
import {parse} from 'acorn'
8182
import {attachComments} from 'estree-util-attach-comments'
83+
import recast from 'recast'
8284

83-
const code = String(await fs.readFile('index.js'))
85+
const code = String(await fs.readFile('x.js'))
8486
const comments = []
85-
const tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
87+
const tree = parse(code, {
88+
sourceType: 'module',
89+
ecmaVersion: 2020,
90+
onComment: comments
91+
})
8692

8793
attachComments(tree, comments)
8894

@@ -148,7 +154,7 @@ and direct `start` / `end` fields.
148154

149155
###### Returns
150156

151-
The given `tree` ([`Program`][program]).
157+
Nothing (`undefined`).
152158

153159
## Types
154160

test.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,67 @@ test('attachComments', async function (t) {
1919
})
2020

2121
await t.test('should support an empty document', async function () {
22-
assert.equal(recast.print(attachComments(...parse(''))).code, '')
22+
const [tree, comments] = parse('')
23+
attachComments(tree, comments)
24+
assert.equal(recast.print(tree).code, '')
2325
})
2426

2527
await t.test('should support no comments', async function () {
26-
assert.equal(recast.print(attachComments(...parse('a + 1'))).code, 'a + 1;')
28+
const [tree, comments] = parse('a + 1')
29+
attachComments(tree, comments)
30+
assert.equal(recast.print(tree).code, 'a + 1;')
2731
})
2832

2933
await t.test('should support a single block comment', async function () {
30-
assert.equal(
31-
recast.print(attachComments(...parse('/* ! */'))).code,
32-
'/* ! */\n'
33-
)
34+
const [tree, comments] = parse('/* ! */')
35+
attachComments(tree, comments)
36+
assert.equal(recast.print(tree).code, '/* ! */\n')
3437
})
3538

3639
await t.test('should support a single line comment', async function () {
37-
assert.equal(recast.print(attachComments(...parse('// !'))).code, '// !\n')
40+
const [tree, comments] = parse('// !')
41+
attachComments(tree, comments)
42+
assert.equal(recast.print(tree).code, '// !\n')
3843
})
3944

4045
await t.test('should support some comments', async function () {
46+
const [tree, comments] = parse(
47+
'/* 1 */ function a (/* 2 */b) { return b + 1 }'
48+
)
49+
attachComments(tree, comments)
4150
assert.equal(
42-
recast.print(
43-
attachComments(
44-
...parse('/* 1 */ function a (/* 2 */b) { return b + 1 }')
45-
)
46-
).code,
51+
recast.print(tree).code,
4752
'/* 1 */\nfunction a(\n /* 2 */\n b\n) {\n return b + 1;\n}'
4853
)
4954
})
5055

5156
await t.test('should support a bunch of block comments', async function () {
57+
const [tree, comments] = parse(
58+
'/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }'
59+
)
60+
attachComments(tree, comments)
5261
assert.equal(
53-
recast.print(
54-
attachComments(
55-
...parse(
56-
'/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }'
57-
)
58-
)
59-
).code,
62+
recast.print(tree).code,
6063
'/* 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 */'
6164
)
6265
})
6366

6467
await t.test('should support some more comments', async function () {
68+
const [tree, comments] = parse('/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }')
69+
attachComments(tree, comments)
6570
// Recast parses `4` as “dangling”:
6671
// <https://github.com/benjamn/recast/blob/dd7c5ec/lib/comments.ts#L255-L256>
6772
// But apprently doesn’t serialize it?
68-
assert.equal(
69-
recast.print(
70-
attachComments(...parse('/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }'))
71-
).code,
72-
'/* 1 */\na = /* 2 */\n/* 3 */\n{};'
73-
)
73+
assert.equal(recast.print(tree).code, '/* 1 */\na = /* 2 */\n/* 3 */\n{};')
7474
})
7575

7676
await t.test('should support a bunch of line comments', async function () {
77+
const [tree, comments] = parse(
78+
'// 1\nfunction // 2\na // 3\n(// 4\nb) // 5\n { // 6\n return b + // 7\n 1 // 8\n }'
79+
)
80+
attachComments(tree, comments)
7781
assert.equal(
78-
recast.print(
79-
attachComments(
80-
...parse(
81-
'// 1\nfunction // 2\na // 3\n(// 4\nb) // 5\n { // 6\n return b + // 7\n 1 // 8\n }'
82-
)
83-
)
84-
).code,
82+
recast.print(tree).code,
8583
'// 1\nfunction // 2\na(\n // 3\n // 4\n b\n) // 5\n{\n // 6\n return b + // 7\n 1;\n}// 8'
8684
)
8785
})
@@ -100,8 +98,9 @@ test('attachComments', async function (t) {
10098
})
10199

102100
removePositions(tree)
101+
attachComments(tree, comments)
103102

104-
assert.equal(recast.print(attachComments(tree, comments)).code, 'a + 1;')
103+
assert.equal(recast.print(tree).code, 'a + 1;')
105104
}
106105
)
107106

@@ -116,7 +115,9 @@ test('attachComments', async function (t) {
116115
onComment: comments
117116
})
118117

119-
assert.equal(recast.print(attachComments(tree)).code, '1 + 1;')
118+
attachComments(tree)
119+
120+
assert.equal(recast.print(tree).code, '1 + 1;')
120121
})
121122

122123
await t.test(
@@ -134,7 +135,9 @@ test('attachComments', async function (t) {
134135

135136
removePositions(comments)
136137

137-
assert.equal(recast.print(attachComments(tree, comments)).code, 'a + 1;')
138+
attachComments(tree, comments)
139+
140+
assert.equal(recast.print(tree).code, 'a + 1;')
138141
}
139142
)
140143

@@ -152,10 +155,9 @@ test('attachComments', async function (t) {
152155

153156
removePositions(tree)
154157

155-
assert.equal(
156-
recast.print(attachComments(tree, comments)).code,
157-
'/* 1 */\na + /* 2 */\n/* 3 */\n1;'
158-
)
158+
attachComments(tree, comments)
159+
160+
assert.equal(recast.print(tree).code, '/* 1 */\na + /* 2 */\n/* 3 */\n1;')
159161
})
160162

161163
await t.test('should use `loc`s', async function () {
@@ -171,11 +173,9 @@ test('attachComments', async function (t) {
171173
})
172174

173175
removePositions(tree)
176+
attachComments(tree, comments)
174177

175-
assert.equal(
176-
recast.print(attachComments(tree, comments)).code,
177-
'/* 1 */\na + /* 2 */\n/* 3 */\n1;'
178-
)
178+
assert.equal(recast.print(tree).code, '/* 1 */\na + /* 2 */\n/* 3 */\n1;')
179179
})
180180
})
181181

0 commit comments

Comments
 (0)