Skip to content

Commit 583deff

Browse files
committed
Add JSDoc based types
1 parent 2d993f8 commit 583deff

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist-util-visit').Visitor<Node>} Visitor
4+
*/
5+
16
import {visit} from 'unist-util-visit'
27

3-
// Make an mdast tree compact by merging adjacent text nodes.
8+
/**
9+
* Make an mdast tree compact by merging adjacent text nodes.
10+
*
11+
* @template {Node} T
12+
* @param {T} tree
13+
* @returns {T}
14+
*/
415
export function compact(tree) {
516
visit(tree, visitor)
617

718
return tree
819

20+
/** @type {Visitor} */
921
function visitor(child, index, parent) {
1022
var siblings = parent ? parent.children : []
1123
var previous = index && siblings[index - 1]
@@ -16,10 +28,12 @@ export function compact(tree) {
1628
(child.type === 'text' || child.type === 'blockquote')
1729
) {
1830
if (child.value) {
31+
// @ts-ignore must be text.
1932
previous.value += child.value
2033
}
2134

2235
if (child.children) {
36+
// @ts-ignore must be block quote.
2337
previous.children = previous.children.concat(child.children)
2438
}
2539

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,31 @@
2626
"sideEffects": false,
2727
"type": "module",
2828
"main": "index.js",
29+
"types": "index.d.ts",
2930
"files": [
31+
"index.d.ts",
3032
"index.js"
3133
],
3234
"dependencies": {
35+
"@types/unist": "^2.0.0",
3336
"unist-util-visit": "^3.0.0"
3437
},
3538
"devDependencies": {
39+
"@types/tape": "^4.0.0",
3640
"c8": "^7.0.0",
3741
"prettier": "^2.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
"unist-builder": "^3.0.0",
4249
"xo": "^0.39.0"
4350
},
4451
"scripts": {
52+
"prepack": "npm run build && npm run format",
53+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4554
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4655
"test-api": "node test.js",
4756
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
@@ -66,5 +75,10 @@
6675
"plugins": [
6776
"preset-wooorm"
6877
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true
6983
}
7084
}

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)