Skip to content

Add types #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
"author": "Eugene Sharygin <[email protected]>",
"contributors": [
"Eugene Sharygin <[email protected]>",
"Titus Wormer <[email protected]> (https://wooorm.com)"
"Titus Wormer <[email protected]> (https://wooorm.com)",
"Christian Murphy <[email protected]>"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"flatmap": "0.0.3",
"unist-util-is": "^3.0.0"
"unist-util-is": "^4.0.0"
},
"devDependencies": {
"@types/mdast": "^3.0.3",
"dtslint": "^0.9.9",
"nyc": "^14.0.0",
"prettier": "^1.0.0",
"remark-cli": "^6.0.0",
Expand All @@ -41,7 +46,8 @@
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
47 changes: 47 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// TypeScript Version: 3.5

import {Node} from 'unist'
import {Test} from 'unist-util-is'

declare namespace unistUtilFilter {
/**
* Options for unist util filter
*/
interface Options {
/**
* Whether to drop parent nodes if they had children,
* but all their children were filtered out
*
* @default true
*/
cascade?: boolean
}
}

/**
* Create a new tree consisting of copies of all nodes that pass test.
* The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
*
* @param tree Tree to filter
* @param filter unist-util-is compatible test
*/
declare function unistUtilFilter<T extends Node>(
tree: Node,
filter?: Test<T>
): T | null

/**
* Create a new tree consisting of copies of all nodes that pass test.
* The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
*
* @param tree Tree to filter
* @param options additional configuration
* @param filter unist-util-is compatible test
*/
declare function unistUtilFilter<T extends Node>(
tree: Node,
options: unistUtilFilter.Options,
filter?: Test<T>
): T | null

export = unistUtilFilter
10 changes: 10 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unist-util-filter": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false,
"no-redundant-jsdoc": false
}
}
11 changes: 11 additions & 0 deletions types/unist-util-filter-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as filter from 'unist-util-filter'
import {Heading} from 'mdast'

filter() // $ExpectError
filter({type: 'root'}) // $ExpectType Node | null
filter({type: 'root'}, 'root') // $ExpectType Node | null
filter({type: 'root'}, {}, 'root') // $ExpectType Node | null
filter({type: 'root'}, {notAnOption: true}, 'root') // $ExpectError
filter({type: 'root'}, {cascade: false}, 'root') // $ExpectType Node | null
filter<Heading>({type: 'root'}, 'heading') // $ExpectType Heading | null
filter<Heading>({type: 'root'}, 'notAHeading') // $ExpectError