Skip to content

Commit 6ee499e

Browse files
committed
Add support for null as input in types
1 parent 2a9afde commit 6ee499e

File tree

7 files changed

+72
-62
lines changed

7 files changed

+72
-62
lines changed

index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ import {parse} from './lib/parse.js'
1717
*
1818
* @param {string} selector
1919
* CSS selector, such as (`h1`, `a, b`).
20-
* @param {Node|undefined} [node]
20+
* @param {Node | null | undefined} [node]
2121
* Node that might match `selector`, should be an element.
22-
* @param {Space|undefined} [space='html']
22+
* @param {Space | null | undefined} [space='html']
2323
* Name of namespace (`'svg'` or `'html'`).
2424
* @returns {boolean}
2525
* Whether `node` matches `selector`.
2626
*/
2727
export function matches(selector, node, space) {
2828
return Boolean(
29-
any(parse(selector), node, {space, one: true, shallow: true})[0]
29+
any(parse(selector), node || undefined, {
30+
space: space || undefined,
31+
one: true,
32+
shallow: true
33+
})[0]
3034
)
3135
}
3236

@@ -36,17 +40,23 @@ export function matches(selector, node, space) {
3640
*
3741
* @param {string} selector
3842
* CSS selector, such as (`h1`, `a, b`).
39-
* @param {Node|undefined} [tree]
43+
* @param {Node | null | undefined} [tree]
4044
* Tree to search.
41-
* @param {Space|undefined} [space='html']
45+
* @param {Space | null | undefined} [space='html']
4246
* Name of namespace (`'svg'` or `'html'`).
4347
* @returns {Element|null}
4448
* First element in `tree` that matches `selector` or `null` if nothing is
4549
* found.
4650
* This could be `tree` itself.
4751
*/
4852
export function select(selector, tree, space) {
49-
return any(parse(selector), tree, {space, one: true})[0] || null
53+
// To do in major: return `undefined` instead.
54+
return (
55+
any(parse(selector), tree || undefined, {
56+
space: space || undefined,
57+
one: true
58+
})[0] || null
59+
)
5060
}
5161

5262
/**
@@ -55,14 +65,14 @@ export function select(selector, tree, space) {
5565
*
5666
* @param {string} selector
5767
* CSS selector, such as (`h1`, `a, b`).
58-
* @param {Node|undefined} [tree]
68+
* @param {Node | null | undefined} [tree]
5969
* Tree to search.
60-
* @param {Space|undefined} [space='html']
70+
* @param {Space | null | undefined} [space='html']
6171
* Name of namespace (`'svg'` or `'html'`).
6272
* @returns {Array<Element>}
6373
* Elements in `tree` that match `selector`.
6474
* This could include `tree` itself.
6575
*/
6676
export function selectAll(selector, tree, space) {
67-
return any(parse(selector), tree, {space})
77+
return any(parse(selector), tree || undefined, {space: space || undefined})
6878
}

lib/any.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function rule(query, tree, state) {
7676
query,
7777
tree,
7878
0,
79-
null,
79+
undefined,
8080
configure(query, {
8181
// @ts-expect-error assume elements.
8282
scopeElements: tree.type === 'root' ? tree.children : [tree],

lib/enter-state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function enterState(state, node) {
112112
function inferDirectionality(child) {
113113
if (child.type === 'text') {
114114
dirInferred = dirBidi(child.value)
115-
return dirInferred ? EXIT : null
115+
return dirInferred ? EXIT : undefined
116116
}
117117

118118
if (

lib/nest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {parent, element} from './util.js'
1414

1515
const own = {}.hasOwnProperty
1616

17-
/** @type {(query: Rule, node: Node, index: number|null, parent: Parent|null, state: SelectState) => void} */
17+
/** @type {(query: Rule, node: Node, index: number | undefined, parent: Parent | undefined, state: SelectState) => void} */
1818
const handle = zwitch('nestingOperator', {
1919
unknown: unknownNesting,
2020
// @ts-expect-error: hush.
@@ -47,7 +47,7 @@ function unknownNesting(query) {
4747
function topScan(query, node, index, parent, state) {
4848
// Shouldn’t happen.
4949
/* c8 ignore next 3 */
50-
if (parent || index === null) {
50+
if (parent || index === undefined) {
5151
throw new Error('topScan is supposed to be called from the root node')
5252
}
5353

@@ -97,15 +97,15 @@ function child(query, node, _1, _2, state) {
9797
function adjacentSibling(query, _, index, parent, state) {
9898
// Shouldn’t happen.
9999
/* c8 ignore next */
100-
if (!parent || index === null) return
100+
if (!parent || index === undefined) return
101101
indexedSearch(query, parent, state, index + 1, true)
102102
}
103103

104104
/** @type {Handler} */
105105
function generalSibling(query, _, index, parent, state) {
106106
// Shouldn’t happen.
107107
/* c8 ignore next */
108-
if (!parent || index === null) return
108+
if (!parent || index === undefined) return
109109
indexedSearch(query, parent, state, index + 1)
110110
}
111111

@@ -129,7 +129,7 @@ function indexedSearch(query, parent, state, from, firstElementOnly) {
129129
const delayed = []
130130

131131
// Start looking at `from`
132-
if (from === undefined || from === null) from = 0
132+
if (from === undefined || from === undefined) from = 0
133133

134134
// Exit if there are no further nodes.
135135
if (from >= children.length) return

lib/pseudo.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {whitespace} from 'hast-util-whitespace'
1919
import {zwitch} from 'zwitch'
2020
import {any} from './any.js'
2121

22-
/** @type {(rule: Rule|RulePseudo, element: Element, index: number|null, parent: Parent|null, state: SelectState) => boolean} */
22+
/** @type {(rule: Rule|RulePseudo, element: Element, index: number | undefined, parent: Parent | undefined, state: SelectState) => boolean} */
2323
const handle = zwitch('name', {
2424
unknown: unknownPseudo,
2525
invalid: invalidPseudo,
@@ -74,8 +74,8 @@ pseudo.needsIndex = [
7474
/**
7575
* @param {Rule} query
7676
* @param {Element} element
77-
* @param {number|null} index
78-
* @param {Parent|null} parent
77+
* @param {number|undefined} index
78+
* @param {Parent|undefined} parent
7979
* @param {SelectState} state
8080
* @returns {boolean}
8181
*/
@@ -93,8 +93,8 @@ export function pseudo(query, element, index, parent, state) {
9393
/**
9494
* @param {RulePseudoSelector} query
9595
* @param {Element} element
96-
* @param {number|null} _
97-
* @param {Parent|null} parent
96+
* @param {number|undefined} _
97+
* @param {Parent|undefined} parent
9898
* @param {SelectState} state
9999
* @returns {boolean}
100100
*/
@@ -116,8 +116,8 @@ function matches(query, element, _, parent, state) {
116116
/**
117117
* @param {RulePseudoSelector} query
118118
* @param {Element} element
119-
* @param {number|null} index
120-
* @param {Parent|null} parent
119+
* @param {number|undefined} index
120+
* @param {Parent|undefined} parent
121121
* @param {SelectState} state
122122
* @returns {boolean}
123123
*/
@@ -161,8 +161,8 @@ function checked(_, element) {
161161
/**
162162
* @param {RulePseudo} query
163163
* @param {Element} _1
164-
* @param {number|null} _2
165-
* @param {Parent|null} _3
164+
* @param {number|undefined} _2
165+
* @param {Parent|undefined} _3
166166
* @param {SelectState} state
167167
* @returns {boolean}
168168
*/
@@ -223,8 +223,8 @@ function optional(query, element) {
223223
/**
224224
* @param {RulePseudo} _
225225
* @param {Element} element
226-
* @param {number|null} _1
227-
* @param {Parent|null} _2
226+
* @param {number|undefined} _1
227+
* @param {Parent|undefined} _2
228228
* @param {SelectState} state
229229
* @returns {boolean}
230230
*/
@@ -237,8 +237,8 @@ function readWrite(_, element, _1, _2, state) {
237237
/**
238238
* @param {RulePseudo} query
239239
* @param {Element} element
240-
* @param {number|null} index
241-
* @param {Parent|null} parent
240+
* @param {number|undefined} index
241+
* @param {Parent|undefined} parent
242242
* @param {SelectState} state
243243
* @returns {boolean}
244244
*/
@@ -249,8 +249,8 @@ function readOnly(query, element, index, parent, state) {
249249
/**
250250
* @param {RulePseudo} _
251251
* @param {Element} element
252-
* @param {number|null} _1
253-
* @param {Parent|null} parent
252+
* @param {number|undefined} _1
253+
* @param {Parent|undefined} parent
254254
* @param {SelectState} state
255255
* @returns {boolean}
256256
*/
@@ -266,8 +266,8 @@ function root(_, element, _1, parent, state) {
266266
/**
267267
* @param {RulePseudo} _
268268
* @param {Element} element
269-
* @param {number|null} _1
270-
* @param {Parent|null} _2
269+
* @param {number|undefined} _1
270+
* @param {Parent|undefined} _2
271271
* @param {SelectState} state
272272
* @returns {boolean}
273273
*/
@@ -318,8 +318,8 @@ function blank(_, element) {
318318
/**
319319
* @param {RulePseudo} query
320320
* @param {Element} _1
321-
* @param {number|null} _2
322-
* @param {Parent|null} _3
321+
* @param {number|undefined} _2
322+
* @param {Parent|undefined} _3
323323
* @param {SelectState} state
324324
* @returns {boolean}
325325
*/
@@ -331,16 +331,16 @@ function firstChild(query, _1, _2, _3, state) {
331331
/**
332332
* @param {RulePseudo} query
333333
* @param {Element} _1
334-
* @param {number|null} _2
335-
* @param {Parent|null} _3
334+
* @param {number|undefined} _2
335+
* @param {Parent|undefined} _3
336336
* @param {SelectState} state
337337
* @returns {boolean}
338338
*/
339339
function lang(query, _1, _2, _3, state) {
340340
return (
341341
state.language !== '' &&
342342
state.language !== undefined &&
343-
state.language !== null &&
343+
state.language !== undefined &&
344344
// @ts-expect-error never `selectors`.
345345
extendedFilter(state.language, commas(query.value)).length > 0
346346
)
@@ -349,8 +349,8 @@ function lang(query, _1, _2, _3, state) {
349349
/**
350350
* @param {RulePseudo} query
351351
* @param {Element} _1
352-
* @param {number|null} _2
353-
* @param {Parent|null} _3
352+
* @param {number|undefined} _2
353+
* @param {Parent|undefined} _3
354354
* @param {SelectState} state
355355
* @returns {boolean}
356356
*/
@@ -364,8 +364,8 @@ function lastChild(query, _1, _2, _3, state) {
364364
/**
365365
* @param {RulePseudo} query
366366
* @param {Element} _1
367-
* @param {number|null} _2
368-
* @param {Parent|null} _3
367+
* @param {number|undefined} _2
368+
* @param {Parent|undefined} _3
369369
* @param {SelectState} state
370370
* @returns {boolean}
371371
*/
@@ -377,8 +377,8 @@ function onlyChild(query, _1, _2, _3, state) {
377377
/**
378378
* @param {RulePseudoNth} query
379379
* @param {Element} _1
380-
* @param {number|null} _2
381-
* @param {Parent|null} _3
380+
* @param {number|undefined} _2
381+
* @param {Parent|undefined} _3
382382
* @param {SelectState} state
383383
* @returns {boolean}
384384
*/
@@ -392,8 +392,8 @@ function nthChild(query, _1, _2, _3, state) {
392392
/**
393393
* @param {RulePseudoNth} query
394394
* @param {Element} _1
395-
* @param {number|null} _2
396-
* @param {Parent|null} _3
395+
* @param {number|undefined} _2
396+
* @param {Parent|undefined} _3
397397
* @param {SelectState} state
398398
* @returns {boolean}
399399
*/
@@ -409,8 +409,8 @@ function nthLastChild(query, _1, _2, _3, state) {
409409
/**
410410
* @param {RulePseudoNth} query
411411
* @param {Element} _1
412-
* @param {number|null} _2
413-
* @param {Parent|null} _3
412+
* @param {number|undefined} _2
413+
* @param {Parent|undefined} _3
414414
* @param {SelectState} state
415415
* @returns {boolean}
416416
*/
@@ -422,8 +422,8 @@ function nthOfType(query, _1, _2, _3, state) {
422422
/**
423423
* @param {RulePseudoNth} query
424424
* @param {Element} _1
425-
* @param {number|null} _2
426-
* @param {Parent|null} _3
425+
* @param {number|undefined} _2
426+
* @param {Parent|undefined} _3
427427
* @param {SelectState} state
428428
* @returns {boolean}
429429
*/
@@ -439,8 +439,8 @@ function nthLastOfType(query, _1, _2, _3, state) {
439439
/**
440440
* @param {RulePseudo} query
441441
* @param {Element} _1
442-
* @param {number|null} _2
443-
* @param {Parent|null} _3
442+
* @param {number|undefined} _2
443+
* @param {Parent|undefined} _3
444444
* @param {SelectState} state
445445
* @returns {boolean}
446446
*/
@@ -452,8 +452,8 @@ function firstOfType(query, _1, _2, _3, state) {
452452
/**
453453
* @param {RulePseudo} query
454454
* @param {Element} _1
455-
* @param {number|null} _2
456-
* @param {Parent|null} _3
455+
* @param {number|undefined} _2
456+
* @param {Parent|undefined} _3
457457
* @param {SelectState} state
458458
* @returns {boolean}
459459
*/
@@ -469,8 +469,8 @@ function lastOfType(query, _1, _2, _3, state) {
469469
/**
470470
* @param {RulePseudo} query
471471
* @param {Element} _1
472-
* @param {number|null} _2
473-
* @param {Parent|null} _3
472+
* @param {number|undefined} _2
473+
* @param {Parent|undefined} _3
474474
* @param {SelectState} state
475475
* @returns {boolean}
476476
*/
@@ -528,8 +528,8 @@ function assertDeep(state, query) {
528528
/**
529529
* @param {RulePseudoSelector} query
530530
* @param {Element} element
531-
* @param {number|null} _1
532-
* @param {Parent|null} _2
531+
* @param {number|undefined} _1
532+
* @param {Parent|undefined} _2
533533
* @param {SelectState} state
534534
* @returns {boolean}
535535
*/

lib/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {element} from './util.js'
1515
/**
1616
* @param {Rule} query
1717
* @param {Node} node
18-
* @param {number|null} index
19-
* @param {Parent|null} parent
18+
* @param {number|undefined} index
19+
* @param {Parent|undefined} parent
2020
* @param {SelectState} state
2121
* @returns {boolean}
2222
*/

lib/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@
6969
* @param {Rule} query
7070
* @param {Node} node
7171
* @param {number} index
72-
* @param {Parent|null} parent
72+
* @param {Parent|undefined} parent
7373
* @param {SelectState} state
7474
*/
7575

7676
/**
7777
* @typedef {(
78-
* ((query: Rule, node: Node, index: number|null, parent: Parent|null, state: SelectState) => void)
78+
* ((query: Rule, node: Node, index: number | undefined, parent: Parent | undefined, state: SelectState) => void)
7979
* )} Handler
8080
*/
8181

0 commit comments

Comments
 (0)