Skip to content

Commit 9ea9026

Browse files
committed
Add JSDoc based types
1 parent fc6f506 commit 9ea9026

9 files changed

+80
-89
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: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
*/
5+
6+
/**
7+
* Function called with a node to produce a new node.
8+
*
9+
* @callback MapFunction
10+
* @param {Node} node Current node being processed
11+
* @param {number} [index] Index of `node`, or `null`
12+
* @param {Parent} [parent] Parent of `node`, or `null`
13+
* @returns {Node} Node to be used in the new tree. Its children are not used: if the original node has children, those are mapped.
14+
*/
15+
16+
/**
17+
* Unist utility to create a new tree by mapping all nodes with the given function.
18+
*
19+
* @param {Node} tree Tree to map
20+
* @param {MapFunction} iteratee Function that returns a new node
21+
* @returns {Node} New mapped tree.
22+
*/
123
export function map(tree, iteratee) {
224
return preorder(tree, null, null)
325

26+
/**
27+
* @param {Node} node
28+
* @param {number} [index]
29+
* @param {Parent} [parent]
30+
* @returns {Node}
31+
*/
432
function preorder(node, index, parent) {
533
var newNode = Object.assign({}, iteratee(node, index, parent))
634

735
if (node.children) {
8-
newNode.children = node.children.map(function (child, index) {
36+
// @ts-ignore Looks like a parent.
37+
newNode.children = node.children.map(function (
38+
/** @type {Node} */ child,
39+
/** @type {number} */ index
40+
) {
41+
// @ts-ignore Looks like a parent.
942
return preorder(child, index, node)
1043
})
1144
}

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,35 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32-
"types": "types/index.d.ts",
32+
"types": "index.d.ts",
3333
"files": [
34-
"index.js",
35-
"types/index.d.ts"
34+
"index.d.ts",
35+
"index.js"
3636
],
37-
"dependencies": {},
37+
"dependencies": {
38+
"@types/unist": "^2.0.0"
39+
},
3840
"devDependencies": {
41+
"@types/tape": "^4.0.0",
3942
"c8": "^7.0.0",
40-
"dtslint": "^4.0.0",
4143
"prettier": "^2.0.0",
4244
"remark-cli": "^9.0.0",
4345
"remark-preset-wooorm": "^8.0.0",
46+
"rimraf": "^3.0.0",
4447
"tape": "^5.0.0",
48+
"type-coverage": "^2.0.0",
4549
"typescript": "^4.0.0",
4650
"unist-builder": "^2.0.0",
4751
"unist-util-is": "^4.0.0",
4852
"xo": "^0.38.0"
4953
},
5054
"scripts": {
55+
"prepack": "npm run build && npm run format",
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5157
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5258
"test-api": "node test.js",
5359
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54-
"test-types": "dtslint types",
55-
"test": "npm run format && npm run test-coverage && npm run test-types"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5661
},
5762
"prettier": {
5863
"tabWidth": 2,
@@ -73,5 +78,10 @@
7378
"plugins": [
7479
"preset-wooorm"
7580
]
81+
},
82+
"typeCoverage": {
83+
"atLeast": 100,
84+
"detail": true,
85+
"strict": true
7686
}
7787
}

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
*/
4+
15
import test from 'tape'
26
import u from 'unist-builder'
37
import {map} from './index.js'
@@ -16,19 +20,28 @@ test('unist-util-map', function (t) {
1620
)
1721

1822
t.deepEqual(
23+
// @ts-ignore runtime.
1924
map({}, addValue),
2025
{value: 'test'},
2126
'should work when passing an empty object'
2227
)
2328

2429
t.end()
2530

31+
/**
32+
* @param {Node} node
33+
* @returns {Node}
34+
*/
2635
function changeLeaf(node) {
2736
return node.type === 'leaf'
2837
? Object.assign({}, node, {value: 'CHANGED'})
2938
: node
3039
}
3140

41+
/**
42+
* @param {Node} node
43+
* @returns {Node?}
44+
*/
3245
function nullLeaf(node) {
3346
return node.type === 'leaf' ? null : node
3447
}

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

types/index.d.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

types/tsconfig.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

types/tslint.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

types/unist-util-map-tests.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)