Skip to content

Commit 17f2167

Browse files
Add types (#3)
Closes GH-3. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 1c7dadf commit 17f2167

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

Diff for: package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@
3030
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
3131
],
3232
"files": [
33-
"index.js"
33+
"index.js",
34+
"types/index.d.ts"
3435
],
36+
"types": "types/index.d.ts",
3537
"dependencies": {
3638
"unist-util-is": "^4.0.0"
3739
},
3840
"devDependencies": {
41+
"dtslint": "^4.0.0",
3942
"nyc": "^15.0.0",
4043
"prettier": "^2.0.0",
4144
"remark-cli": "^9.0.0",
@@ -48,7 +51,8 @@
4851
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4952
"test-api": "node test",
5053
"test-coverage": "nyc --reporter lcov tape test.js",
51-
"test": "npm run format && npm run test-coverage"
54+
"test-types": "dtslint types",
55+
"test": "npm run format && npm run test-coverage && npm run test-types"
5256
},
5357
"nyc": {
5458
"check-coverage": true,

Diff for: types/index.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node} from 'unist'
4+
import {Test} from 'unist-util-is'
5+
6+
// NOTE: namespace is needed to use `export = remove`
7+
declare namespace remove {
8+
interface RemoveOptions {
9+
/**
10+
* Whether to drop parent nodes if they had children, but all their children were filtered out test
11+
*/
12+
cascade?: boolean
13+
}
14+
}
15+
16+
/**
17+
*
18+
* Mutate the given tree by removing all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
19+
*
20+
* @param tree Tree to filter
21+
* @param test is-compatible test (such as a type)
22+
*/
23+
declare function remove<T extends Node>(tree: T, test?: Test<Node>): T | null
24+
/**
25+
*
26+
* Mutate the given tree by removing all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
27+
*
28+
* @param tree Tree to filter
29+
* @param options Whether to drop parent nodes if they had children, but all their children were filtered out. Default is {cascade: true}
30+
* @param test is-compatible test (such as a type)
31+
*/
32+
declare function remove<T extends Node>(
33+
tree: T,
34+
options?: remove.RemoveOptions,
35+
test?: Test<Node>
36+
): T | null
37+
38+
export = remove

Diff for: types/test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as remove from 'uninst-util-remove'
2+
import * as u from 'unist-builder'
3+
4+
const tree = u('root', [
5+
u('leaf', '1'),
6+
u('node', [
7+
u('leaf', '2'),
8+
u('node', [u('leaf', '3'), u('other', '4')]),
9+
u('node', [u('leaf', '5')])
10+
]),
11+
u('leaf', '6')
12+
])
13+
14+
remove() // $ExpectError
15+
remove('leaf') // $ExpectError
16+
17+
remove(tree)
18+
remove(tree, 'leaf')
19+
remove(tree, {cascade: false}, 'leaf')

Diff for: types/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "es2015",
4+
"lib": ["es2015"],
5+
"moduleResolution": "node",
6+
"strict": true,
7+
"noEmit": true,
8+
"baseUrl": ".",
9+
"paths": {
10+
"uninst-util-remove": ["."]
11+
}
12+
}
13+
}
14+

Diff for: types/tslint.json

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

0 commit comments

Comments
 (0)