-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
214 lines (185 loc) · 6.38 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @typedef {import('estree').Comment} Comment
* @typedef {import('estree').Node} Nodes
* @typedef {import('estree').Program} Program
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {parse as acornParse} from 'acorn'
import {attachComments} from 'estree-util-attach-comments'
import {visit} from 'estree-util-visit'
import recast from 'recast'
test('attachComments', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('estree-util-attach-comments')).sort(),
['attachComments']
)
})
await t.test('should support an empty document', async function () {
const [tree, comments] = parse('')
attachComments(tree, comments)
assert.equal(recast.print(tree).code, '')
})
await t.test('should support no comments', async function () {
const [tree, comments] = parse('a + 1')
attachComments(tree, comments)
assert.equal(recast.print(tree).code, 'a + 1;')
})
await t.test('should support a single block comment', async function () {
const [tree, comments] = parse('/* ! */')
attachComments(tree, comments)
assert.equal(recast.print(tree).code, '/* ! */\n')
})
await t.test('should support a single line comment', async function () {
const [tree, comments] = parse('// !')
attachComments(tree, comments)
assert.equal(recast.print(tree).code, '// !\n')
})
await t.test('should support some comments', async function () {
const [tree, comments] = parse(
'/* 1 */ function a (/* 2 */b) { return b + 1 }'
)
attachComments(tree, comments)
assert.equal(
recast.print(tree).code,
'/* 1 */\nfunction a(\n /* 2 */\n b\n) {\n return b + 1;\n}'
)
})
await t.test('should support a bunch of block comments', async function () {
const [tree, comments] = parse(
'/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }'
)
attachComments(tree, comments)
assert.equal(
recast.print(tree).code,
'/* 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 */'
)
})
await t.test('should support some more comments', async function () {
const [tree, comments] = parse('/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }')
attachComments(tree, comments)
// Recast parses `4` as “dangling”:
// <https://github.com/benjamn/recast/blob/dd7c5ec/lib/comments.ts#L255-L256>
// But apprently doesn’t serialize it?
assert.equal(recast.print(tree).code, '/* 1 */\na = /* 2 */\n/* 3 */\n{};')
})
await t.test('should support a bunch of line comments', async function () {
const [tree, comments] = parse(
'// 1\nfunction // 2\na // 3\n(// 4\nb) // 5\n { // 6\n return b + // 7\n 1 // 8\n }'
)
attachComments(tree, comments)
assert.equal(
recast.print(tree).code,
'// 1\nfunction // 2\na(\n // 3\n // 4\n b\n) // 5\n{\n // 6\n return b + // 7\n 1;\n}// 8'
)
})
await t.test(
'should not fail on a tree w/o positional info',
async function () {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
ecmaVersion: 2020,
// @ts-expect-error: acorn looks like estree.
onComment: comments
})
removePositions(tree)
attachComments(tree, comments)
assert.equal(recast.print(tree).code, 'a + 1;')
}
)
await t.test('should not fail w/o comments', async function () {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse('1 + 1', {
ecmaVersion: 2020,
// @ts-expect-error: acorn looks like estree.
onComment: comments
})
attachComments(tree)
assert.equal(recast.print(tree).code, '1 + 1;')
})
await t.test(
'should not fail on comments w/o positional info',
async function () {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
ecmaVersion: 2020,
// @ts-expect-error: acorn looks like estree.
onComment: comments
})
removePositions(comments)
attachComments(tree, comments)
assert.equal(recast.print(tree).code, 'a + 1;')
}
)
await t.test('should use `range`s', async function () {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
ecmaVersion: 2020,
ranges: true,
// @ts-expect-error: acorn looks like estree.
onComment: comments
})
removePositions(tree)
attachComments(tree, comments)
assert.equal(recast.print(tree).code, '/* 1 */\na + /* 2 */\n/* 3 */\n1;')
})
await t.test('should use `loc`s', async function () {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
ecmaVersion: 2020,
locations: true,
// @ts-expect-error: acorn looks like estree.
onComment: comments
})
removePositions(tree)
attachComments(tree, comments)
assert.equal(recast.print(tree).code, '/* 1 */\na + /* 2 */\n/* 3 */\n1;')
})
})
/**
* @param {string} doc
* @returns {[Program, Array<Comment>]}
*/
function parse(doc) {
/** @type {Array<Comment>} */
const comments = []
/** @type {Program} */
// @ts-expect-error: acorn looks like estree.
const tree = acornParse(doc, {ecmaVersion: 2020, onComment: comments})
return [tree, comments]
}
/**
* @param {Array<Comment | Nodes> | Comment | Nodes} value
* @returns {undefined}
*/
function removePositions(value) {
visit(
// @ts-expect-error: comments and arrays are fine.
value,
/**
* @param {Nodes} node
*/
function (node) {
// @ts-expect-error: acorn-specific extension.
delete node.start
// @ts-expect-error: acorn-specific extension.
delete node.end
}
)
}