Skip to content

Commit 7608c62

Browse files
committed
Add JSDoc based types
1 parent 90c3158 commit 7608c62

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

Diff for: .gitignore

+1
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/

Diff for: index.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
// Stringify one nlcst node or list of nodes.
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
*/
4+
5+
/**
6+
* Stringify one nlcst node or list of nodes.
7+
*
8+
* @param {unknown} node
9+
* @param {string} [separator='']
10+
* @returns {string}
11+
*/
212
export function toString(node, separator = '') {
3-
var values
4-
var length
13+
var index = -1
14+
/** @type {Array.<Node>} */
515
var children
16+
/** @type {Array.<string>} */
17+
var values
618

7-
if (!node || (!('length' in node) && !node.type)) {
19+
// @ts-ignore Looks like an object.
20+
if (!node || (!Array.isArray(node) && !node.type)) {
821
throw new Error('Expected node, not `' + node + '`')
922
}
1023

11-
if (typeof node.value === 'string') {
12-
return node.value
13-
}
24+
// @ts-ignore Looks like a literal.
25+
if (typeof node.value === 'string') return node.value
1426

27+
// @ts-ignore Looks like a list of nodes or parent.
1528
children = 'length' in node ? node : node.children
16-
length = children.length
1729

1830
// Shortcut: This is pretty common, and a small performance win.
19-
if (length === 1 && 'value' in children[0]) {
31+
if (children.length === 1 && 'value' in children[0]) {
32+
// @ts-ignore Looks like a literal.
2033
return children[0].value
2134
}
2235

2336
values = []
2437

25-
while (length--) {
26-
values[length] = toString(children[length], separator)
38+
while (++index < children.length) {
39+
values[index] = toString(children[index], separator)
2740
}
2841

2942
return values.join(separator)

Diff for: package.json

+17-1
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/unist": "^2.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.0",
3845
"tape": "^5.0.0",
46+
"type-coverage": "^2.0.0",
47+
"typescript": "^4.0.0",
3948
"unist-builder": "^3.0.0",
4049
"xo": "^0.38.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 . -w --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
}

Diff for: tsconfig.json

+15
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)