From 27b28da700aac96835f2a3943fd8b041f239c1af Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 21 Jul 2021 15:36:31 +0200 Subject: [PATCH] Fix types to allow `null` * And add `strict` to `tsconfig.json` --- index.js | 32 ++++++++++++++++++-------------- test/main.js | 18 +++++++++--------- test/property.js | 5 ++++- tsconfig.json | 3 ++- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index c6fa71f..64f63e8 100644 --- a/index.js +++ b/index.js @@ -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} */ @@ -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} */ @@ -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} */ @@ -54,8 +54,8 @@ export const is = * When a `parent` node is known the `index` of node should also be given. * * @type {( - * ((node: unknown, test: T['type']|Partial|TestFunctionPredicate|Array.|TestFunctionPredicate>, index?: number, parent?: Parent, context?: unknown) => node is T) & - * ((node?: unknown, test?: Test, index?: number, parent?: Parent, context?: unknown) => boolean) + * ((node: unknown, test: T['type']|Partial|TestFunctionPredicate|Array.|TestFunctionPredicate>, 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) * )} */ ( @@ -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`). */ @@ -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 @@ -175,6 +175,8 @@ function anyFactory(tests) { while (++index < checks.length) { if (checks[index].call(this, ...parameters)) return true } + + return false } } @@ -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 @@ -237,6 +240,7 @@ function castFactory(check) { * @returns {boolean} */ function assertion(...parameters) { + // @ts-expect-error: spreading is fine. return Boolean(check.call(this, ...parameters)) } } diff --git a/test/main.js b/test/main.js index c7a9f9c..8fdaec9 100644 --- a/test/main.js +++ b/test/main.js @@ -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/, @@ -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/, @@ -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/, @@ -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/, @@ -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) { @@ -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) @@ -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) { diff --git a/test/property.js b/test/property.js index 85153bf..f306c38 100644 --- a/test/property.js +++ b/test/property.js @@ -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) @@ -45,12 +45,14 @@ 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 @@ -58,6 +60,7 @@ test('unist-util-is properties', (t) => { fc.tuple( fc.constant(node), fc.subarray( + // @ts-expect-error: hush Object.keys(node).filter((key) => !isObject(node[key])), {minLength: 1} ) diff --git a/tsconfig.json b/tsconfig.json index 1cb61bf..ab9f0b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "declaration": true, "emitDeclarationOnly": true, "allowSyntheticDefaultImports": true, - "skipLibCheck": true + "skipLibCheck": true, + "strict": true } }