Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 077d217

Browse files
committed
Add JSDoc based types
1 parent f909b13 commit 077d217

File tree

9 files changed

+66
-45
lines changed

9 files changed

+66
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coverage/
22
node_modules/
33
.DS_Store
4+
*.d.ts
45
*.log
56
yarn.lock

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('hast').Properties} Properties
4+
*/
5+
16
import {toString} from 'mdast-util-to-string'
27
import {visit} from 'unist-util-visit'
38
import BananaSlug from 'github-slugger'
49

510
const slugs = new BananaSlug()
611

7-
// Patch slugs on heading nodes.
12+
/**
13+
* Plugin to add anchors headings using GitHub’s algorithm.
14+
*
15+
* @type {import('unified').Plugin<void[], Root>}
16+
*/
817
export default function remarkSlug() {
9-
return (ast) => {
18+
return (tree) => {
1019
slugs.reset()
1120

12-
visit(ast, 'heading', (node) => {
21+
visit(tree, 'heading', (node) => {
1322
const data = node.data || (node.data = {})
14-
const props = data.hProperties || (data.hProperties = {})
23+
const props = /** @type {Properties} */ (
24+
data.hProperties || (data.hProperties = {})
25+
)
1526
let id = props.id
1627

17-
id = id ? slugs.slug(id, true) : slugs.slug(toString(node))
28+
id = id ? slugs.slug(String(id), true) : slugs.slug(toString(node))
1829

1930
data.id = id
2031
props.id = id

package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,41 @@
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.3.2",
40+
"@types/mdast": "^3.0.0",
3741
"github-slugger": "^1.0.0",
3842
"mdast-util-to-string": "^3.0.0",
43+
"unified": "^10.0.0",
3944
"unist-util-visit": "^4.0.0"
4045
},
4146
"devDependencies": {
47+
"@types/github-slugger": "^1.3.0",
48+
"@types/tape": "^4.0.0",
4249
"c8": "^7.0.0",
4350
"prettier": "^2.0.0",
4451
"remark": "^14.0.0",
4552
"remark-cli": "^10.0.0",
4653
"remark-preset-wooorm": "^8.0.0",
54+
"rimraf": "^3.0.0",
4755
"tape": "^5.0.0",
56+
"type-coverage": "^2.0.0",
57+
"typescript": "^4.0.0",
4858
"unist-builder": "^3.0.0",
4959
"unist-util-remove-position": "^4.0.0",
5060
"xo": "^0.39.0"
5161
},
5262
"scripts": {
63+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5364
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5465
"test-api": "node --conditions development test.js",
5566
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
56-
"test": "npm run format && npm run build && npm run test-coverage"
67+
"test": "npm run build && npm run format && npm run test-coverage"
5768
},
5869
"prettier": {
5970
"tabWidth": 2,
@@ -64,14 +75,17 @@
6475
"trailingComma": "none"
6576
},
6677
"xo": {
67-
"prettier": true,
68-
"ignores": [
69-
"types/index.d.ts"
70-
]
78+
"prettier": true
7179
},
7280
"remarkConfig": {
7381
"plugins": [
7482
"preset-wooorm"
7583
]
84+
},
85+
"typeCoverage": {
86+
"atLeast": 100,
87+
"detail": true,
88+
"strict": true,
89+
"ignoreCatch": true
7690
}
7791
}

test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
15
import test from 'tape'
26
import {remark} from 'remark'
37
import {u} from 'unist-builder'
@@ -215,6 +219,10 @@ test('remarkSlug', (t) => {
215219
t.end()
216220
})
217221

222+
/**
223+
* @param {string|null} label
224+
* @param {string} id
225+
*/
218226
function heading(label, id) {
219227
return u(
220228
'heading',
@@ -223,7 +231,11 @@ function heading(label, id) {
223231
)
224232
}
225233

226-
function process(doc, options) {
227-
const processor = remark().use(remarkSlug, options)
234+
/**
235+
* @param {string} doc
236+
* @returns {Root}
237+
*/
238+
function process(doc) {
239+
const processor = remark().use(remarkSlug)
228240
return removePosition(processor.runSync(processor.parse(doc)), true)
229241
}

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
"strict": true
15+
}
16+
}

types/index.d.ts

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

types/test.ts

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

types/tsconfig.json

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

types/tslint.json

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

0 commit comments

Comments
 (0)