From b37693930ea7ad6b7def91038bceeb4a1703ee1f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 5 May 2021 16:04:30 +0200 Subject: [PATCH] Add JSDoc based types --- .gitignore | 1 + index.js | 57 +++++++++++++++++++++++++++++++++++---------------- package.json | 18 +++++++++++++++- tsconfig.json | 15 ++++++++++++++ 4 files changed, 72 insertions(+), 19 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 505e286..f2683ef 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,29 @@ -// Get stats for a file, list of files, or list of messages. +/** + * @typedef {import('vfile').VFile} VFile + * @typedef {import('vfile-message').VFileMessage} VFileMessage + * + * @typedef Statistics + * @property {number} fatal Fatal errors (`fatal: true`) + * @property {number} warn warning errors (`fatal: false`) + * @property {number} info informational messages (`fatal: null|undefined`) + * @property {number} nonfatal warning + info + * @property {number} total nonfatal + fatal + */ + +/** + * Get stats for a file, list of files, or list of messages. + * + * @param {Array.|VFile|VFileMessage} value + * @returns {Statistics} + */ export function statistics(value) { var result = {true: 0, false: 0, null: 0} if (value) { - if (value[0] && value[0].messages) { - // Multiple vfiles. - countInAll(value) + if (Array.isArray(value)) { + list(value) } else { - // One vfile / messages. - countAll(value.messages || value) + one(value) } } @@ -20,23 +35,29 @@ export function statistics(value) { total: result.true + result.false + result.null } - function countInAll(files) { + /** + * @param {Array.} value + * @returns {void} + */ + function list(value) { var index = -1 - while (++index < files.length) { - countAll(files[index].messages) + while (++index < value.length) { + one(value[index]) } } - function countAll(messages) { - var index = -1 + /** + * @param {VFile|VFileMessage} value + * @returns {void} + */ + function one(value) { + if ('messages' in value) return list(value.messages) - while (++index < messages.length) { - result[ - messages[index].fatal === undefined || messages[index].fatal === null - ? null - : Boolean(messages[index].fatal) - ]++ - } + result[ + value.fatal === undefined || value.fatal === null + ? null + : Boolean(value.fatal) + ]++ } } diff --git a/package.json b/package.json index 7ce4333..7bf40f3 100644 --- a/package.json +++ b/package.json @@ -28,19 +28,30 @@ "sideEffects": false, "type": "module", "main": "index.js", + "types": "index.d.ts", "files": [ + "index.d.ts", "index.js" ], + "dependencies": { + "vfile-message": "^3.0.0" + }, "devDependencies": { - "c8": "^7.7.2", + "@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", "vfile": "^5.0.0", "xo": "^0.39.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", @@ -65,5 +76,10 @@ "plugins": [ "preset-wooorm" ] + }, + "typeCoverage": { + "atLeast": 100, + "detail": true, + "strict": true } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..be08abe --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ +{ + "include": ["*.js"], + "compilerOptions": { + "target": "ES2020", + "lib": ["ES2020"], + "module": "ES2020", + "moduleResolution": "node", + "allowJs": true, + "checkJs": true, + "declaration": true, + "emitDeclarationOnly": true, + "allowSyntheticDefaultImports": true, + "skipLibCheck": true + } +}