Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 5, 2021
1 parent 0d00b56 commit b376939
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
57 changes: 39 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -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>|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)
}
}

Expand All @@ -20,23 +35,29 @@ export function statistics(value) {
total: result.true + result.false + result.null
}

function countInAll(files) {
/**
* @param {Array.<VFile|VFileMessage>} 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)
]++
}
}
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -65,5 +76,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit b376939

Please sign in to comment.