Skip to content

Commit

Permalink
Fix types to allow null
Browse files Browse the repository at this point in the history
* And add `strict` to `tsconfig.json`
  • Loading branch information
wooorm committed Jul 21, 2021
1 parent 9dc1e50 commit 27b28da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
32 changes: 18 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*
* @callback TestFunctionAnything
* @param {Node} node
* @param {number} [index]
* @param {Parent} [parent]
* @param {number|null|undefined} [index]
* @param {Parent|null|undefined} [parent]
* @returns {boolean|void}
*/

Expand All @@ -24,16 +24,16 @@
* @template {Node} X
* @callback TestFunctionPredicate
* @param {Node} node
* @param {number} [index]
* @param {Parent} [parent]
* @param {number|null|undefined} [index]
* @param {Parent|null|undefined} [parent]
* @returns {node is X}
*/

/**
* @callback AssertAnything
* @param {unknown} [node]
* @param {number} [index]
* @param {Parent} [parent]
* @param {number|null|undefined} [index]
* @param {Parent|null|undefined} [parent]
* @returns {boolean}
*/

Expand All @@ -43,8 +43,8 @@
* @template {Node} Y
* @callback AssertPredicate
* @param {unknown} [node]
* @param {number} [index]
* @param {Parent} [parent]
* @param {number|null|undefined} [index]
* @param {Parent|null|undefined} [parent]
* @returns {node is Y}
*/

Expand All @@ -54,8 +54,8 @@ export const is =
* When a `parent` node is known the `index` of node should also be given.
*
* @type {(
* (<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) &
* ((node?: unknown, test?: Test, index?: number, parent?: Parent, context?: unknown) => boolean)
* (<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) &
* ((node?: unknown, test?: Test, index?: number|null|undefined, parent?: Parent|null|undefined, context?: unknown) => boolean)
* )}
*/
(
Expand All @@ -70,8 +70,8 @@ export const is =
* When `function` checks if function passed the node is true.
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
* When `array`, checks any one of the subtests pass.
* @param {number} [index] Position of `node` in `parent`
* @param {Parent} [parent] Parent of `node`
* @param {number|null|undefined} [index] Position of `node` in `parent`
* @param {Parent|null|undefined} [parent] Parent of `node`
* @param {unknown} [context] Context object to invoke `test` with
* @returns {boolean} Whether test passed and `node` is a `Node` (object with `type` set to non-empty `string`).
*/
Expand Down Expand Up @@ -104,7 +104,7 @@ export const is =
throw new Error('Expected both parent and index')
}

// @ts-ignore Looks like a node.
// @ts-expect-error Looks like a node.
return node && node.type && typeof node.type === 'string'
? Boolean(check.call(context, node, index, parent))
: false
Expand Down Expand Up @@ -175,6 +175,8 @@ function anyFactory(tests) {
while (++index < checks.length) {
if (checks[index].call(this, ...parameters)) return true
}

return false
}
}

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

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

return true
Expand Down Expand Up @@ -237,6 +240,7 @@ function castFactory(check) {
* @returns {boolean}
*/
function assertion(...parameters) {
// @ts-expect-error: spreading is fine.
return Boolean(check.call(this, ...parameters))
}
}
Expand Down
18 changes: 9 additions & 9 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('unist-util-is', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
is(null, false)
},
/Expected function, string, or object as test/,
Expand All @@ -37,7 +37,7 @@ test('unist-util-is', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
is(node, null, false, parent)
},
/Expected positive finite index/,
Expand All @@ -46,7 +46,7 @@ test('unist-util-is', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
is(node, null, 0, {})
},
/Expected parent node/,
Expand All @@ -55,7 +55,7 @@ test('unist-util-is', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
is(node, null, 0, {type: 'paragraph'})
},
/Expected parent node/,
Expand Down Expand Up @@ -92,7 +92,7 @@ test('unist-util-is', (t) => {
t.test('should accept a test', (t) => {
/**
* @param {unknown} _
* @param {number} n
* @param {number|null|undefined} n
* @returns {boolean}
*/
function test(_, n) {
Expand All @@ -114,8 +114,8 @@ test('unist-util-is', (t) => {
/**
* @this {context}
* @param {Node} a
* @param {number} b
* @param {Parent} c
* @param {number|null|undefined} b
* @param {Parent|null|undefined} c
*/
function test(a, b, c) {
t.equal(this, context)
Expand All @@ -140,8 +140,8 @@ test('unist-util-is', (t) => {
/**
* @this {context}
* @param {Node} a
* @param {number} b
* @param {Parent} c
* @param {number|null|undefined} b
* @param {Parent|null|undefined} c
* @returns {boolean}
*/
function test(a, b, c) {
Expand Down
5 changes: 4 additions & 1 deletion test/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('unist-util-is properties', (t) => {
fc.assert(
fc.property(
fc.unicodeJsonObject().filter(
// @ts-ignore Looks like a node.
// @ts-expect-error Looks like a node.
(node) => !(isPlainObject(node) && typeof node.type === 'string')
),
(node) => !is(node)
Expand All @@ -45,19 +45,22 @@ test('unist-util-is properties', (t) => {
() =>
fc.assert(
fc.property(
// @ts-expect-error: hush
fc
.unicodeJsonObject()
// Filter for JSON objects which unist can work with
.filter(
(node) =>
isPlainObject(node) &&
// @ts-expect-error: hush
Object.keys(node).some((key) => !isObject(node[key]))
)
// Return node and a list with a random subset of its primitive value keys
.chain((node) =>
fc.tuple(
fc.constant(node),
fc.subarray(
// @ts-expect-error: hush
Object.keys(node).filter((key) => !isObject(node[key])),
{minLength: 1}
)
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 27b28da

Please sign in to comment.