Skip to content

Commit 968e97c

Browse files
authored
Add types
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent a8e3c3f commit 968e97c

File tree

6 files changed

+96
-4
lines changed

6 files changed

+96
-4
lines changed

Diff for: index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function iteratorFactory(callback) {
2121
throw new Error('Missing children in `parent` for `modifier`')
2222
}
2323

24-
return iterate(children, callback, parent)
24+
iterate(children, callback, parent)
2525
}
2626
}
2727

Diff for: package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@
2121
},
2222
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
2323
"contributors": [
24-
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
24+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
25+
"Merlijn Vos <merlijn@soverin.net>"
2526
],
2627
"files": [
27-
"index.js"
28+
"index.js",
29+
"types/index.d.ts"
2830
],
31+
"types": "types/index.d.ts",
2932
"dependencies": {
3033
"array-iterate": "^1.0.0"
3134
},
3235
"devDependencies": {
3336
"browserify": "^16.0.0",
37+
"dtslint": "^3.3.0",
3438
"nyc": "^15.0.0",
3539
"prettier": "^1.0.0",
3640
"remark-cli": "^7.0.0",
3741
"remark-preset-wooorm": "^6.0.0",
3842
"tape": "^4.0.0",
3943
"tinyify": "^2.0.0",
44+
"unified": "^8.4.2",
4045
"xo": "^0.26.0"
4146
},
4247
"scripts": {
@@ -46,7 +51,8 @@
4651
"build": "npm run build-bundle && npm run build-mangle",
4752
"test-api": "node test",
4853
"test-coverage": "nyc --reporter lcov tape test.js",
49-
"test": "npm run format && npm run build && npm run test-coverage"
54+
"test-types": "dtslint types",
55+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
5056
},
5157
"prettier": {
5258
"tabWidth": 2,

Diff for: types/index.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node, Parent} from 'unist'
4+
5+
declare namespace unistUtilModifyChildren {
6+
type Modifier = (
7+
node: Node,
8+
index: number,
9+
parent: Parent
10+
) => number | void
11+
12+
type Modify = (tree: Node) => void
13+
}
14+
15+
/**
16+
* unist utility to modify direct children of a parent.
17+
*
18+
* @param callback modifier function that (optionally) returns a next position (number) to iterate.
19+
* @returns callback to be used on the tree.
20+
*/
21+
declare function unistUtilModifyChildren(
22+
modifier: unistUtilModifyChildren.Modifier
23+
): unistUtilModifyChildren.Modify
24+
25+
export = unistUtilModifyChildren

Diff for: types/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"unist-util-modify-children": ["index.d.ts"]
8+
}
9+
}
10+
}

Diff for: types/tslint.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
{
3+
"extends": "dtslint/dtslint.json",
4+
"rules": {
5+
"whitespace": false,
6+
"semicolon": false
7+
}
8+
}

Diff for: types/unist-util-modify-children-test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Node} from 'unist'
2+
3+
import unified = require('unified')
4+
5+
import * as modifyChildren from 'unist-util-modify-children'
6+
7+
const node: Node = {
8+
type: 'root',
9+
children: [
10+
{type: 'leaf', value: '1'},
11+
{type: 'leaf', children: [{type: 'leaf', value: '2'}]},
12+
{type: 'leaf', value: '3'}
13+
]
14+
}
15+
16+
// $ExpectType Modify
17+
modifyChildren((node, index) => index + 1)
18+
19+
// $ExpectType Modify
20+
modifyChildren(() => {})
21+
22+
// $ExpectError
23+
modifyChildren(() => '')
24+
25+
// $ExpectType void
26+
modifyChildren((node, index) => index + 1)(node)
27+
28+
// Usable in unified transform
29+
unified().use(() => tree => {
30+
const modify = modifyChildren((node, index, parent) => {
31+
if (node.type === 'node') {
32+
parent.children.splice(index, 1, {
33+
type: 'subtree',
34+
children: node.children
35+
})
36+
return index + 1
37+
}
38+
})
39+
40+
modify(tree)
41+
42+
return tree
43+
})

0 commit comments

Comments
 (0)