From 39050d9bee3e05190fb4c925b5302e62e9183931 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 17 Mar 2021 11:42:20 +0100 Subject: [PATCH] Add JSDoc based types --- .gitignore | 1 + index.js | 24 ++++++++++++++++++++---- package.json | 18 ++++++++++++++++-- readme.md | 2 +- test.js | 9 +++++++++ tsconfig.json | 16 ++++++++++++++++ 6 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 735f4af..c977c85 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +*.d.ts *.log coverage/ node_modules/ diff --git a/index.js b/index.js index e3bb8ee..bc00bb2 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,21 @@ -export function bail(error) { - if (error) { - throw error +// prettier-ignore +/** + * Throw a given error. + * + * @type {{ + * (error: Error): never + * (error: null | undefined): void + * (): void + * }} */ +export var bail = ( + /** + * Throw a given error. + * + * @param {Error?} [error] + */ + function (error) { + if (error) { + throw error + } } -} +) diff --git a/package.json b/package.json index 33d3e15..cf8fbd6 100644 --- a/package.json +++ b/package.json @@ -23,22 +23,30 @@ "sideEffects": false, "type": "module", "main": "index.js", + "types": "index.d.ts", "files": [ + "index.d.ts", "index.js" ], "devDependencies": { - "c8": "^7.6.0", + "@types/tape": "^4.0.0", + "c8": "^7.0.0", "prettier": "^2.0.0", "remark-cli": "^9.0.0", "remark-preset-wooorm": "^8.0.0", + "rimraf": "^3.0.0", "tape": "^5.0.0", + "type-coverage": "^2.0.0", + "typescript": "^4.0.0", "xo": "^0.38.0" }, "scripts": { + "prepack": "npm run build && npm run format", + "build": "rimraf \"*.d.ts\" && tsc && type-coverage", "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", "test-api": "node test.js", "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", - "test": "npm run format && npm run test-coverage" + "test": "npm run build && npm run format && npm run test-coverage" }, "prettier": { "tabWidth": 2, @@ -51,6 +59,7 @@ "xo": { "prettier": true, "rules": { + "import/no-mutable-exports": "off", "no-var": "off", "prefer-arrow-callback": "off" } @@ -59,5 +68,10 @@ "plugins": [ "preset-wooorm" ] + }, + "typeCoverage": { + "atLeast": 100, + "detail": true, + "strict": true } } diff --git a/readme.md b/readme.md index 89a2c98..9ac7f9e 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ npm install bail ## Use ```js -var bail = require('bail') +import {bail} from 'bail' bail() diff --git a/test.js b/test.js index e162a6a..7749e4c 100644 --- a/test.js +++ b/test.js @@ -6,7 +6,16 @@ test('bail([err])', function (t) { bail() }) + t.doesNotThrow(function () { + bail(null) + }) + + t.doesNotThrow(function () { + bail(undefined) + }) + t.throws(function () { + // @ts-ignore bail('foo') }, /foo/) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8ac10fe --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "files": ["index.js"], + "include": ["*.js"], + "compilerOptions": { + "target": "ES2020", + "lib": ["ES2020"], + "module": "ES2020", + "moduleResolution": "node", + "allowJs": true, + "checkJs": true, + "declaration": true, + "emitDeclarationOnly": true, + "allowSyntheticDefaultImports": true, + "skipLibCheck": true + } +}