Skip to content

Commit ecabf42

Browse files
committed
Add improved jsdoc
1 parent ccb5949 commit ecabf42

File tree

3 files changed

+71
-45
lines changed

3 files changed

+71
-45
lines changed

index.js

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,92 @@
11
/**
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist').Parent} Parent
4-
*
54
* @typedef {string} Type
65
* @typedef {Record<string, unknown>} Props
7-
*
86
* @typedef {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} Test
9-
*/
10-
11-
/**
12-
* Check if a node passes a test
137
*
148
* @callback TestFunctionAnything
9+
* Arbitrary function to define whether a node passes.
10+
* @param {unknown} this
11+
* The to `is` given `context`
1512
* @param {Node} node
13+
* Node to check.
1614
* @param {number|null|undefined} [index]
15+
* Index of `node` in `parent`.
1716
* @param {Parent|null|undefined} [parent]
17+
* Parent of `node`.
1818
* @returns {boolean|void}
19-
*/
20-
21-
/**
22-
* Check if a node passes a certain node test
19+
* Whether `node` matches.
2320
*
24-
* @template {Node} X
25-
* @callback TestFunctionPredicate
26-
* @param {Node} node
21+
* @callback AssertAnything
22+
* Check if a node is an element and passes a certain node test.
23+
* @param {unknown} [node]
24+
* Thing to check and check that it’s a node.
2725
* @param {number|null|undefined} [index]
26+
* Index of `node` in `parent`.
2827
* @param {Parent|null|undefined} [parent]
29-
* @returns {node is X}
28+
* Parent of `node`.
29+
* @returns {boolean}
30+
* Whether `node` matches.
3031
*/
3132

3233
/**
33-
* @callback AssertAnything
34-
* @param {unknown} [node]
34+
* @template {Node} Kind
35+
* @callback TestFunctionPredicate
36+
* Arbitrary function to define whether a node passes, using a TypeScript type
37+
* predicate.
38+
* @param {Node} node
39+
* Node to check.
3540
* @param {number|null|undefined} [index]
41+
* Index of `node` in `parent`.
3642
* @param {Parent|null|undefined} [parent]
37-
* @returns {boolean}
43+
* Parent of `node`.
44+
* @returns {node is Kind}
45+
* Whether `node` matches.
3846
*/
3947

4048
/**
41-
* Check if a node passes a certain node test
42-
*
43-
* @template {Node} Y
49+
* @template {Node} Kind
4450
* @callback AssertPredicate
51+
* Check if a node is an element and passes a certain node test, using a
52+
* TypeScript type predicate.
4553
* @param {unknown} [node]
54+
* Thing to check and check that it’s a node.
4655
* @param {number|null|undefined} [index]
56+
* Index of `node` in `parent`.
4757
* @param {Parent|null|undefined} [parent]
48-
* @returns {node is Y}
58+
* Parent of `node`.
59+
* @returns {node is Kind}
60+
* Whether `node` matches.
4961
*/
5062

