Skip to content

Commit df155b7

Browse files
committed
Add JSDoc based types
1 parent adc6d70 commit df155b7

File tree

8 files changed

+90
-102
lines changed

8 files changed

+90
-102
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 & 20 deletions
This file was deleted.

index.js

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,63 @@
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 findAllBefore(parent, index, test) {
4-
var is = convert(test)
5-
var results = []
6-
var offset = -1
12+
export const findAllBefore =
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 = []
32+
var offset = -1
733

8-
if (!parent || !parent.type || !parent.children) {
9-
throw new Error('Expected parent node')
10-
}
34+
if (!parent || !parent.type || !parent.children) {
35+
throw new Error('Expected parent node')
36+
}
1137

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

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

24-
// Performance.
25-
if (index > parent.children.length) {
26-
index = parent.children.length
27-
}
50+
// Performance.
51+
if (index > parent.children.length) {
52+
index = parent.children.length
53+
}
2854

29-
while (++offset < index) {
30-
if (is(parent.children[offset], offset, parent)) {
31-
results.push(parent.children[offset])
32-
}
33-
}
55+
while (++offset < index) {
56+
if (is(parent.children[offset], offset, parent)) {
57+
results.push(parent.children[offset])
58+
}
59+
}
3460

35-
return results
36-
}
61+
return results
62+
}
63+
)

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,29 @@
3131
"index.js"
3232
],
3333
"dependencies": {
34+
"@types/unist": "^2.0.0",
3435
"unist-util-is": "^5.0.0"
3536
},
3637
"devDependencies": {
38+
"@types/tape": "^4.0.0",
3739
"c8": "^7.0.0",
3840
"prettier": "^2.0.0",
3941
"remark": "^13.0.0",
4042
"remark-cli": "^9.0.0",
4143
"remark-preset-wooorm": "^8.0.0",
44+
"rimraf": "^3.0.0",
4245
"tape": "^5.0.0",
46+
"type-coverage": "^2.0.0",
47+
"typescript": "^4.0.0",
4348
"xo": "^0.38.0"
4449
},
4550
"scripts": {
51+
"prepack": "npm run build && npm run format",
52+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4653
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4754
"test-api": "node test.js",
4855
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
49-
"test": "npm run format && npm run test-coverage"
56+
"test": "npm run build && npm run format && npm run test-coverage"
5057
},
5158
"prettier": {
5259
"tabWidth": 2,
@@ -67,5 +74,10 @@
6774
"plugins": [
6875
"preset-wooorm"
6976
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true
7082
}
7183
}

test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var children = paragraph.children
99
test('unist-util-find-all-before', function (t) {
1010
t.throws(
1111
function () {
12+
// @ts-ignore runtime.
1213
findAllBefore()
1314
},
1415
/Expected parent node/,
@@ -17,6 +18,7 @@ test('unist-util-find-all-before', function (t) {
1718

1819
t.throws(
1920
function () {
21+
// @ts-ignore runtime.
2022
findAllBefore({type: 'foo'})
2123
},
2224
/Expected parent node/,
@@ -25,6 +27,7 @@ test('unist-util-find-all-before', function (t) {
2527

2628
t.throws(
2729
function () {
30+
// @ts-ignore runtime.
2831
findAllBefore({type: 'foo', children: []})
2932
},
3033
/Expected child node or index/,
@@ -49,6 +52,7 @@ test('unist-util-find-all-before', function (t) {
4952

5053
t.throws(
5154
function () {
55+
// @ts-ignore runtime.
5256
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
5357
},
5458
/Expected function, string, or object as test/,
@@ -57,6 +61,7 @@ test('unist-util-find-all-before', function (t) {
5761

5862
t.throws(
5963
function () {
64+
// @ts-ignore runtime.
6065
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, true)
6166
},
6267
/Expected function, string, or object as test/,
@@ -154,7 +159,11 @@ test('unist-util-find-all-before', function (t) {
154159
'should return children when given a `test` and existing (#4)'
155160
)
156161

157-
function test(node, n) {
162+
/**
163+
* @param {unknown} _
164+
* @param {number} n
165+
*/
166+
function test(_, n) {
158167
return n > 3
159168
}
160169

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-before": ["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
}

tslint.json

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

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

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

0 commit comments

Comments
 (0)