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..7aae6cf --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,29 @@ +// TypeScript Version: 3.5 + +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 + +/** + * 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/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..85a1b7e --- /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 | null + +selectAll() // $ExpectError +selectAll('*') // $ExpectError +selectAll('*', {type: 'root'}) // $ExpectType Node[]