5163
/**
52-
* Check if a node passes a test.
53-
* When a `parent` node is known the `index` of node should also be given.
64+
* Check if `node` passes a test.
65+
* When a `parent` node is given the `index` of node should also be given.
5466
*
5567
* @param node
5668
* Node to check, can be anything.
5769
* @param test
58-
* * When nullish, checks if `node` is a `Node`.
59-
* * When `string`, works like passing `(node) => node.type === test`.
60-
* * When `function` checks if function passed the node is true.
61-
* * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
62-
* * When `array`, checks any one of the subtests pass.
70+
* * when nullish, checks if `node` is a `Node`.
71+
* * when `string`, works like passing `(node) => node.type === test`.
72+
* * when `array`, checks any one of the subtests pass.
73+
* * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
74+
* * when `function` checks if function passed the node is true.
6375
* @param index
6476
* Position of `node` in `parent`, must be a number if `parent` is also given.
6577
* @param parent
6678
* Parent of `node`, must be given if `index` is also given.
6779
* @param context
68-
* Context object to invoke `test` with, optional
80+
* Context object to call `test` with
6981
* @returns
7082
* Whether test passed and `node` is a `Node` (object with `type` set to
7183
* non-empty `string`).
7284
*/
7385
export const is =
7486
/**
7587
* @type {(
76-
* (<ExplicitNode extends Node>(node: unknown, test: ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>|Array<ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>>, index: number, parent: Parent, context?: unknown) => node is ExplicitNode) &
77-
* (<ExplicitNode extends Node>(node: unknown, test: ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>|Array<ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>>, index?: null|undefined, parent?: null|undefined, context?: unknown) => node is ExplicitNode) &
88+
* (<Kind extends Node>(node: unknown, test: Kind['type']|Partial<Kind>|TestFunctionPredicate<Kind>|Array<Kind['type']|Partial<Kind>|TestFunctionPredicate<Kind>>, index: number, parent: Parent, context?: unknown) => node is Kind) &
89+
* (<Kind extends Node>(node: unknown, test: Kind['type']|Partial<Kind>|TestFunctionPredicate<Kind>|Array<Kind['type']|Partial<Kind>|TestFunctionPredicate<Kind>>, index?: null|undefined, parent?: null|undefined, context?: unknown) => node is Kind) &
7890
* ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &
7991
* ((node?: unknown, test?: Test, index?: null|undefined, parent?: null|undefined, context?: unknown) => boolean)
8092
* )}
@@ -124,22 +136,35 @@ export const is =
124136
}
125137
)
126138

139+
/**
140+
* Create a test function from `test` that can later be called with a `node`,
141+
* `index`, and `parent`.
142+
* Useful if you’re going to test many nodes, for example when creating a
143+
* utility where something else passes an is-compatible test.
144+
*
145+
* The created function is slightly faster that using `is` because it expects
146+
* valid input only.
147+
* Therefore, passing invalid input yields unexpected results.
148+
*
149+
* @param test
150+
* * when nullish, checks if `node` is a `Node`.
151+
* * when `string`, works like passing `(node) => node.type === test`.
152+
* * when `array`, checks any one of the subtests pass.
153+
* * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
154+
* * when `function` checks if function passed the node is true.
155+
* @returns
156+
* Check function that can be called as `check(node, index, parent)`.
157+
*/
127158
export const convert =
128159
/**
129160
* @type {(
130-
* (<ExplicitNode extends Node>(test: ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>) => AssertPredicate<ExplicitNode>) &
161+
* (<Kind extends Node>(test: Kind['type']|Partial<Kind>|TestFunctionPredicate<Kind>) => AssertPredicate<Kind>) &
131162
* ((test?: Test) => AssertAnything)
132163
* )}
133164
*/
134165
(
135166
/**
136-
* Generate an assertion from a check.
137167
* @param {Test} [test]
138-
* When nullish, checks if `node` is a `Node`.
139-
* When `string`, works like passing `function (node) {return node.type === test}`.
140-
* When `function` checks if function passed the node is true.
141-
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
142-
* When `array`, checks any one of the subtests pass.
143168
* @returns {AssertAnything}
144169
*/
145170
function (test) {
@@ -162,6 +187,7 @@ export const convert =
162187
throw new Error('Expected function, string, or object as test')
163188
}
164189
)
190+
165191
/**
166192
* @param {Array<Type|Props|TestFunctionAnything>} tests
167193
* @returns {AssertAnything}
@@ -194,8 +220,8 @@ function anyFactory(tests) {
194220
}
195221

196222
/**
197-
* Utility to assert each property in `test` is represented in `node`, and each
198-
* values are strictly equal.
223+
* Assert each field in `test` is present in `node` and each values are strictly
224+
* equal.
199225
*
200226
* @param {Props} check
201227
* @returns {AssertAnything}
@@ -221,7 +247,7 @@ function propsFactory(check) {
221247
}
222248

223249
/**
224-
* Utility to convert a string into a function which checks a given node’s type
250+
* Convert a string into a function which checks a given node’s type
225251
* for said string.
226252
*
227253
* @param {Type} check
@@ -239,8 +265,9 @@ function typeFactory(check) {
239265
}
240266

241267
/**
242-
* Utility to convert a string into a function which checks a given node’s type
268+
* Convert a string into a function which checks a given node’s type
243269
* for said string.
270+
*
244271
* @param {TestFunctionAnything} check
245272
* @returns {AssertAnything}
246273
*/
@@ -258,7 +285,6 @@ function castFactory(check) {
258285
}
259286
}
260287

261-
// Utility to return true.
262288
function ok() {
263289
return true
264290
}

test/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test('unist-util-is', (t) => {
108108
t.end()
109109
})
110110

111-
t.test('should invoke test', (t) => {
111+
t.test('should call test', (t) => {
112112
const context = {foo: 'bar'}
113113

114114
t.plan(4)

test/property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('unist-util-is properties', (t) => {
4141
() =>
4242
fc.assert(
4343
// @ts-expect-error: fine.
44-
fc.property(fc.record({type: fc.string({minLength: 1})}), node => {
44+
fc.property(fc.record({type: fc.string({minLength: 1})}), (node) => {
4545
is(node, node.type)
4646
})
4747
),

0 commit comments

Comments
 (0)