Skip to content

Commit e101488

Browse files
committed
Add JSDoc based types
1 parent 08f972e commit e101488

File tree

8 files changed

+50
-42
lines changed

8 files changed

+50
-42
lines changed

.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/

index.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
// Get the text content of a node.
2-
// Prefer the node’s plain-text fields, otherwise serialize its children,
3-
// and if the given value is an array, serialize the nodes in it.
1+
/**
2+
* Get the text content of a node.
3+
* Prefer the node’s plain-text fields, otherwise serialize its children,
4+
* and if the given value is an array, serialize the nodes in it.
5+
*
6+
* @param {unknown} node
7+
* @returns {string}
8+
*/
49
export function toString(node) {
510
return (
611
(node &&
12+
typeof node === 'object' &&
13+
// @ts-ignore looks like a literal.
714
(node.value ||
15+
// @ts-ignore looks like an image.
816
node.alt ||
17+
// @ts-ignore looks like an image/link.
918
node.title ||
19+
// @ts-ignore looks like a parent.
1020
('children' in node && all(node.children)) ||
11-
('length' in node && all(node)))) ||
21+
(Array.isArray(node) && all(node)))) ||
1222
''
1323
)
1424
}
1525

26+
/**
27+
* @param {Array.<unknown>} values
28+
* @returns {string}
29+
*/
1630
function all(values) {
31+
/** @type {Array.<string>} */
1732
var result = []
1833
var index = -1
1934

package.json

+15-7
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,30 @@
2727
"sideEffects": false,
2828
"type": "module",
2929
"main": "index.js",
30-
"types": "types/index.d.ts",
30+
"types": "index.d.ts",
3131
"files": [
32-
"types/index.d.ts",
32+
"index.d.ts",
3333
"index.js"
3434
],
3535
"devDependencies": {
36+
"@types/tape": "^4.0.0",
3637
"c8": "^7.0.0",
3738
"prettier": "^2.0.0",
3839
"remark-cli": "^9.0.0",
3940
"remark-preset-wooorm": "^8.0.0",
41+
"rimraf": "^3.0.0",
4042
"tape": "^5.0.0",
43+
"type-coverage": "^2.0.0",
44+
"typescript": "^4.0.0",
4145
"xo": "^0.39.0"
4246
},
4347
"scripts": {
48+
"prepack": "npm run build && npm run format",
49+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4450
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4551
"test-api": "node test.js",
4652
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
47-
"test": "npm run format && npm run test-coverage"
53+
"test": "npm run build && npm run format && npm run test-coverage"
4854
},
4955
"prettier": {
5056
"tabWidth": 2,
@@ -59,14 +65,16 @@
5965
"rules": {
6066
"no-var": "off",
6167
"prefer-arrow-callback": "off"
62-
},
63-
"ignore": [
64-
"types"
65-
]
68+
}
6669
},
6770
"remarkConfig": {
6871
"plugins": [
6972
"preset-wooorm"
7073
]
74+
},
75+
"typeCoverage": {
76+
"atLeast": 100,
77+
"detail": true,
78+
"strict": true
7179
}
7280
}

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+
}

types/index.d.ts

-8
This file was deleted.

types/test.ts

-5
This file was deleted.

types/tsconfig.json

-11
This file was deleted.

types/tslint.json

-7
This file was deleted.

0 commit comments

Comments
 (0)