Skip to content

Commit bf0cd72

Browse files
committed
Add JSDoc based types
1 parent eefc92c commit bf0cd72

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('mdast').Heading} Heading
4+
*/
5+
16
import {visit} from 'unist-util-visit'
27

38
var max = 6
49

10+
/**
11+
* Make sure that there is only one top-level heading in the document by
12+
* adjusting headings depths accordingly.
13+
*
14+
* @template {Node} T
15+
* @param {T} tree
16+
* @returns {T}
17+
*/
518
export function normalizeHeadings(tree) {
19+
/** @type {boolean} */
620
var multiple
21+
/** @type {Heading} */
722
var first
23+
/** @type {Heading} */
824
var title
925

1026
visit(tree, 'heading', infer)
@@ -21,6 +37,9 @@ export function normalizeHeadings(tree) {
2137

2238
return tree
2339

40+
/**
41+
* @param {Heading} node
42+
*/
2443
function infer(node) {
2544
if (!first) {
2645
first = node
@@ -35,6 +54,9 @@ export function normalizeHeadings(tree) {
3554
}
3655
}
3756

57+
/**
58+
* @param {Heading} node
59+
*/
3860
function increase(node) {
3961
if (node !== title && node.depth < max) {
4062
node.depth++

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,33 @@
3131
"sideEffects": false,
3232
"type": "module",
3333
"main": "index.js",
34+
"types": "index.d.ts",
3435
"files": [
36+
"index.d.ts",
3537
"index.js"
3638
],
3739
"dependencies": {
40+
"@types/mdast": "^3.0.0",
41+
"@types/unist": "^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
"prettier": "^2.0.0",
4348
"remark": "^13.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
"unist-util-remove-position": "^4.0.0",
4856
"xo": "^0.39.0"
4957
},
5058
"scripts": {
59+
"prepack": "npm run build && npm run format",
60+
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage",
5161
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5262
"test-api": "node test/index.js",
5363
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
@@ -72,5 +82,10 @@
7282
"plugins": [
7383
"preset-wooorm"
7484
]
85+
},
86+
"typeCoverage": {
87+
"atLeast": 100,
88+
"detail": true,
89+
"strict": true
7590
}
7691
}

test/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ test('Level 7', function (t) {
1919
t.end()
2020
})
2121

22+
/**
23+
* @param {import('tape').Test} t
24+
* @param {string} test
25+
* @param {string} message
26+
*/
2227
function check(t, test, message) {
2328
var input = fs.readFileSync(path.join('test', 'fixture', test + '.in'))
2429
var output = fs.readFileSync(path.join('test', 'fixture', test + '.out'))

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js", "test/**.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)