Skip to content

Commit b0889ea

Browse files
committed
Refactor code-style
* Add support for `null` in input of API * Add more docs to JSDoc * Add a type check
1 parent b614205 commit b0889ea

File tree

3 files changed

+110
-40
lines changed

3 files changed

+110
-40
lines changed

index.js

+101-33
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,68 @@ import {inspect} from './inspect.js'
1919
const own = {}.hasOwnProperty
2020

2121
/**
22-
* Assert that `node` is a valid unist node.
22+
* Assert that `tree` is a valid unist node.
23+
*
2324
* If `node` is a parent, all children will be asserted too.
2425
*
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.
2834
*/
29-
export function assert(node, parent) {
30-
return wrap(assertNode)(node, parent)
35+
export function assert(tree, parent) {
36+
return wrap(assertNode)(tree, parent)
3137
}
3238

3339
/**
34-
* Assert that `node` is a valid unist parent.
40+
* Assert that `tree` is a valid unist parent.
41+
*
3542
* All children will be asserted too.
3643
*
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.
4052
*/
41-
export function parent(node, parent) {
42-
return wrap(assertParent)(node, parent)
53+
export function parent(tree, parent) {
54+
return wrap(assertParent)(tree, parent)
4355
}
4456

4557
/**
4658
* Assert that `node` is a valid unist literal.
4759
*
4860
* @param {unknown} [node]
49-
* @param {Parent} [parent]
61+
* Thing to assert.
62+
* @param {Parent | null | undefined} [parent]
63+
* Optional, valid parent.
5064
* @returns {asserts node is Literal}
65+
* Whether `node` is a literal.
66+
* @throws {AssertionError}
67+
* When `node` is not a literal.
5168
*/
5269
export function literal(node, parent) {
5370
return wrap(assertLiteral)(node, parent)
5471
}
5572

5673
/**
57-
* Assert that `node` is a valid unist node, but neither parent nor literal.
74+
* Assert that `node` is a valid void node.
5875
*
5976
* @param {unknown} [node]
60-
* @param {Parent} [parent]
77+
* Thing to assert.
78+
* @param {Parent | null | undefined} [parent]
79+
* Optional, valid parent.
6180
* @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.
6284
*/
6385
export function _void(node, parent) {
6486
return wrap(assertVoid)(node, parent)
@@ -74,17 +96,25 @@ const defined = new Set(['type', 'value', 'children', 'position'])
7496
* Wrapper that adds the current node (and parent, if available) to error
7597
* messages.
7698
*
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.
79105
*/
80106
export function wrap(fn) {
81107
return wrapped
82108

83109
/**
84110
* @param {unknown} node
85-
* @param {Parent|null|undefined} [parent]
111+
* Thing to check.
112+
* @param {Parent | null | undefined} [parent]
113+
* Optional, valid parent.
86114
* @throws {AssertionError}
115+
* Whether `node` is a node but neither parent nor literal.
87116
* @returns {void}
117+
* Nothing.
88118
*/
89119
function wrapped(node, parent) {
90120
try {
@@ -103,10 +133,16 @@ export function wrap(fn) {
103133
}
104134

105135
/**
106-
* Assert.
136+
* Assert that `node` is a valid unist parent.
137+
*
138+
* All children will be asserted too.
107139
*
108140
* @param {unknown} node
141+
* Thing to assert.
109142
* @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.
110146
*/
111147
function assertNode(node) {
112148
let index = -1
@@ -173,7 +209,9 @@ function assertNode(node) {
173209
* same (deep) value.
174210
*
175211
* @param {string} key
212+
* Name of field.
176213
* @param {unknown} value
214+
* Value of field.
177215
*/
178216
function vanilla(key, value) {
179217
try {
@@ -185,10 +223,13 @@ function vanilla(key, value) {
185223

186224
/**
187225
* Stringify a value to inspect it.
226+
*
188227
* Tries `JSON.stringify()`, and if that fails uses `String()` instead.
189228
*
190229
* @param {unknown} value
230+
* Anything (should be JSON).
191231
* @returns {string}
232+
* User-visible preresentation.
192233
*/
193234
function view(value) {
194235
try {
@@ -200,10 +241,16 @@ function view(value) {
200241
}
201242

202243
/**
203-
* Assert `node` is a parent node.
244+
* Assert that `node` is a valid unist parent.
245+
*
246+
* All children will be asserted too.
204247
*
205248
* @param {Node} node
249+
* Thing to assert.
206250
* @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.
207254
*/
208255
function assertParent(node) {
209256
assertNode(node)
@@ -217,10 +264,14 @@ function assertParent(node) {
217264
}
218265

219266
/**
220-
* Assert `node` is a literal node.
267+
* Assert that `node` is a valid unist literal.
221268
*
222-
* @param {Node} node
269+
* @param {unknown} [node]
270+
* Thing to assert.
223271
* @returns {asserts node is Literal}
272+
* Whether `node` is a literal.
273+
* @throws {AssertionError}
274+
* When `node` is not a literal.
224275
*/
225276
function assertLiteral(node) {
226277
assertNode(node)
@@ -234,10 +285,14 @@ function assertLiteral(node) {
234285
}
235286

236287
/**
237-
* Assert `node` is a unist node, but neither parent nor literal.
288+
* Assert that `node` is a valid void node.
238289
*
239-
* @param {Node} node
290+
* @param {unknown} [node]
291+
* Thing to assert.
240292
* @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.
241296
*/
242297
function assertVoid(node) {
243298
assertNode(node)
@@ -251,48 +306,61 @@ function assertVoid(node) {
251306
}
252307

253308
/**
254-
* Assert `position` is a unist position.
309+
* Assert that `position` is a unist position.
255310
*
256-
* @param {Position} position
311+
* @param {unknown} position
312+
* Thing to assert.
257313
* @returns {asserts position is Position}
314+
* Whether `position` is a unist position.
315+
* @throws {AssertionError}
316+
* When `position` is not a position.
258317
*/
259318
function position(position) {
260319
if (position !== null && position !== undefined) {
261320
nodeAssert.ok(
262-
position === Object(position),
321+
typeof position === 'object' && position === Object(position),
263322
'`position` should be an object'
264323
)
265324

325+
// @ts-expect-error: indexable.
266326
point(position.start, 'position.start')
327+
// @ts-expect-error: indexable.
267328
point(position.end, 'position.end')
268329
}
269330
}
270331

271332
/**
272333
* Assert `point` is a unist point.
273334
*
274-
* @param {Point} point
335+
* @param {unknown} point
336+
* Thing to assert.
275337
* @param {string} label
338+
* Whether `point` is a unist point.
276339
* @returns {asserts point is Point}
340+
* When `point` is not a point.
277341
*/
278342
function point(point, label) {
279343
if (point !== null && point !== undefined) {
280344
nodeAssert.ok(
281-
point === Object(point),
345+
typeof point === 'object' && point === Object(point),
282346
'`' + label + '` should be an object'
283347
)
284348

285-
if (point.line !== null && point.line !== undefined) {
349+
if ('line' in point && point.line !== null && point.line !== undefined) {
286350
nodeAssert.ok(
287-
'line' in point,
351+
typeof point.line === 'number',
288352
'`' + label + '` should have numeric `line`'
289353
)
290354
nodeAssert.ok(point.line >= 1, '`' + label + '.line` should be gte `1`')
291355
}
292356

293-
if (point.column !== null && point.column !== undefined) {
357+
if (
358+
'column' in point &&
359+
point.column !== null &&
360+
point.column !== undefined
361+
) {
294362
nodeAssert.ok(
295-
'column' in point,
363+
typeof point.column === 'number',
296364
'`' + label + '` should have numeric `column`'
297365
)
298366
nodeAssert.ok(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"prepack": "npm run build && npm run format",
6060
"build": "tsc --build --clean && tsc --build && tsd && type-coverage",
6161
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
62-
"test-api": "node --conditions development test.js",
62+
"test-api": "node --conditions development test/index.js",
6363
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
6464
"test": "npm run build && npm run format && npm run test-coverage"
6565
},

readme.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`assert(node[, parent])`](#assertnode-parent)
21-
* [`parent(node[, parent])`](#parentnode-parent)
20+
* [`assert(tree[, parent])`](#asserttree-parent)
21+
* [`parent(tree[, parent])`](#parenttree-parent)
2222
* [`literal(node[, parent])`](#literalnode-parent)
2323
* [`_void(node[, parent])`](#_voidnode-parent)
2424
* [`wrap(fn)`](#wrapfn)
@@ -97,18 +97,20 @@ This package exports the identifiers `assert`, `parent`, `literal`, `_void`,
9797
and `wrap`.
9898
There is no default export.
9999

100-
### `assert(node[, parent])`
100+
### `assert(tree[, parent])`
101+
102+
Assert that `tree` is a valid unist [`Node`][node].
101103

102-
Assert that `node` is a valid unist [`Node`][node].
103104
If `tree` is a [parent][], all children will be asserted as well.
104105

105106
###### Throws
106107

107108
When `node`, or one of its children, is not a valid node.
108109

109-
### `parent(node[, parent])`
110+
### `parent(tree[, parent])`
111+
112+
Assert that `tree` is a valid unist [`Parent`][parent].
110113

111-
Assert that `node` is a valid unist [`Parent`][parent].
112114
All children will be asserted as well.
113115

114116
### `literal(node[, parent])`

0 commit comments

Comments
 (0)