Skip to content

Commit 61a3eed

Browse files
committed
Add improved docs
1 parent fbb5ebb commit 61a3eed

File tree

2 files changed

+119
-30
lines changed

2 files changed

+119
-30
lines changed

lib/index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
22
* @typedef {import('assert').AssertionError} AssertionError
3+
* Assertion error from `node:assert`.
4+
*
35
* @typedef {import('unist').Node} Node
46
* @typedef {import('unist').Parent} Parent
57
* @typedef {import('unist').Literal} Literal
@@ -21,14 +23,14 @@ const own = {}.hasOwnProperty
2123
/**
2224
* Assert that `tree` is a valid unist node.
2325
*
24-
* If `node` is a parent, all children will be asserted too.
26+
* If `tree` is a parent, all children will be asserted too.
2527
*
2628
* @param {unknown} [tree]
2729
* Thing to assert.
2830
* @param {Parent | null | undefined} [parent]
2931
* Optional, valid parent.
3032
* @returns {asserts tree is Node}
31-
* Whether `tree` (and its descendants) are valid nodes.
33+
* Nothing.
3234
* @throws {AssertionError}
3335
* When `tree` (or its descendants) is not a node.
3436
*/
@@ -46,7 +48,7 @@ export function assert(tree, parent) {
4648
* @param {Parent | null | undefined} [parent]
4749
* Optional, valid parent.
4850
* @returns {asserts tree is Parent}
49-
* Whether `tree` is a parent and its descendants are valid nodes.
51+
* Nothing.
5052
* @throws {AssertionError}
5153
* When `tree` is not a parent or its descendants are not nodes.
5254
*/
@@ -62,7 +64,7 @@ export function parent(tree, parent) {
6264
* @param {Parent | null | undefined} [parent]
6365
* Optional, valid parent.
6466
* @returns {asserts node is Literal}
65-
* Whether `node` is a literal.
67+
* Nothing.
6668
* @throws {AssertionError}
6769
* When `node` is not a literal.
6870
*/
@@ -78,7 +80,7 @@ export function literal(node, parent) {
7880
* @param {Parent | null | undefined} [parent]
7981
* Optional, valid parent.
8082
* @returns {asserts node is _Void}
81-
* Whether `node` is a node but neither parent nor literal.
83+
* Nothing.
8284
* @throws {AssertionError}
8385
* When `node` is not a node, a parent, or a literal.
8486
*/
@@ -140,7 +142,7 @@ export function wrap(fn) {
140142
* @param {unknown} node
141143
* Thing to assert.
142144
* @returns {asserts node is Node}
143-
* Whether `node` (and its descendants) are valid nodes.
145+
* Nothing.
144146
* @throws {AssertionError}
145147
* When `node` (or its descendants) is not a node.
146148
*/
@@ -248,7 +250,7 @@ function view(value) {
248250
* @param {Node} node
249251
* Thing to assert.
250252
* @returns {asserts node is Parent}
251-
* Whether `node` is a parent and its descendants are valid nodes.
253+
* Nothing.
252254
* @throws {AssertionError}
253255
* When `node` is not a parent or its descendants are not nodes.
254256
*/
@@ -269,7 +271,7 @@ function assertParent(node) {
269271
* @param {unknown} [node]
270272
* Thing to assert.
271273
* @returns {asserts node is Literal}
272-
* Whether `node` is a literal.
274+
* Nothing.
273275
* @throws {AssertionError}
274276
* When `node` is not a literal.
275277
*/
@@ -290,7 +292,7 @@ function assertLiteral(node) {
290292
* @param {unknown} [node]
291293
* Thing to assert.
292294
* @returns {asserts node is _Void}
293-
* Whether `node` is a node but neither parent nor literal.
295+
* Nothing.
294296
* @throws {AssertionError}
295297
* When `node` is not a node, a parent, or a literal.
296298
*/
@@ -311,7 +313,7 @@ function assertVoid(node) {
311313
* @param {unknown} position
312314
* Thing to assert.
313315
* @returns {asserts position is Position}
314-
* Whether `position` is a unist position.
316+
* Nothing.
315317
* @throws {AssertionError}
316318
* When `position` is not a position.
317319
*/
@@ -335,9 +337,11 @@ function position(position) {
335337
* @param {unknown} point
336338
* Thing to assert.
337339
* @param {string} label
338-
* Whether `point` is a unist point.
340+
* `start` or `end`
339341
* @returns {asserts point is Point}
340-
* When `point` is not a point.
342+
* Nothing.
343+
* @throws {AssertionError}
344+
* When `point` is not a position.
341345
*/
342346
function point(point, label) {
343347
if (point !== null && point !== undefined) {

readme.md

+103-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [`literal(node[, parent])`](#literalnode-parent)
2323
* [`_void(node[, parent])`](#_voidnode-parent)
2424
* [`wrap(fn)`](#wrapfn)
25+
* [`AssertionError`](#assertionerror)
2526
* [Extensions](#extensions)
2627
* [Types](#types)
2728
* [Compatibility](#compatibility)
@@ -45,7 +46,7 @@ do the same but for mdast, hast, and nlcst nodes, respectively.
4546
## Install
4647

4748
This package is [ESM only][esm].
48-
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
49+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4950

5051
```sh
5152
npm install unist-util-assert
@@ -93,41 +94,113 @@ assert({type: 'paragraph', children: ['foo']})
9394

9495
## API
9596

96-
This package exports the identifiers `assert`, `parent`, `literal`, `_void`,
97-
and `wrap`.
97+
This package exports the identifiers [`_void`][void], [`assert`][assert],
98+
[`literal`][literal], [`parent`][parent], and [`wrap`][wrap].
9899
There is no default export.
99100

100101
### `assert(tree[, parent])`
101102

102103
Assert that `tree` is a valid unist [`Node`][node].
103104

104-
If `tree` is a [parent][], all children will be asserted as well.
105+
If `tree` is a parent, all children will be asserted too.
106+
107+
###### Parameters
108+
109+
* `tree` (`unknown`)
110+
— thing to assert
111+
* `parent` ([`Parent`][parent-node], optional)
112+
— optional, valid parent
113+
114+
###### Returns
115+
116+
Nothing.
105117

106118
###### Throws
107119

108-
When `node`, or one of its children, is not a valid node.
120+
When `tree` (or its descendants) is not a node.
109121

110122
### `parent(tree[, parent])`
111123

112-
Assert that `tree` is a valid unist [`Parent`][parent].
124+
Assert that `tree` is a valid unist [`Parent`][parent-node].
125+
126+
All children will be asserted too.
127+
128+
###### Parameters
129+
130+
* `tree` (`unknown`)
131+
— thing to assert
132+
* `parent` ([`Parent`][parent-node], optional)
133+
— optional, valid parent
134+
135+
###### Returns
136+
137+
Nothing.
138+
139+
###### Throws
113140

114-
All children will be asserted as well.
141+
When `tree` is not a parent or its descendants are not nodes.
115142

116143
### `literal(node[, parent])`
117144

118-
Assert that `node` is a valid unist [`Literal`][literal].
145+
Assert that `node` is a valid unist [`Literal`][literal-node].
146+
147+
###### Parameters
148+
149+
* `tree` (`unknown`)
150+
— thing to assert
151+
* `parent` ([`Parent`][parent-node], optional)
152+
— optional, valid parent
153+
154+
###### Returns
155+
156+
Nothing.
157+
158+
###### Throws
159+
160+
When `node` is not a literal.
119161

120162
### `_void(node[, parent])`
121163

122-
Assert that `node` is a valid unist [`Node`][node], but neither
123-
[`Parent`][parent] nor
124-
[`Literal`][literal].
164+
Assert that `node` is a valid void node.
165+
166+
###### Parameters
167+
168+
* `tree` (`unknown`)
169+
— thing to assert
170+
* `parent` ([`Parent`][parent-node], optional)
171+
— optional, valid parent
172+
173+
###### Returns
174+
175+
Nothing.
176+
177+
###### Throws
178+
179+
When `node` is not a node, a parent, or a literal.
125180

126181
### `wrap(fn)`
127182

128-
Wraps `fn` (which is passed a node, and an optional parent node), so that any
129-
errors thrown inside it will contain information regarding the node (and the
130-
parent, when given).
183+
Wrapper that adds the current node (and parent, if available) to error
184+
messages.
185+
186+
###### Parameters
187+
188+
* `fn` (`(node?: any, parent?: Parent | null | undefined) => asserts node is Node)`)
189+
— custom assertion
190+
191+
###### Returns
192+
193+
Wrapped `fn`.
194+
195+
### `AssertionError`
196+
197+
Assertion error from `node:assert` (TypeScript type).
198+
199+
###### Type
200+
201+
```ts
202+
type AssertionError = import('node:assert').AssertionError
203+
```
131204
132205
## Extensions
133206
@@ -141,13 +214,13 @@ message.
141214
## Types
142215
143216
This package is fully typed with [TypeScript][].
144-
It exports the additional type `AssertionError`.
217+
It exports the additional type [`AssertionError`][assertionerror].
145218
146219
## Compatibility
147220
148221
Projects maintained by the unified collective are compatible with all maintained
149222
versions of Node.js.
150-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
223+
As of now, that is Node.js 14.14+ and 16.0+.
151224
Our projects sometimes work with older versions, but this is not guaranteed.
152225
153226
## Related
@@ -225,12 +298,24 @@ abide by its terms.
225298
226299
[node]: https://github.com/syntax-tree/unist#node
227300
228-
[parent]: https://github.com/syntax-tree/unist#parent
301+
[parent-node]: https://github.com/syntax-tree/unist#parent
229302
230-
[literal]: https://github.com/syntax-tree/unist#literal
303+
[literal-node]: https://github.com/syntax-tree/unist#literal
231304
232305
[mdast-util-assert]: https://github.com/syntax-tree/mdast-util-assert
233306
234307
[hast-util-assert]: https://github.com/syntax-tree/hast-util-assert
235308
236309
[nlcst-test]: https://github.com/syntax-tree/nlcst-test
310+
311+
[assert]: #asserttree-parent
312+
313+
[parent]: #parenttree-parent
314+
315+
[literal]: #literalnode-parent
316+
317+
[void]: #_voidnode-parent
318+
319+
[wrap]: #wrapfn
320+
321+
[assertionerror]: #assertionerror

0 commit comments

Comments
 (0)