diff --git a/package.json b/package.json index adcf96a..8c63a44 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,15 @@ "Titus Wormer (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", @@ -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, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..ee16410 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,38 @@ +// TypeScript Version: 3.5 + +import {Node} from 'unist' +import {Test} from 'unist-util-is' + +// NOTE: namespace is needed to use `export = remove` +declare namespace remove { + 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) + */ +declare function remove(tree: T, test?: Test): 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) + */ +declare function remove( + tree: T, + options?: remove.RemoveOptions, + test?: Test +): T | null + +export = remove diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..8693074 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,19 @@ +import * as 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') diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..05a67ff --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "es2015", + "lib": ["es2015"], + "moduleResolution": "node", + "strict": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "uninst-util-remove": ["."] + } + } +} + diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..4e872e2 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": true, + "semicolon": false, + "whitespace": false + } +}