Skip to content

Commit 9f621f0

Browse files
committed
Add JSDoc based types
1 parent aec7eca commit 9f621f0

File tree

7 files changed

+89
-93
lines changed

7 files changed

+89
-93
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.d.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

index.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
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 findAllAfter(parent, index, test) {
4-
var is = convert(test)
5-
var results = []
12+
export const findAllAfter =
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>>) => Array.<T>) &
16+
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Array.<Node>)
17+
* )}
18+
*/
19+
(
20+
/**
21+
* Utility to get all children of a parent after a node or index
22+
*
23+
* @param {Parent} parent Parent node
24+
* @param {Node|number} index Child of `parent`, or it’s index
25+
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type)
26+
* @returns {Array.<Node>}
27+
*/
28+
function (parent, index, test) {
29+
var is = convert(test)
30+
/** @type {Array.<Node>} */
31+
var results = []
632

7-
if (!parent || !parent.type || !parent.children) {
8-
throw new Error('Expected parent node')
9-
}
33+
if (!parent || !parent.type || !parent.children) {
34+
throw new Error('Expected parent node')
35+
}
1036

11-
if (typeof index === 'number') {
12-
if (index < 0 || index === Number.POSITIVE_INFINITY) {
13-
throw new Error('Expected positive finite number as index')
14-
}
15-
} else {
16-
index = parent.children.indexOf(index)
37+
if (typeof index === 'number') {
38+
if (index < 0 || index === Number.POSITIVE_INFINITY) {
39+
throw new Error('Expected positive finite number as index')
40+
}
41+
} else {
42+
index = parent.children.indexOf(index)
1743

18-
if (index < 0) {
19-
throw new Error('Expected child node or index')
20-
}
21-
}
44+
if (index < 0) {
45+
throw new Error('Expected child node or index')
46+
}
47+
}
2248

23-
while (++index < parent.children.length) {
24-
if (is(parent.children[index], index, parent)) {
25-
results.push(parent.children[index])
26-
}
27-
}
49+
while (++index < parent.children.length) {
50+
if (is(parent.children[index], index, parent)) {
51+
results.push(parent.children[index])
52+
}
53+
}
2854

29-
return results
30-
}
55+
return results
56+
}
57+
)

package.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,29 @@
3333
"index.js"
3434
],
3535
"dependencies": {
36+
"@types/unist": "^2.0.0",
3637
"unist-util-is": "^5.0.0"
3738
},
3839
"devDependencies": {
3940
"@types/tape": "^4.0.0",
4041
"c8": "^7.0.0",
41-
"dtslint": "^4.0.0",
4242
"prettier": "^2.0.0",
4343
"remark": "^13.0.0",
4444
"remark-cli": "^9.0.0",
4545
"remark-preset-wooorm": "^8.0.0",
46+
"rimraf": "^3.0.0",
4647
"tape": "^5.0.0",
48+
"type-coverage": "^2.0.0",
4749
"typescript": "^4.0.0",
4850
"xo": "^0.38.0"
4951
},
5052
"scripts": {
53+
"prepack": "npm run build && npm run format",
54+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5155
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5256
"test-api": "node test.js",
5357
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54-
"test-types": "# dtslint .",
55-
"test": "npm run format && npm run test-coverage && npm run test-types"
58+
"test": "npm run build && npm run format && npm run test-coverage"
5659
},
5760
"prettier": {
5861
"tabWidth": 2,
@@ -67,14 +70,16 @@
6770
"rules": {
6871
"no-var": "off",
6972
"prefer-arrow-callback": "off"
70-
},
71-
"ignore": [
72-
"*.d.ts"
73-
]
73+
}
7474
},
7575
"remarkConfig": {
7676
"plugins": [
7777
"preset-wooorm"
7878
]
79+
},
80+
"typeCoverage": {
81+
"atLeast": 100,
82+
"detail": true,
83+
"strict": true
7984
}
8085
}

test.js

Lines changed: 15 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 {findAllAfter} from './index.js'
@@ -9,6 +13,7 @@ var children = paragraph.children
913
test('unist-util-find-all-after', function (t) {
1014
t.throws(
1115
function () {
16+
// @ts-ignore runtime.
1217
findAllAfter()
1318
},
1419
/Expected parent node/,
@@ -17,6 +22,7 @@ test('unist-util-find-all-after', function (t) {
1722

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

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

5057
t.throws(
5158
function () {
59+
// @ts-ignore runtime.
5260
findAllAfter({type: 'foo', children: []}, false)
5361
},
5462
/Expected child node or index/,
@@ -76,6 +84,7 @@ test('unist-util-find-all-after', function (t) {
7684
findAllAfter(
7785
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
7886
0,
87+
// @ts-ignore runtime.
7988
false
8089
)
8190
},
@@ -88,6 +97,7 @@ test('unist-util-find-all-after', function (t) {
8897
findAllAfter(
8998
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
9099
0,
100+
// @ts-ignore runtime.
91101
true
92102
)
93103
},
@@ -184,7 +194,11 @@ test('unist-util-find-all-after', function (t) {
184194
'should return a child when given a `test` and existing (#4)'
185195
)
186196

187-
function test(node, n) {
197+
/**
198+
* @param {Node} _
199+
* @param {number} n
200+
*/
201+
function test(_, n) {
188202
return n >= 5
189203
}
190204

tsconfig.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
2+
"include": ["*.js"],
23
"compilerOptions": {
3-
"lib": ["es2015"],
4-
"strict": true,
5-
"baseUrl": ".",
6-
"paths": {
7-
"unist-util-find-all-after": ["index.d.ts"]
8-
}
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
914
}
1015
}

unist-util-find-all-after-test.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)