Skip to content

Commit 06def74

Browse files
committed
Add better docs
1 parent 9048259 commit 06def74

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
coverage/
2+
node_modules/
13
.DS_Store
24
*.d.ts
35
*.log
4-
coverage/
5-
node_modules/
66
yarn.lock

index.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,45 @@
4848
* @returns {node is Y}
4949
*/
5050

51+
/**
52+
* Check if a node passes a test.
53+
* When a `parent` node is known the `index` of node should also be given.
54+
*
55+
* @param node
56+
* Node to check, can be anything.
57+
* @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.
63+
* @param index
64+
* Position of `node` in `parent`, must be a number if `parent` is also given.
65+
* @param parent
66+
* Parent of `node`, must be given if `index` is also given.
67+
* @param context
68+
* Context object to invoke `test` with, optional
69+
* @returns
70+
* Whether test passed and `node` is a `Node` (object with `type` set to
71+
* non-empty `string`).
72+
*/
5173
export const is =
5274
/**
53-
* Check if a node passes a test.
54-
* When a `parent` node is known the `index` of node should also be given.
55-
*
5675
* @type {(
57-
* (<T extends Node>(node: unknown, test: T['type']|Partial<T>|TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|TestFunctionPredicate<T>>, index?: number|null|undefined, parent?: Parent|null|undefined, context?: unknown) => node is T) &
58-
* ((node?: unknown, test?: Test, index?: number|null|undefined, parent?: Parent|null|undefined, context?: unknown) => boolean)
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) &
78+
* ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &
79+
* ((node?: unknown, test?: Test, index?: null|undefined, parent?: null|undefined, context?: unknown) => boolean)
5980
* )}
6081
*/
6182
(
6283
/**
63-
* Check if a node passes a test.
64-
* When a `parent` node is known the `index` of node should also be given.
65-
*
66-
* @param {unknown} [node] Node to check
84+
* @param {unknown} [node]
6785
* @param {Test} [test]
68-
* When nullish, checks if `node` is a `Node`.
69-
* When `string`, works like passing `function (node) {return node.type === test}`.
70-
* When `function` checks if function passed the node is true.
71-
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
72-
* When `array`, checks any one of the subtests pass.
73-
* @param {number|null|undefined} [index] Position of `node` in `parent`
74-
* @param {Parent|null|undefined} [parent] Parent of `node`
75-
* @param {unknown} [context] Context object to invoke `test` with
76-
* @returns {boolean} Whether test passed and `node` is a `Node` (object with `type` set to non-empty `string`).
86+
* @param {number|null|undefined} [index]
87+
* @param {Parent|null|undefined} [parent]
88+
* @param {unknown} [context]
89+
* @returns {boolean}
7790
*/
7891
// eslint-disable-next-line max-params
7992
function is(node, test, index, parent, context) {
@@ -114,7 +127,7 @@ export const is =
114127
export const convert =
115128
/**
116129
* @type {(
117-
* (<T extends Node>(test: T['type']|Partial<T>|TestFunctionPredicate<T>) => AssertPredicate<T>) &
130+
* (<ExplicitNode extends Node>(test: ExplicitNode['type']|Partial<ExplicitNode>|TestFunctionPredicate<ExplicitNode>) => AssertPredicate<ExplicitNode>) &
118131
* ((test?: Test) => AssertAnything)
119132
* )}
120133
*/

test/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ test('unist-util-is', (t) => {
6464

6565
t.throws(
6666
() => {
67+
// @ts-expect-error: both `index` and `parent` are needed.
6768
is(node, null, 0)
6869
},
6970
/Expected both parent and index/,
@@ -72,6 +73,7 @@ test('unist-util-is', (t) => {
7273

7374
t.throws(
7475
() => {
76+
// @ts-expect-error: both `index` and `parent` are needed.
7577
is(node, null, null, parent)
7678
},
7779
/Expected both parent and index/,

0 commit comments

Comments
 (0)