Skip to content

Commit d8680ec

Browse files
committed
Add JSDoc based types
1 parent 5c35f92 commit d8680ec

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
*
5+
* @typedef {import('unist-util-is').Type} Type
6+
* @typedef {import('unist-util-is').Props} Props
7+
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
8+
*/
9+
110
import {convert} from 'unist-util-is'
211

12+
/** @type {Array.<Node>} */
13+
var empty
14+
15+
/**
16+
* @param {Node} node
17+
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test]
18+
* @returns {number}
19+
*/
320
export function size(node, test) {
421
var is = convert(test)
22+
523
return fastSize(node)
624

25+
/**
26+
* @param {Node} node
27+
*/
728
function fastSize(node) {
8-
var children = node && node.children
29+
/** @type {Array.<Node>} */
30+
// @ts-ignore Looks like a parent.
31+
var children = (node && node.children) || empty
932
var count = 0
1033
var index = -1
1134

1235
if (children && children.length > 0) {
1336
while (++index < children.length) {
37+
// @ts-ignore Looks like a parent.
1438
if (is(children[index], index, node)) count++
1539
count += fastSize(children[index])
1640
}

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,35 @@
2525
"sideEffects": false,
2626
"type": "module",
2727
"main": "index.js",
28+
"types": "index.d.ts",
2829
"files": [
30+
"index.d.ts",
2931
"index.js"
3032
],
3133
"dependencies": {
34+
"@types/unist": "^2.0.0",
3235
"unist-util-is": "^5.0.0"
3336
},
3437
"devDependencies": {
38+
"@types/tape": "^4.0.0",
3539
"c8": "^7.0.0",
3640
"hastscript": "^6.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
"xo": "^0.38.0"
4249
},
4350
"scripts": {
51+
"prepack": "npm run build && npm run format",
52+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4453
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4554
"test-api": "node test.js",
4655
"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"
56+
"test": "npm run build && npm run format && npm run test-coverage"
4857
},
4958
"prettier": {
5059
"tabWidth": 2,
@@ -65,5 +74,10 @@
6574
"plugins": [
6675
"preset-wooorm"
6776
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true
6882
}
6983
}

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)