Skip to content

Commit e7c26af

Browse files
committed
Add JSDoc based types
1 parent df038c8 commit e7c26af

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
/**
2+
* @typedef {import('unist').Node} UnistNode
3+
* @typedef {import('hast').Element} HastElement
4+
* @typedef {import('unist-util-visit').Visitor<HastElement>} VisitElement
5+
*/
6+
17
import {headingRank} from 'hast-util-heading-rank'
28
import {visit} from 'unist-util-visit'
39

10+
/**
11+
* @template {UnistNode} T
12+
* @param {T} tree
13+
* @param {number} shift
14+
* @returns {T}
15+
*/
416
export function shiftHeading(tree, shift) {
517
if (
618
typeof shift !== 'number' ||
@@ -15,6 +27,7 @@ export function shiftHeading(tree, shift) {
1527

1628
return tree
1729

30+
/** @type {VisitElement} */
1831
function visitor(node) {
1932
var rank = headingRank(node)
2033

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,37 @@
3030
"sideEffects": false,
3131
"type": "module",
3232
"main": "index.js",
33+
"types": "index.d.ts",
3334
"files": [
35+
"index.d.ts",
3436
"index.js"
3537
],
3638
"dependencies": {
39+
"@types/hast": "^2.0.0",
40+
"@types/unist": "^2.0.0",
3741
"hast-util-heading-rank": "^2.0.0",
3842
"unist-util-visit": "^3.0.0"
3943
},
4044
"devDependencies": {
45+
"@types/tape": "^4.0.0",
4146
"c8": "^7.0.0",
4247
"hastscript": "^7.0.0",
4348
"prettier": "^2.0.0",
4449
"remark-cli": "^9.0.0",
4550
"remark-preset-wooorm": "^8.0.0",
51+
"rimraf": "^3.0.0",
4652
"tape": "^5.0.0",
53+
"type-coverage": "^2.0.0",
54+
"typescript": "^4.0.0",
4755
"xo": "^0.39.0"
4856
},
4957
"scripts": {
58+
"prepack": "npm run build && npm run format",
59+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5060
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5161
"test-api": "node test.js",
5262
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
53-
"test": "npm run format && npm run test-coverage"
63+
"test": "npm run build && npm run format && npm run test-coverage"
5464
},
5565
"prettier": {
5666
"tabWidth": 2,
@@ -71,5 +81,10 @@
7181
"plugins": [
7282
"preset-wooorm"
7383
]
84+
},
85+
"typeCoverage": {
86+
"atLeast": 100,
87+
"detail": true,
88+
"strict": true
7489
}
7590
}

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {shiftHeading} from './index.js'
55
test('shiftHeading', function (t) {
66
t.throws(
77
function () {
8+
// @ts-ignore runtime.
89
shiftHeading(h(''))
910
},
1011
/^Error: Expected a non-null finite integer, not `undefined`$/,

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)