From b860929d1e915f4cd999f2e3f5132f68bf3857f6 Mon Sep 17 00:00:00 2001 From: mrzmmr Date: Wed, 29 Jul 2020 10:12:21 -0700 Subject: [PATCH 1/2] Add types --- index.d.ts | 20 ++++++++++++++++ package.json | 12 ++++++++-- tsconfig.json | 10 ++++++++ tslint.json | 8 +++++++ unist-util-find-all-before-test.ts | 38 ++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 index.d.ts create mode 100644 tsconfig.json create mode 100644 tslint.json create mode 100644 unist-util-find-all-before-test.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6235e24 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,20 @@ +// TypeScript Version: 3.5 + +import {Node, Parent} from 'unist' +import {Test} from 'unist-util-is' + +export = findAllBefore + +/** + * Unist utility to get all children of a parent before a node or index + * + * @param parent Parent to search in + * @param index or Node to start from + * @param test that Nodes must pass to be included + * @returns Array of found Nodes + */ +declare function findAllBefore( + parent: Parent, + index: number | Node, + test?: Test | Array> +): Node[] diff --git a/package.json b/package.json index 2bc1098..12568a7 100644 --- a/package.json +++ b/package.json @@ -22,11 +22,17 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], + "files": [ + "index.js", + "index.d.ts" + ], + "types": "index.d.ts", "dependencies": { "unist-util-is": "^4.0.0" }, "devDependencies": { "browserify": "^16.0.0", + "dtslint": "^3.6.14", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark": "^12.0.0", @@ -43,7 +49,8 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run format && npm run build && npm run test-coverage" + "test-types": "dtslint .", + "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, @@ -66,7 +73,8 @@ "unicorn/prefer-number-properties": "off" }, "ignore": [ - "unist-util-find-all-before.js" + "unist-util-find-all-before.js", + "*.ts" ] }, "remarkConfig": { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..65db59c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-find-all-before": ["index.d.ts"] + } + } +} \ No newline at end of file diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..c25b128 --- /dev/null +++ b/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false, + "strict-export-declare-modifiers": false + } +} \ No newline at end of file diff --git a/unist-util-find-all-before-test.ts b/unist-util-find-all-before-test.ts new file mode 100644 index 0000000..2fd4afb --- /dev/null +++ b/unist-util-find-all-before-test.ts @@ -0,0 +1,38 @@ +import {Node, Parent} from 'unist' +import findAllBefore = require('unist-util-find-all-before') + +const heading: Node = { + type: 'heading', + depth: 2, + children: [] +} + +const parent: Parent = { + type: 'root', + children: [heading] +} + +// Missing params +// $ExpectError +findAllBefore() + +// $ExpectError +findAllBefore(parent) + +// Find by index or node +findAllBefore(parent, 1) +findAllBefore(parent, heading) + +// Invalid test type +// $ExpectError +findAllBefore(parent, 1, false) + +// Valid test type +findAllBefore(parent, 1, 'paragraph') + +// Invalid retrurn type +// $ExpectError +const returnsString: string = findAllBefore(parent, 1) + +// Valid return type +const nodes: Node[] = findAllBefore(parent, 1) From 94e9d4609001faf2db4e4aae0d6115a632e62881 Mon Sep 17 00:00:00 2001 From: Paul Zimmer Date: Wed, 29 Jul 2020 21:21:56 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Christian Murphy --- tsconfig.json | 2 +- tslint.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 65db59c..4deefd5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,4 +7,4 @@ "unist-util-find-all-before": ["index.d.ts"] } } -} \ No newline at end of file +} diff --git a/tslint.json b/tslint.json index c25b128..26aae6e 100644 --- a/tslint.json +++ b/tslint.json @@ -5,4 +5,4 @@ "whitespace": false, "strict-export-declare-modifiers": false } -} \ No newline at end of file +}