From 313bce7436908a41968d91082b0a1cc49ce1e8e8 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sat, 26 Oct 2019 12:58:29 -0700 Subject: [PATCH 1/2] types: add typings for unist util filter --- .npmrc | 1 + package.json | 14 +++++++--- types/index.d.ts | 47 ++++++++++++++++++++++++++++++++ types/tsconfig.json | 10 +++++++ types/tslint.json | 8 ++++++ types/unist-util-filter-tests.ts | 12 ++++++++ 6 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 .npmrc create mode 100644 types/index.d.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json create mode 100644 types/unist-util-filter-tests.ts diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/package.json b/package.json index 2cb5549..bf3568d 100644 --- a/package.json +++ b/package.json @@ -19,16 +19,21 @@ "author": "Eugene Sharygin ", "contributors": [ "Eugene Sharygin ", - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Christian Murphy " ], "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", @@ -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, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..236d945 --- /dev/null +++ b/types/index.d.ts @@ -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( + tree: Node, + filter?: Test +): 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( + tree: Node, + options: unistUtilFilter.Options, + filter?: Test +): T | null + +export = unistUtilFilter diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..f3c3fae --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-filter": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..7b05c3b --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "semicolon": false, + "no-redundant-jsdoc": false + } +} diff --git a/types/unist-util-filter-tests.ts b/types/unist-util-filter-tests.ts new file mode 100644 index 0000000..f0b2c6c --- /dev/null +++ b/types/unist-util-filter-tests.ts @@ -0,0 +1,12 @@ +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({type: 'root'}, {cascade: false}, 'root') // $ExpectType Node | null +filter({type: 'root'}, 'heading') // $ExpectType Heading | null +filter({type: 'root'}, 'notAHeading') // $ExpectError From cfb036cc0defef3211175f8bf2f722ebc6a49461 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 27 Oct 2019 21:33:10 -0700 Subject: [PATCH 2/2] test: remove duplicated type test --- types/unist-util-filter-tests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/unist-util-filter-tests.ts b/types/unist-util-filter-tests.ts index f0b2c6c..67a5dea 100644 --- a/types/unist-util-filter-tests.ts +++ b/types/unist-util-filter-tests.ts @@ -7,6 +7,5 @@ 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({type: 'root'}, {cascade: false}, 'root') // $ExpectType Node | null filter({type: 'root'}, 'heading') // $ExpectType Heading | null filter({type: 'root'}, 'notAHeading') // $ExpectError