Skip to content

Commit 27b28da

Browse files
committed
Fix types to allow null
* And add `strict` to `tsconfig.json`
1 parent 9dc1e50 commit 27b28da

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

index.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*
1414
* @callback TestFunctionAnything
1515
* @param {Node} node
16-
* @param {number} [index]
17-
* @param {Parent} [parent]
16+
* @param {number|null|undefined} [index]
17+
* @param {Parent|null|undefined} [parent]
1818
* @returns {boolean|void}
1919
*/
2020

@@ -24,16 +24,16 @@
2424
* @template {Node} X
2525
* @callback TestFunctionPredicate
2626
* @param {Node} node
27-
* @param {number} [index]
28-
* @param {Parent} [parent]
27+
* @param {number|null|undefined} [index]
28+
* @param {Parent|null|undefined} [parent]
2929
* @returns {node is X}
3030
*/
3131

3232
/**
3333
* @callback AssertAnything
3434
* @param {unknown} [node]
35-
* @param {number} [index]
36-
* @param {Parent} [parent]
35+
* @param {number|null|undefined} [index]
36+
* @param {Parent|null|undefined} [parent]
3737
* @returns {boolean}
3838
*/
3939

@@ -43,8 +43,8 @@
4343
* @template {Node} Y
4444
* @callback AssertPredicate
4545
* @param {unknown} [node]
46-
* @param {number} [index]
47-
* @param {Parent} [parent]
46+
* @param {number|null|undefined} [index]
47+
* @param {Parent|null|undefined} [parent]
4848
* @returns {node is Y}
4949
*/
5050

@@ -54,8 +54,8 @@ export const is =
5454
* When a `parent` node is known the `index` of node should also be given.
5555
*
5656
* @type {(
57-
* (<T extends Node>(node: unknown, test: T['type']|Partial<T>|TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|TestFunctionPredicate<T>>, index?: number, parent?: Parent, context?: unknown) => node is T) &
58-
* ((node?: unknown, test?: Test, index?: number, parent?: Parent, context?: unknown) => boolean)
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)
5959
* )}
6060
*/
6161
(
@@ -70,8 +70,8 @@ export const is =
7070
* When `function` checks if function passed the node is true.
7171
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
7272
* When `array`, checks any one of the subtests pass.
73-
* @param {number} [index] Position of `node` in `parent`
74-
* @param {Parent} [parent] Parent of `node`
73+
* @param {number|null|undefined} [index] Position of `node` in `parent`
74+
* @param {Parent|null|undefined} [parent] Parent of `node`
7575
* @param {unknown} [context] Context object to invoke `test` with
7676
* @returns {boolean} Whether test passed and `node` is a `Node` (object with `type` set to non-empty `string`).
7777
*/
@@ -104,7 +104,7 @@ export const is =
104104
throw new Error('Expected both parent and index')
105105
}
106106

107-
// @ts-ignore Looks like a node.
107+
// @ts-expect-error Looks like a node.
108108
return node && node.type && typeof node.type === 'string'
109109
? Boolean(check.call(context, node, index, parent))
110110
: false
@@ -175,6 +175,8 @@ function anyFactory(tests) {
175175
while (++index < checks.length) {
176176
if (checks[index].call(this, ...parameters)) return true
177177
}
178+
179+
return false
178180
}
179181
}
180182

@@ -197,7 +199,8 @@ function propsFactory(check) {
197199
let key
198200

199201
for (key in check) {
200-
if (node[key] !== check[key]) return
202+
// @ts-expect-error: hush, it sure works as an index.
203+
if (node[key] !== check[key]) return false
201204
}
202205

203206
return true
@@ -237,6 +240,7 @@ function castFactory(check) {
237240
* @returns {boolean}
238241
*/
239242
function assertion(...parameters) {
243+
// @ts-expect-error: spreading is fine.
240244
return Boolean(check.call(this, ...parameters))
241245
}
242246
}

test/main.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('unist-util-is', (t) => {
1212

1313
t.throws(
1414
() => {
15-
// @ts-ignore runtime.
15+
// @ts-expect-error runtime.
1616
is(null, false)
1717
},
1818
/Expected function, string, or object as test/,
@@ -37,7 +37,7 @@ test('unist-util-is', (t) => {
3737

3838
t.throws(
3939
() => {
40-
// @ts-ignore runtime.
40+
// @ts-expect-error runtime.
4141
is(node, null, false, parent)
4242
},
4343
/Expected positive finite index/,
@@ -46,7 +46,7 @@ test('unist-util-is', (t) => {
4646

4747
t.throws(
4848
() => {
49-
// @ts-ignore runtime.
49+
// @ts-expect-error runtime.
5050
is(node, null, 0, {})
5151
},
5252
/Expected parent node/,
@@ -55,7 +55,7 @@ test('unist-util-is', (t) => {
5555

5656
t.throws(
5757
() => {
58-
// @ts-ignore runtime.
58+
// @ts-expect-error runtime.
5959
is(node, null, 0, {type: 'paragraph'})
6060
},
6161
/Expected parent node/,
@@ -92,7 +92,7 @@ test('unist-util-is', (t) => {
9292
t.test('should accept a test', (t) => {
9393
/**
9494
* @param {unknown} _
95-
* @param {number} n
95+
* @param {number|null|undefined} n
9696
* @returns {boolean}
9797
*/
9898
function test(_, n) {
@@ -114,8 +114,8 @@ test('unist-util-is', (t) => {
114114
/**
115115
* @this {context}
116116
* @param {Node} a
117-
* @param {number} b
118-
* @param {Parent} c
117+
* @param {number|null|undefined} b
118+
* @param {Parent|null|undefined} c
119119
*/
120120
function test(a, b, c) {
121121
t.equal(this, context)
@@ -140,8 +140,8 @@ test('unist-util-is', (t) => {
140140
/**
141141
* @this {context}
142142
* @param {Node} a
143-
* @param {number} b
144-
* @param {Parent} c
143+
* @param {number|null|undefined} b
144+
* @param {Parent|null|undefined} c
145145
* @returns {boolean}
146146
*/
147147
function test(a, b, c) {

test/property.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('unist-util-is properties', (t) => {
2222
fc.assert(
2323
fc.property(
2424
fc.unicodeJsonObject().filter(
25-
// @ts-ignore Looks like a node.
25+
// @ts-expect-error Looks like a node.
2626
(node) => !(isPlainObject(node) && typeof node.type === 'string')
2727
),
2828
(node) => !is(node)
@@ -45,19 +45,22 @@ test('unist-util-is properties', (t) => {
4545
() =>
4646
fc.assert(
4747
fc.property(
48+
// @ts-expect-error: hush
4849
fc
4950
.unicodeJsonObject()
5051
// Filter for JSON objects which unist can work with
5152
.filter(
5253
(node) =>
5354
isPlainObject(node) &&
55+
// @ts-expect-error: hush
5456
Object.keys(node).some((key) => !isObject(node[key]))
5557
)
5658
// Return node and a list with a random subset of its primitive value keys
5759
.chain((node) =>
5860
fc.tuple(
5961
fc.constant(node),
6062
fc.subarray(
63+
// @ts-expect-error: hush
6164
Object.keys(node).filter((key) => !isObject(node[key])),
6265
{minLength: 1}
6366
)

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"declaration": true,
1111
"emitDeclarationOnly": true,
1212
"allowSyntheticDefaultImports": true,
13-
"skipLibCheck": true
13+
"skipLibCheck": true,
14+
"strict": true
1415
}
1516
}

0 commit comments

Comments
 (0)