Skip to content

Commit 5bbc32a

Browse files
committed
Refactor code-style
1 parent 6e28fa1 commit 5bbc32a

File tree

4 files changed

+281
-236
lines changed

4 files changed

+281
-236
lines changed

index.js

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
/**
2-
* @typedef {import('unist').Parent} Parent
32
* @typedef {import('hast').Element} Element
3+
* @typedef {import('unist').Parent} Parent
44
*/
55

66
/**
7-
* @typedef {null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything>} Test
7+
* @typedef {Array<TestFunctionAnything | string> | TestFunctionAnything | string | null | undefined} Test
88
* Check for an arbitrary element, unaware of TypeScript inferral.
99
*
1010
* @callback TestFunctionAnything
1111
* Check if an element passes a test, unaware of TypeScript inferral.
1212
* @param {Element} element
1313
* An element.
1414
* @param {number | null | undefined} [index]
15-
* The element’s position in its parent.
15+
* Position of `node` in `parent`.
1616
* @param {Parent | null | undefined} [parent]
17-
* The element’s parent.
18-
* @returns {boolean | void}
17+
* Parent of `node`.
18+
* @returns {boolean | undefined | void}
1919
* Whether this element passes the test.
2020
*/
2121

2222
/**
2323
* @template {Element} T
2424
* Element type.
25-
* @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>>} PredicateTest
25+
* @typedef {Array<TestFunctionPredicate<T> | T['tagName']> | TestFunctionPredicate<T> | T['tagName']} PredicateTest
2626
* Check for an element that can be inferred by TypeScript.
2727
*/
2828

@@ -36,9 +36,9 @@
3636
* @param {Element} element
3737
* An element.
3838
* @param {number | null | undefined} [index]
39-
* The element’s position in its parent.
39+
* Position of `node` in `parent`.
4040
* @param {Parent | null | undefined} [parent]
41-
* The element’s parent.
41+
* Parent of `node`.
4242
* @returns {element is T}
4343
* Whether this element passes the test.
4444
*/
@@ -49,9 +49,9 @@
4949
* @param {unknown} [node]
5050
* Anything (typically a node).
5151
* @param {number | null | undefined} [index]
52-
* The node’s position in its parent.
52+
* Position of `node` in `parent`.
5353
* @param {Parent | null | undefined} [parent]
54-
* The node’s parent.
54+
* Parent of `node`.
5555
* @returns {boolean}
5656
* Whether this is an element and passes a test.
5757
*/
@@ -66,9 +66,9 @@
6666
* @param {unknown} [node]
6767
* Anything (typically a node).
6868
* @param {number | null | undefined} [index]
69-
* The node’s position in its parent.
69+
* Position of `node` in `parent`.
7070
* @param {Parent | null | undefined} [parent]
71-
* The node’s parent.
71+
* Parent of `node`.
7272
* @returns {node is T}
7373
* Whether this is an element and passes a test.
7474
*/
@@ -79,15 +79,18 @@
7979
* @param node
8080
* Thing to check, typically `Node`.
8181
* @param test
82-
* A check for a specific element.
82+
* Check for a specific element.
8383
* @param index
84-
* The node’s position in its parent.
84+
* Position of `node` in `parent`.
8585
* @param parent
86-
* The node’s parent.
86+
* Parent of `node`.
87+
* @param context
88+
* Context to call `test` with.
8789
* @returns
8890
* Whether `node` is an element and passes a test.
8991
*/
9092
export const isElement =
93+
// Note: JSDoc overloads don’t support optional type parameters.
9194
/**
9295
* @type {(
9396
* (() => false) &
@@ -109,8 +112,8 @@ export const isElement =
109112
const check = convertElement(test)
110113

111114
if (
112-
index !== undefined &&
113115
index !== null &&
116+
index !== undefined &&
114117
(typeof index !== 'number' ||
115118
index < 0 ||
116119
index === Number.POSITIVE_INFINITY)
@@ -119,21 +122,21 @@ export const isElement =
119122
}
120123

121124
if (
122-
parent !== undefined &&
123125
parent !== null &&
126+
parent !== undefined &&
124127
(!parent.type || !parent.children)
125128
) {
126129
throw new Error('Expected parent node')
127130
}
128131

129-
// @ts-expect-error Looks like a node.
132+
// @ts-expect-error: looks like a node.
130133
if (!node || !node.type || typeof node.type !== 'string') {
131134
return false
132135
}
133136

134137
if (
135-
(parent === undefined || parent === null) !==
136-
(index === undefined || index === null)
138+
(parent === null || parent === undefined) !==
139+
(index === null || index === undefined)
137140
) {
138141
throw new Error('Expected both parent and index')
139142
}
@@ -172,7 +175,7 @@ export const convertElement =
172175
* @returns {AssertAnything}
173176
*/
174177
function (test) {
175-
if (test === undefined || test === null) {
178+
if (test === null || test === undefined) {
176179
return element
177180
}
178181

@@ -195,7 +198,7 @@ export const convertElement =
195198
/**
196199
* Handle multiple tests.
197200
*
198-
* @param {Array<string | TestFunctionAnything>} tests
201+
* @param {Array<TestFunctionAnything | string>} tests
199202
* @returns {AssertAnything}
200203
*/
201204
function anyFactory(tests) {
@@ -261,7 +264,7 @@ function castFactory(check) {
261264
* @returns {boolean}
262265
*/
263266
function assertion(node, ...parameters) {
264-
// @ts-expect-error: fine.
267+
// @ts-expect-error: assume valid parameters.
265268
return element(node) && Boolean(check.call(this, node, ...parameters))
266269
}
267270
}
@@ -276,9 +279,9 @@ function element(node) {
276279
return Boolean(
277280
node &&
278281
typeof node === 'object' &&
279-
// @ts-expect-error Looks like a node.
282+
'type' in node &&
280283
node.type === 'element' &&
281-
// @ts-expect-error Looks like an element.
284+
'tagName' in node &&
282285
typeof node.tagName === 'string'
283286
)
284287
}

index.test-d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import type {Element} from 'hast'
2+
import {expectError, expectNotType, expectType} from 'tsd'
13
import {unified} from 'unified'
24
import type {Node} from 'unist'
3-
import {expectType, expectNotType, expectError} from 'tsd'
4-
import type {Element} from 'hast'
55
import {isElement, convertElement} from './index.js'
66

77
/* Setup. */

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ type).
181181
###### Type
182182

183183
```ts
184-
type Test = null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything>
184+
type Test = Array<TestFunctionAnything | string> | TestFunctionAnything | string | null | undefined
185185
```
186186
187187
Checks that the given thing is an element, and then:

0 commit comments

Comments
 (0)