Skip to content

Commit d8075a8

Browse files
committed
Add JSDoc based types
1 parent a069621 commit d8075a8

File tree

5 files changed

+97
-31
lines changed

5 files changed

+97
-31
lines changed

Diff for: .gitignore

+1
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/

Diff for: index.js

+50-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,58 @@
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 findBefore(parent, index, test) {
4-
var is = convert(test)
12+
export var findBefore =
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-
// Performance.
23-
if (index > parent.children.length) {
24-
index = parent.children.length
25-
}
45+
// Performance.
46+
if (index > parent.children.length) {
47+
index = parent.children.length
48+
}
2649

27-
while (index--) {
28-
if (is(parent.children[index], index, parent)) {
29-
return parent.children[index]
30-
}
31-
}
50+
while (index--) {
51+
if (is(parent.children[index], index, parent)) {
52+
return parent.children[index]
53+
}
54+
}
3255

33-
return null
34-
}
56+
return null
57+
}
58+
)

Diff for: package.json

+16-1
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 build && npm run format && 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
}

Diff for: test.js

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

1823
t.throws(
1924
function () {
20-
findBefore({
21-
type: 'foo'
22-
})
25+
// @ts-ignore runtime
26+
findBefore({type: 'foo'})
2327
},
2428
/Expected parent node/,
2529
'should fail without parent node'
2630
)
2731

2832
t.throws(
2933
function () {
34+
// @ts-ignore runtime
3035
findBefore({type: 'foo', children: []})
3136
},
3237
/Expected child node or index/,
@@ -51,6 +56,7 @@ test('unist-util-find-before', function (t) {
5156

5257
t.throws(
5358
function () {
59+
// @ts-ignore runtime
5460
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
5561
},
5662
/Expected function, string, or object as test/,
@@ -59,6 +65,7 @@ test('unist-util-find-before', function (t) {
5965

6066
t.throws(
6167
function () {
68+
// @ts-ignore runtime
6269
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, true)
6370
},
6471
/Expected function, string, or object as test/,
@@ -154,7 +161,11 @@ test('unist-util-find-before', function (t) {
154161
'should return a child when given a `test` and existing (#4)'
155162
)
156163

157-
function test(node, n) {
164+
/**
165+
* @param {Node} _
166+
* @param {number} n
167+
*/
168+
function test(_, n) {
158169
return n === 3
159170
}
160171

Diff for: tsconfig.json

+15
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)