Skip to content

Commit 059cb08

Browse files
committed
Add JSDoc based types
1 parent 2492695 commit 059cb08

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1+
/**
2+
* @typedef {import('xast').Root} Root
3+
* @typedef {import('xast').Element} Element
4+
* @typedef {import('xast').RootChildMap} RootChildMap
5+
* @typedef {RootChildMap[keyof RootChildMap]} Child
6+
* @typedef {Root|Child} Node
7+
* @typedef {Root|Element} Parent
8+
*/
9+
10+
/**
11+
* @param {Node} node
12+
* @returns {string}
13+
*/
114
export function toString(node) {
215
// A root or an element
16+
// @ts-ignore Looks like a parent.
317
if ('children' in node) return all(node)
18+
// @ts-ignore Looks like a literal.
419
return 'value' in node ? node.value : ''
520
}
621

22+
/**
23+
* @param {Node} node
24+
* @returns {string}
25+
*/
726
function one(node) {
827
if (node.type === 'text') return node.value
928
// Ignore things like comments, instruction, cdata.
29+
// @ts-ignore Looks like a parent.
1030
return node.children ? all(node) : ''
1131
}
1232

33+
/**
34+
* @param {Parent} node
35+
* @returns {string}
36+
*/
1337
function all(node) {
1438
var children = node.children
1539
var index = -1
40+
/** @type {Array.<string>} */
1641
var result = []
1742

1843
while (++index < children.length) {

package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,34 @@
2727
"sideEffects": false,
2828
"type": "module",
2929
"main": "index.js",
30+
"types": "index.d.ts",
3031
"files": [
32+
"index.d.ts",
3133
"index.js"
3234
],
35+
"dependencies": {
36+
"@types/xast": "^1.0.0"
37+
},
3338
"devDependencies": {
39+
"@types/tape": "^4.0.0",
3440
"c8": "^7.0.0",
3541
"prettier": "^2.0.0",
3642
"remark-cli": "^9.0.0",
3743
"remark-preset-wooorm": "^8.0.0",
44+
"rimraf": "^3.0.2",
3845
"tape": "^5.0.0",
46+
"type-coverage": "^2.17.5",
47+
"typescript": "^4.2.4",
3948
"unist-builder": "^3.0.0",
4049
"xo": "^0.39.0"
4150
},
4251
"scripts": {
52+
"prepack": "npm run build && npm run format",
53+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4354
"format": "remark . -qfo && prettier . --write --loglevel warn && xo --fix",
4455
"test-api": "node test.js",
4556
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
46-
"test": "npm run format && npm run test-coverage"
57+
"test": "npm run build && npm run format && npm run test-coverage"
4758
},
4859
"prettier": {
4960
"tabWidth": 2,
@@ -64,5 +75,10 @@
6475
"plugins": [
6576
"preset-wooorm"
6677
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true
6783
}
6884
}

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)