Skip to content

Commit 5ba4173

Browse files
committed
Add JSDoc based types
1 parent 2f188b3 commit 5ba4173

File tree

5 files changed

+94
-24
lines changed

5 files changed

+94
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

index.js

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
*
5+
* @typedef {import('unist-util-is').Type} Type
6+
* @typedef {import('unist-util-is').Props} Props
7+
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
8+
*/
9+
110
import {convert} from 'unist-util-is'
211

3-
export function findAfter(parent, index, test) {
4-
var is = convert(test)
12+
export var findAfter =
13+
/**
14+
* @type {(
15+
* (<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) &
16+
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null)
17+
* )}
18+
*/
19+
(
20+
/**
21+
* @param {Parent} parent Parent node
22+
* @param {Node|number} index Child of `parent`, or it’s index
23+
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type)
24+
* @returns {Node|null}
25+
*/
26+
function (parent, index, test) {
27+
var is = convert(test)
528

6-
if (!parent || !parent.type || !parent.children) {
7-
throw new Error('Expected parent node')
8-
}
29+
if (!parent || !parent.type || !parent.children) {
30+
throw new Error('Expected parent node')
31+
}
932

10-
if (typeof index === 'number') {
11-
if (index < 0 || index === Number.POSITIVE_INFINITY) {
12-
throw new Error('Expected positive finite number as index')
13-
}
14-
} else {
15-
index = parent.children.indexOf(index)
33+
if (typeof index === 'number') {
34+
if (index < 0 || index === Number.POSITIVE_INFINITY) {
35+
throw new Error('Expected positive finite number as index')
36+
}
37+
} else {
38+
index = parent.children.indexOf(index)
1639

17-
if (index < 0) {
18-
throw new Error('Expected child node or index')
19-
}
20-
}
40+
if (index < 0) {
41+
throw new Error('Expected child node or index')
42+
}
43+
}
2144

22-
while (++index < parent.children.length) {
23-
if (is(parent.children[index], index, parent)) {
24-
return parent.children[index]
25-
}
26-
}
45+
while (++index < parent.children.length) {
46+
if (is(parent.children[index], index, parent)) {
47+
return parent.children[index]
48+
}
49+
}
2750

28-
return null
29-
}
51+
return null
52+
}
53+
)

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,35 @@
2525
"sideEffects": false,
2626
"type": "module",
2727
"main": "index.js",
28+
"types": "index.d.ts",
2829
"files": [
30+
"index.d.ts",
2931
"index.js"
3032
],
3133
"dependencies": {
34+
"@types/unist": "^2.0.0",
3235
"unist-util-is": "^5.0.0"
3336
},
3437
"devDependencies": {
38+
"@types/tape": "^4.0.0",
3539
"c8": "^7.0.0",
3640
"prettier": "^2.0.0",
3741
"remark": "^13.0.0",
3842
"remark-cli": "^9.0.0",
3943
"remark-preset-wooorm": "^8.0.0",
44+
"rimraf": "^3.0.0",
4045
"tape": "^5.0.0",
46+
"type-coverage": "^2.0.0",
47+
"typescript": "^4.0.0",
4148
"xo": "^0.38.0"
4249
},
4350
"scripts": {
51+
"prepack": "npm run build && npm run format",
52+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4453
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4554
"test-api": "node test.js",
4655
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
47-
"test": "npm run format && npm run test-coverage"
56+
"test": "npm run format && npm run build && npm run test-coverage"
4857
},
4958
"prettier": {
5059
"tabWidth": 2,
@@ -57,6 +66,7 @@
5766
"xo": {
5867
"prettier": true,
5968
"rules": {
69+
"import/no-mutable-exports": "off",
6070
"no-var": "off",
6171
"prefer-arrow-callback": "off"
6272
}
@@ -65,5 +75,10 @@
6575
"plugins": [
6676
"preset-wooorm"
6777
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true
6883
}
6984
}

test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
*/
4+
15
import test from 'tape'
26
import remark from 'remark'
37
import {findAfter} from './index.js'
@@ -9,6 +13,7 @@ var children = paragraph.children
913
test('unist-util-find-after', function (t) {
1014
t.throws(
1115
function () {
16+
// @ts-ignore runtime.
1217
findAfter()
1318
},
1419
/Expected parent node/,
@@ -17,6 +22,7 @@ test('unist-util-find-after', function (t) {
1722

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

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

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

4250
t.throws(
4351
function () {
52+
// @ts-ignore runtime.
4453
findAfter({type: 'foo', children: []}, {type: 'bar'})
4554
},
4655
/Expected child node or index/,
@@ -52,6 +61,7 @@ test('unist-util-find-after', function (t) {
5261
findAfter(
5362
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
5463
0,
64+
// @ts-ignore runtime.
5565
false
5666
)
5767
},
@@ -64,6 +74,7 @@ test('unist-util-find-after', function (t) {
6474
findAfter(
6575
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
6676
0,
77+
// @ts-ignore runtime.
6778
true
6879
)
6980
},
@@ -160,7 +171,11 @@ test('unist-util-find-after', function (t) {
160171
'should return a child when given a `test` and existing (#4)'
161172
)
162173

163-
function test(node, n) {
174+
/**
175+
* @param {Node} _
176+
* @param {number} n
177+
*/
178+
function test(_, n) {
164179
return n === 5
165180
}
166181

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true
14+
}
15+
}

0 commit comments

Comments
 (0)