Skip to content

Add types #3

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 19 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"unist-util-is": "^4.0.0"
},
"devDependencies": {
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
Expand All @@ -48,7 +51,8 @@
"format": "remark . -qfo && prettier . -w --loglevel warn && 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
33 changes: 33 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// TypeScript Version: 3.5

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

export interface RemoveOptions {
/**
* Whether to drop parent nodes if they had children, but all their children were filtered out test
*/
cascade?: boolean
}

/**
*
* 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.
*
* @param tree Tree to filter
* @param test is-compatible test (such as a type)
*/
export function remove<T extends Node>(tree: T, test?: Test<Node>): T | null
/**
*
* 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.
*
* @param tree Tree to filter
* @param options Whether to drop parent nodes if they had children, but all their children were filtered out. Default is {cascade: true}
* @param test is-compatible test (such as a type)
*/
export function remove<T extends Node>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is exported using module.exports = remove.

To reflect this correctly in the types, this should be exported using:

export = remove

tree: T,
options?: RemoveOptions,
test?: Test<Node>
): T | null
19 changes: 19 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {remove} from 'uninst-util-remove';
import * as u from 'unist-builder';

const tree = u('root', [
u('leaf', '1'),
u('node', [
u('leaf', '2'),
u('node', [u('leaf', '3'), u('other', '4')]),
u('node', [u('leaf', '5')])
]),
u('leaf', '6')
]);

remove() // $ExpectError
remove('leaf') // $ExpectError

remove(tree)
remove(tree, 'leaf');
remove(tree, {cascade: false}, 'leaf');
14 changes: 14 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "es2015",
"lib": ["es2015"],
"moduleResolution": "node",
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"uninst-util-remove": ["."]
}
}
}

9 changes: 9 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": true,
"semicolon": false,
"whitespace": false
}
}