1
1
/**
2
- * @typedef {import('unist').Node } Node
3
2
* @typedef {import('unist').Parent } Parent
4
3
* @typedef {import('hast').Element } Element
5
- *
4
+ */
5
+
6
+ /**
6
7
* @typedef {string } TagName
8
+ * Check for a certain tag name.
7
9
*
8
- * @typedef {null|undefined|TagName|TestFunctionAnything|Array<TagName|TestFunctionAnything> } Test
10
+ * @typedef {null | undefined | TagName | TestFunctionAnything | Array<TagName | TestFunctionAnything> } Test
11
+ * Check for an arbitrary element, unaware of TypeScript.
9
12
*
10
13
* @callback TestFunctionAnything
11
- * Check if an element passes a test.
14
+ * Check if an element passes a test, unaware of TypeScript .
12
15
* @param {Element } element
13
- * @param {number|null|undefined } [index]
14
- * @param {Parent|null|undefined } [parent]
15
- * @returns {boolean|void }
16
+ * An element.
17
+ * @param {number | null | undefined } [index]
18
+ * Optionally, its position in its parent.
19
+ * @param {Parent | null | undefined } [parent]
20
+ * Optionally, its parent.
21
+ * @returns {boolean | void }
22
+ * Whether this element passes the test.
16
23
*/
17
24
18
25
/**
19
26
* @template {Element} T
20
- * @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array<T['tagName']|TestFunctionPredicate<T>> } PredicateTest
27
+ * Element type.
28
+ * @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>> } PredicateTest
29
+ * Basic check for an element that can be inferred by TypeScript.
21
30
*/
22
31
23
32
/**
24
33
* Check if an element passes a certain node test.
25
34
*
26
- * @template {Element} X
35
+ * @template {Element} T
36
+ * Element type.
27
37
* @callback TestFunctionPredicate
38
+ * Complex test function for an element that can be inferred by TypeScript.
28
39
* @param {Element } element
29
- * @param {number|null|undefined } [index]
30
- * @param {Parent|null|undefined } [parent]
31
- * @returns {element is X }
40
+ * An element.
41
+ * @param {number | null | undefined } [index]
42
+ * Optionally, its position in its parent.
43
+ * @param {Parent | null | undefined } [parent]
44
+ * Optionally, its parent.
45
+ * @returns {element is T }
46
+ * Whether this element passes the test.
32
47
*/
33
48
34
49
/**
35
50
* Check if a node is an element and passes a certain node test
36
51
*
37
52
* @callback AssertAnything
53
+ * Check that an arbitrary value is an element, unaware of TypeScript.
38
54
* @param {unknown } [node]
39
- * @param {number|null|undefined } [index]
40
- * @param {Parent|null|undefined } [parent]
55
+ * Anything (typically a node).
56
+ * @param {number | null | undefined } [index]
57
+ * Optionally, its position in its parent.
58
+ * @param {Parent | null | undefined } [parent]
59
+ * Optionally, its parent.
41
60
* @returns {boolean }
61
+ * Whether this is an element and passes a test.
42
62
*/
43
63
44
64
/**
45
65
* Check if a node is an element and passes a certain node test
46
66
*
47
- * @template {Element} Y
67
+ * @template {Element} T
68
+ * Element type.
48
69
* @callback AssertPredicate
70
+ * Check that an arbitrary value is a specific element, aware of TypeScript.
49
71
* @param {unknown } [node]
50
- * @param {number|null|undefined } [index]
51
- * @param {Parent|null|undefined } [parent]
52
- * @returns {node is Y }
72
+ * Anything (typically a node).
73
+ * @param {number | null | undefined } [index]
74
+ * Optionally, its position in its parent.
75
+ * @param {Parent | null | undefined } [parent]
76
+ * Optionally, its parent.
77
+ * @returns {node is T }
78
+ * Whether this is an element and passes a test.
53
79
*/
54
80
55
- // Check if `node` is an `element` and whether it passes the given test.
81
+ /**
82
+ * Check if `node` is an `Element` and whether it passes the given test.
83
+ *
84
+ * @param node
85
+ * Anything (typically a node).
86
+ * @param test
87
+ * A check.
88
+ * @param index
89
+ * Optionally, its position in its parent.
90
+ * @param parent
91
+ * Optionally, its parent.
92
+ * @returns
93
+ * Whether `node` is an element and passes a test.
94
+ */
56
95
export const isElement =
57
96
/**
58
- * Check if a node is an element and passes a test.
59
- * When a `parent` node is known the `index` of node should also be given.
60
- *
61
97
* @type {(
62
98
* (() => false) &
63
99
* (<T extends Element = Element>(node: unknown, test?: PredicateTest<T>, index?: number, parent?: Parent, context?: unknown) => node is T) &
@@ -66,18 +102,12 @@ export const isElement =
66
102
*/
67
103
(
68
104
/**
69
- * Check if a node passes a test.
70
- * When a `parent` node is known the `index` of node should also be given.
71
- *
72
- * @param {unknown } [node] Node to check
73
- * @param {Test } [test] When nullish, checks if `node` is a `Node`.
74
- * When `string`, works like passing `function (node) {return node.type === test}`.
75
- * When `function` checks if function passed the node is true.
76
- * When `array`, checks any one of the subtests pass.
77
- * @param {number } [index] Position of `node` in `parent`
78
- * @param {Parent } [parent] Parent of `node`
79
- * @param {unknown } [context] Context object to invoke `test` with
80
- * @returns {boolean } Whether test passed and `node` is an `Element` (object with `type` set to `element` and `tagName` set to a non-empty string).
105
+ * @param {unknown } [node]
106
+ * @param {Test | undefined } [test]
107
+ * @param {number | null | undefined } [index]
108
+ * @param {Parent | null | undefined } [parent]
109
+ * @param {unknown } [context]
110
+ * @returns {boolean }
81
111
*/
82
112
// eslint-disable-next-line max-params
83
113
function ( node , test , index , parent , context ) {
@@ -117,22 +147,28 @@ export const isElement =
117
147
}
118
148
)
119
149
150
+ /**
151
+ * Generate an assertion from a check.
152
+ *
153
+ * @param test
154
+ * * When nullish, checks if `node` is a `Node`.
155
+ * * When `string`, works like passing `function (node) {return node.type === test}`.
156
+ * * When `function` checks if function passed the node is true.
157
+ * * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
158
+ * * When `array`, checks any one of the subtests pass.
159
+ * @returns
160
+ * A test.
161
+ */
120
162
export const convertElement =
121
163
/**
122
164
* @type {(
123
- * (<T extends Element>(test: T['tagName']| TestFunctionPredicate<T>) => AssertPredicate<T>) &
165
+ * (<T extends Element>(test: T['tagName'] | TestFunctionPredicate<T>) => AssertPredicate<T>) &
124
166
* ((test?: Test) => AssertAnything)
125
167
* )}
126
168
*/
127
169
(
128
170
/**
129
- * Generate an assertion from a check.
130
- * @param {Test } [test]
131
- * When nullish, checks if `node` is a `Node`.
132
- * When `string`, works like passing `function (node) {return node.type === test}`.
133
- * When `function` checks if function passed the node is true.
134
- * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
135
- * When `array`, checks any one of the subtests pass.
171
+ * @param {Test | null | undefined } [test]
136
172
* @returns {AssertAnything }
137
173
*/
138
174
function ( test ) {
@@ -157,7 +193,9 @@ export const convertElement =
157
193
)
158
194
159
195
/**
160
- * @param {Array<TagName|TestFunctionAnything> } tests
196
+ * Handle multiple tests.
197
+ *
198
+ * @param {Array<TagName | TestFunctionAnything> } tests
161
199
* @returns {AssertAnything }
162
200
*/
163
201
function anyFactory ( tests ) {
@@ -190,8 +228,7 @@ function anyFactory(tests) {
190
228
}
191
229
192
230
/**
193
- * Utility to convert a string into a function which checks a given node’s tag
194
- * name for said string.
231
+ * Turn a string into a test for an element with a certain tag name.
195
232
*
196
233
* @param {TagName } check
197
234
* @returns {AssertAnything }
@@ -209,6 +246,8 @@ function tagNameFactory(check) {
209
246
}
210
247
211
248
/**
249
+ * Turn a custom test into a test for an element that passes that test.
250
+ *
212
251
* @param {TestFunctionAnything } check
213
252
* @returns {AssertAnything }
214
253
*/
@@ -228,7 +267,8 @@ function castFactory(check) {
228
267
}
229
268
230
269
/**
231
- * Utility to return true if this is an element.
270
+ * Make sure something is an element.
271
+ *
232
272
* @param {unknown } node
233
273
* @returns {node is Element }
234
274
*/
0 commit comments