Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 19, 2021
1 parent 2f188b3 commit 5ba4173
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
68 changes: 46 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*
* @typedef {import('unist-util-is').Type} Type
* @typedef {import('unist-util-is').Props} Props
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
*/

import {convert} from 'unist-util-is'

export function findAfter(parent, index, test) {
var is = convert(test)
export var findAfter =
/**
* @type {(
* (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) &
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null)
* )}
*/
(
/**
* @param {Parent} parent Parent node
* @param {Node|number} index Child of `parent`, or it’s index
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type)
* @returns {Node|null}
*/
function (parent, index, test) {
var is = convert(test)

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}
if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}

if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)
if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)

if (index < 0) {
throw new Error('Expected child node or index')
}
}
if (index < 0) {
throw new Error('Expected child node or index')
}
}

while (++index < parent.children.length) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}
while (++index < parent.children.length) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}

return null
}
return null
}
)
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"unist-util-is": "^5.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark": "^13.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -57,6 +66,7 @@
"xo": {
"prettier": true,
"rules": {
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
Expand All @@ -65,5 +75,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('unist').Node} Node
*/

import test from 'tape'
import remark from 'remark'
import {findAfter} from './index.js'
Expand All @@ -9,6 +13,7 @@ var children = paragraph.children
test('unist-util-find-after', function (t) {
t.throws(
function () {
// @ts-ignore runtime.
findAfter()
},
/Expected parent node/,
Expand All @@ -17,6 +22,7 @@ test('unist-util-find-after', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
findAfter({type: 'foo'})
},
/Expected parent node/,
Expand All @@ -25,6 +31,7 @@ test('unist-util-find-after', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
findAfter({type: 'foo', children: []})
},
/Expected child node or index/,
Expand All @@ -33,6 +40,7 @@ test('unist-util-find-after', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
findAfter({type: 'foo', children: []}, -1)
},
/Expected positive finite number as index/,
Expand All @@ -41,6 +49,7 @@ test('unist-util-find-after', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
findAfter({type: 'foo', children: []}, {type: 'bar'})
},
/Expected child node or index/,
Expand All @@ -52,6 +61,7 @@ test('unist-util-find-after', function (t) {
findAfter(
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
0,
// @ts-ignore runtime.
false
)
},
Expand All @@ -64,6 +74,7 @@ test('unist-util-find-after', function (t) {
findAfter(
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
0,
// @ts-ignore runtime.
true
)
},
Expand Down Expand Up @@ -160,7 +171,11 @@ test('unist-util-find-after', function (t) {
'should return a child when given a `test` and existing (#4)'
)

function test(node, n) {
/**
* @param {Node} _
* @param {number} n
*/
function test(_, n) {
return n === 5
}

Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 5ba4173

Please sign in to comment.