From 9a5d1860c0ebf54f99bfb5b0f53632b86b3a9f61 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sat, 26 Oct 2019 12:17:20 -0700 Subject: [PATCH 1/2] types: add typings for unist-util-select --- .npmrc | 1 + package.json | 11 ++++++++--- types/index.d.ts | 11 +++++++++++ types/tsconfig.json | 10 ++++++++++ types/tslint.json | 7 +++++++ types/unist-util-select-tests.ts | 13 +++++++++++++ 6 files changed, 50 insertions(+), 3 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-select-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 ee55bf8..187146e 100644 --- a/package.json +++ b/package.json @@ -31,12 +31,15 @@ "author": "Eugene Sharygin ", "contributors": [ "Eugene Sharygin ", - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Christian Murphy " ], "files": [ "index.js", - "lib/" + "lib/", + "types/index.d.ts" ], + "types": "types/index.d.ts", "dependencies": { "css-selector-parser": "^1.1.0", "not": "^0.1.0", @@ -45,6 +48,7 @@ "zwitch": "^1.0.3" }, "devDependencies": { + "dtslint": "^0.9.9", "nyc": "^14.0.0", "prettier": "^1.0.0", "remark-cli": "^6.0.0", @@ -57,7 +61,8 @@ "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test/index.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..b14a930 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,11 @@ +// TypeScript Version: 3.5 + +import {Node} from 'unist' + +declare function matches(selector: string, tree: Node): boolean + +declare function select(selector: string, tree: Node): Node + +declare function selectAll(selector: string, tree: Node): Node[] + +export {matches, select, selectAll} diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..9d2d1d3 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-select": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..6978386 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "semicolon": false + } +} diff --git a/types/unist-util-select-tests.ts b/types/unist-util-select-tests.ts new file mode 100644 index 0000000..25146ee --- /dev/null +++ b/types/unist-util-select-tests.ts @@ -0,0 +1,13 @@ +import {matches, select, selectAll} from 'unist-util-select' + +matches() // $ExpectError +matches('*') // $ExpectError +matches('*', {type: 'root'}) // $ExpectType boolean + +select() // $ExpectError +select('*') // $ExpectError +select('*', {type: 'root'}) // $ExpectType Node + +selectAll() // $ExpectError +selectAll('*') // $ExpectError +selectAll('*', {type: 'root'}) // $ExpectType Node[] From 273c4a4429bb8f5fcb1c6e78f600a2159cd6d1f1 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sat, 26 Oct 2019 12:23:24 -0700 Subject: [PATCH 2/2] types: add tsdoc and note that select can return null --- types/index.d.ts | 20 +++++++++++++++++++- types/unist-util-select-tests.ts | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index b14a930..7aae6cf 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2,10 +2,28 @@ import {Node} from 'unist' +/** + * Is there a match for the given selector in the Unist tree + * + * @param selector CSS-like selector + * @param tree Unist node or tree of nodes to search + */ declare function matches(selector: string, tree: Node): boolean -declare function select(selector: string, tree: Node): Node +/** + * Find first Node that matches selector + * + * @param selector CSS-like selector + * @param tree Unist node or tree of nodes to search + */ +declare function select(selector: string, tree: Node): Node | null +/** + * Find all Nodes that match selector + * + * @param selector CSS-like selector + * @param tree Unist node or tree of nodes to search + */ declare function selectAll(selector: string, tree: Node): Node[] export {matches, select, selectAll} diff --git a/types/unist-util-select-tests.ts b/types/unist-util-select-tests.ts index 25146ee..85a1b7e 100644 --- a/types/unist-util-select-tests.ts +++ b/types/unist-util-select-tests.ts @@ -6,7 +6,7 @@ matches('*', {type: 'root'}) // $ExpectType boolean select() // $ExpectError select('*') // $ExpectError -select('*', {type: 'root'}) // $ExpectType Node +select('*', {type: 'root'}) // $ExpectType Node | null selectAll() // $ExpectError selectAll('*') // $ExpectError