@@ -19,46 +19,68 @@ import {inspect} from './inspect.js'
19
19
const own = { } . hasOwnProperty
20
20
21
21
/**
22
- * Assert that `node` is a valid unist node.
22
+ * Assert that `tree` is a valid unist node.
23
+ *
23
24
* If `node` is a parent, all children will be asserted too.
24
25
*
25
- * @param {unknown } [node]
26
- * @param {Parent } [parent]
27
- * @returns {asserts node is Node }
26
+ * @param {unknown } [tree]
27
+ * Thing to assert.
28
+ * @param {Parent | null | undefined } [parent]
29
+ * Optional, valid parent.
30
+ * @returns {asserts tree is Node }
31
+ * Whether `tree` (and its descendants) are valid nodes.
32
+ * @throws {AssertionError }
33
+ * When `tree` (or its descendants) is not a node.
28
34
*/
29
- export function assert ( node , parent ) {
30
- return wrap ( assertNode ) ( node , parent )
35
+ export function assert ( tree , parent ) {
36
+ return wrap ( assertNode ) ( tree , parent )
31
37
}
32
38
33
39
/**
34
- * Assert that `node` is a valid unist parent.
40
+ * Assert that `tree` is a valid unist parent.
41
+ *
35
42
* All children will be asserted too.
36
43
*
37
- * @param {unknown } [node]
38
- * @param {Parent } [parent]
39
- * @returns {asserts node is Parent }
44
+ * @param {unknown } [tree]
45
+ * Thing to assert.
46
+ * @param {Parent | null | undefined } [parent]
47
+ * Optional, valid parent.
48
+ * @returns {asserts tree is Parent }
49
+ * Whether `tree` is a parent and its descendants are valid nodes.
50
+ * @throws {AssertionError }
51
+ * When `tree` is not a parent or its descendants are not nodes.
40
52
*/
41
- export function parent ( node , parent ) {
42
- return wrap ( assertParent ) ( node , parent )
53
+ export function parent ( tree , parent ) {
54
+ return wrap ( assertParent ) ( tree , parent )
43
55
}
44
56
45
57
/**
46
58
* Assert that `node` is a valid unist literal.
47
59
*
48
60
* @param {unknown } [node]
49
- * @param {Parent } [parent]
61
+ * Thing to assert.
62
+ * @param {Parent | null | undefined } [parent]
63
+ * Optional, valid parent.
50
64
* @returns {asserts node is Literal }
65
+ * Whether `node` is a literal.
66
+ * @throws {AssertionError }
67
+ * When `node` is not a literal.
51
68
*/
52
69
export function literal ( node , parent ) {
53
70
return wrap ( assertLiteral ) ( node , parent )
54
71
}
55
72
56
73
/**
57
- * Assert that `node` is a valid unist node, but neither parent nor literal .
74
+ * Assert that `node` is a valid void node.
58
75
*
59
76
* @param {unknown } [node]
60
- * @param {Parent } [parent]
77
+ * Thing to assert.
78
+ * @param {Parent | null | undefined } [parent]
79
+ * Optional, valid parent.
61
80
* @returns {asserts node is _Void }
81
+ * Whether `node` is a node but neither parent nor literal.
82
+ * @throws {AssertionError }
83
+ * When `node` is not a node, a parent, or a literal.
62
84
*/
63
85
export function _void ( node , parent ) {
64
86
return wrap ( assertVoid ) ( node , parent )
@@ -74,17 +96,25 @@ const defined = new Set(['type', 'value', 'children', 'position'])
74
96
* Wrapper that adds the current node (and parent, if available) to error
75
97
* messages.
76
98
*
77
- * @param {(node?: any, parent?: Parent|null) => asserts node is Node } fn
78
- * @returns {(node?: any, parent?: Parent|null) => asserts node is Node }
99
+ * @template {Node} T
100
+ * Node type.
101
+ * @param {(node?: any, parent?: Parent | null | undefined) => asserts node is T } fn
102
+ * Custom assertion.
103
+ * @returns {(node?: any, parent?: Parent | null | undefined) => asserts node is T }
104
+ * Assertion.
79
105
*/
80
106
export function wrap ( fn ) {
81
107
return wrapped
82
108
83
109
/**
84
110
* @param {unknown } node
85
- * @param {Parent|null|undefined } [parent]
111
+ * Thing to check.
112
+ * @param {Parent | null | undefined } [parent]
113
+ * Optional, valid parent.
86
114
* @throws {AssertionError }
115
+ * Whether `node` is a node but neither parent nor literal.
87
116
* @returns {void }
117
+ * Nothing.
88
118
*/
89
119
function wrapped ( node , parent ) {
90
120
try {
@@ -103,10 +133,16 @@ export function wrap(fn) {
103
133
}
104
134
105
135
/**
106
- * Assert.
136
+ * Assert that `node` is a valid unist parent.
137
+ *
138
+ * All children will be asserted too.
107
139
*
108
140
* @param {unknown } node
141
+ * Thing to assert.
109
142
* @returns {asserts node is Node }
143
+ * Whether `node` (and its descendants) are valid nodes.
144
+ * @throws {AssertionError }
145
+ * When `node` (or its descendants) is not a node.
110
146
*/
111
147
function assertNode ( node ) {
112
148
let index = - 1
@@ -173,7 +209,9 @@ function assertNode(node) {
173
209
* same (deep) value.
174
210
*
175
211
* @param {string } key
212
+ * Name of field.
176
213
* @param {unknown } value
214
+ * Value of field.
177
215
*/
178
216
function vanilla ( key , value ) {
179
217
try {
@@ -185,10 +223,13 @@ function vanilla(key, value) {
185
223
186
224
/**
187
225
* Stringify a value to inspect it.
226
+ *
188
227
* Tries `JSON.stringify()`, and if that fails uses `String()` instead.
189
228
*
190
229
* @param {unknown } value
230
+ * Anything (should be JSON).
191
231
* @returns {string }
232
+ * User-visible preresentation.
192
233
*/
193
234
function view ( value ) {
194
235
try {
@@ -200,10 +241,16 @@ function view(value) {
200
241
}
201
242
202
243
/**
203
- * Assert `node` is a parent node.
244
+ * Assert that `node` is a valid unist parent.
245
+ *
246
+ * All children will be asserted too.
204
247
*
205
248
* @param {Node } node
249
+ * Thing to assert.
206
250
* @returns {asserts node is Parent }
251
+ * Whether `node` is a parent and its descendants are valid nodes.
252
+ * @throws {AssertionError }
253
+ * When `node` is not a parent or its descendants are not nodes.
207
254
*/
208
255
function assertParent ( node ) {
209
256
assertNode ( node )
@@ -217,10 +264,14 @@ function assertParent(node) {
217
264
}
218
265
219
266
/**
220
- * Assert `node` is a literal node .
267
+ * Assert that `node` is a valid unist literal .
221
268
*
222
- * @param {Node } node
269
+ * @param {unknown } [node]
270
+ * Thing to assert.
223
271
* @returns {asserts node is Literal }
272
+ * Whether `node` is a literal.
273
+ * @throws {AssertionError }
274
+ * When `node` is not a literal.
224
275
*/
225
276
function assertLiteral ( node ) {
226
277
assertNode ( node )
@@ -234,10 +285,14 @@ function assertLiteral(node) {
234
285
}
235
286
236
287
/**
237
- * Assert `node` is a unist node, but neither parent nor literal .
288
+ * Assert that `node` is a valid void node .
238
289
*
239
- * @param {Node } node
290
+ * @param {unknown } [node]
291
+ * Thing to assert.
240
292
* @returns {asserts node is _Void }
293
+ * Whether `node` is a node but neither parent nor literal.
294
+ * @throws {AssertionError }
295
+ * When `node` is not a node, a parent, or a literal.
241
296
*/
242
297
function assertVoid ( node ) {
243
298
assertNode ( node )
@@ -251,48 +306,61 @@ function assertVoid(node) {
251
306
}
252
307
253
308
/**
254
- * Assert `position` is a unist position.
309
+ * Assert that `position` is a unist position.
255
310
*
256
- * @param {Position } position
311
+ * @param {unknown } position
312
+ * Thing to assert.
257
313
* @returns {asserts position is Position }
314
+ * Whether `position` is a unist position.
315
+ * @throws {AssertionError }
316
+ * When `position` is not a position.
258
317
*/
259
318
function position ( position ) {
260
319
if ( position !== null && position !== undefined ) {
261
320
nodeAssert . ok (
262
- position === Object ( position ) ,
321
+ typeof position === 'object' && position === Object ( position ) ,
263
322
'`position` should be an object'
264
323
)
265
324
325
+ // @ts -expect-error: indexable.
266
326
point ( position . start , 'position.start' )
327
+ // @ts -expect-error: indexable.
267
328
point ( position . end , 'position.end' )
268
329
}
269
330
}
270
331
271
332
/**
272
333
* Assert `point` is a unist point.
273
334
*
274
- * @param {Point } point
335
+ * @param {unknown } point
336
+ * Thing to assert.
275
337
* @param {string } label
338
+ * Whether `point` is a unist point.
276
339
* @returns {asserts point is Point }
340
+ * When `point` is not a point.
277
341
*/
278
342
function point ( point , label ) {
279
343
if ( point !== null && point !== undefined ) {
280
344
nodeAssert . ok (
281
- point === Object ( point ) ,
345
+ typeof point === 'object' && point === Object ( point ) ,
282
346
'`' + label + '` should be an object'
283
347
)
284
348
285
- if ( point . line !== null && point . line !== undefined ) {
349
+ if ( 'line' in point && point . line !== null && point . line !== undefined ) {
286
350
nodeAssert . ok (
287
- ' line' in point ,
351
+ typeof point . line === 'number' ,
288
352
'`' + label + '` should have numeric `line`'
289
353
)
290
354
nodeAssert . ok ( point . line >= 1 , '`' + label + '.line` should be gte `1`' )
291
355
}
292
356
293
- if ( point . column !== null && point . column !== undefined ) {
357
+ if (
358
+ 'column' in point &&
359
+ point . column !== null &&
360
+ point . column !== undefined
361
+ ) {
294
362
nodeAssert . ok (
295
- ' column' in point ,
363
+ typeof point . column === 'number' ,
296
364
'`' + label + '` should have numeric `column`'
297
365
)
298
366
nodeAssert . ok (
0 commit comments