Skip to content

Commit 3ec1a02

Browse files
ChristianMurphywooorm
authored andcommitted
Add types
Closes GH-2. Reviewed-by: Titus Wormer <tituswormer@gmail.com> Reviewed-by: Junyoung Choi <fluke8259@gmail.com>
1 parent 7699c99 commit 3ec1a02

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
"author": "Eugene Sharygin <eush77@gmail.com>",
2020
"contributors": [
2121
"Eugene Sharygin <eush77@gmail.com>",
22-
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
22+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
23+
"Christian Murphy <christian.murphy.42@gmail.com>"
2324
],
2425
"files": [
25-
"index.js"
26+
"index.js",
27+
"types/index.d.ts"
2628
],
29+
"types": "types/index.d.ts",
2730
"dependencies": {
2831
"flatmap": "0.0.3",
29-
"unist-util-is": "^3.0.0"
32+
"unist-util-is": "^4.0.0"
3033
},
3134
"devDependencies": {
35+
"@types/mdast": "^3.0.3",
36+
"dtslint": "^0.9.9",
3237
"nyc": "^14.0.0",
3338
"prettier": "^1.0.0",
3439
"remark-cli": "^6.0.0",
@@ -41,7 +46,8 @@
4146
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
4247
"test-api": "node test",
4348
"test-coverage": "nyc --reporter lcov tape test.js",
44-
"test": "npm run format && npm run test-coverage"
49+
"test-types": "dtslint types",
50+
"test": "npm run format && npm run test-coverage && npm run test-types"
4551
},
4652
"nyc": {
4753
"check-coverage": true,

types/index.d.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node} from 'unist'
4+
import {Test} from 'unist-util-is'
5+
6+
declare namespace unistUtilFilter {
7+
/**
8+
* Options for unist util filter
9+
*/
10+
interface Options {
11+
/**
12+
* Whether to drop parent nodes if they had children,
13+
* but all their children were filtered out
14+
*
15+
* @default true
16+
*/
17+
cascade?: boolean
18+
}
19+
}
20+
21+
/**
22+
* Create a new tree consisting of copies of all nodes that pass test.
23+
* The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
24+
*
25+
* @param tree Tree to filter
26+
* @param filter unist-util-is compatible test
27+
*/
28+
declare function unistUtilFilter<T extends Node>(
29+
tree: Node,
30+
filter?: Test<T>
31+
): T | null
32+
33+
/**
34+
* Create a new tree consisting of copies of all nodes that pass test.
35+
* The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
36+
*
37+
* @param tree Tree to filter
38+
* @param options additional configuration
39+
* @param filter unist-util-is compatible test
40+
*/
41+
declare function unistUtilFilter<T extends Node>(
42+
tree: Node,
43+
options: unistUtilFilter.Options,
44+
filter?: Test<T>
45+
): T | null
46+
47+
export = unistUtilFilter

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-filter": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

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

types/unist-util-filter-tests.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as filter from 'unist-util-filter'
2+
import {Heading} from 'mdast'
3+
4+
filter() // $ExpectError
5+
filter({type: 'root'}) // $ExpectType Node | null
6+
filter({type: 'root'}, 'root') // $ExpectType Node | null
7+
filter({type: 'root'}, {}, 'root') // $ExpectType Node | null
8+
filter({type: 'root'}, {notAnOption: true}, 'root') // $ExpectError
9+
filter({type: 'root'}, {cascade: false}, 'root') // $ExpectType Node | null
10+
filter<Heading>({type: 'root'}, 'heading') // $ExpectType Heading | null
11+
filter<Heading>({type: 'root'}, 'notAHeading') // $ExpectError

0 commit comments

Comments
 (0)