Skip to content

Commit 446e58b

Browse files
committedFeb 4, 2023
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 6bbf985 commit 446e58b

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed
 

‎lib/index.js

+54-25
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,62 @@
22
* @typedef {import('vfile').VFile} VFile
33
* @typedef {import('vfile-message').VFileMessage} VFileMessage
44
* @typedef {import('vfile-statistics').Statistics} Statistics
5-
*
5+
*/
6+
7+
/**
68
* @typedef Options
79
* Configuration (optional).
8-
* @property {boolean} [color]
10+
* @property {boolean | null | undefined} [color]
911
* Use ANSI colors in report (default: depends).
1012
* The default behavior in Node.js is the check if color is supported.
11-
* @property {boolean} [verbose=false]
13+
* @property {boolean | null | undefined} [verbose=false]
1214
* Show message `note`s.
1315
* Notes are optional, additional, long descriptions.
14-
* @property {boolean} [quiet=false]
16+
* @property {boolean | null | undefined} [quiet=false]
1517
* Do not show files without messages.
16-
* @property {boolean} [silent=false]
18+
* @property {boolean | null | undefined} [silent=false]
1719
* Show errors only.
1820
* This does not show info and warning messages.
1921
* Also sets `quiet` to `true`.
20-
* @property {string} [defaultName='<stdin>']
22+
* @property {string | null | undefined} [defaultName='<stdin>']
2123
* Label to use for files without file path.
2224
* If one file and no `defaultName` is given, no name will show up in the report.
2325
*
24-
* @typedef Row
26+
* @typedef MessageRow
27+
* Message.
2528
* @property {string} place
29+
* Serialized positional info.
2630
* @property {string} label
31+
* Kind of message.
2732
* @property {string} reason
33+
* Reason.
2834
* @property {string} ruleId
35+
* Rule.
2936
* @property {string} source
37+
* Source.
38+
*
39+
* @typedef {keyof MessageRow} MessageColumn
3040
*
3141
* @typedef FileRow
42+
* File header row.
3243
* @property {'file'} type
44+
* Kind.
3345
* @property {VFile} file
46+
* Virtual file.
3447
* @property {Statistics} stats
48+
* Statistics.
3549
*
36-
* @typedef {Record<string, number>} Sizes
50+
* @typedef {Record<MessageColumn, number>} Sizes
51+
* Sizes for message columns.
3752
*
3853
* @typedef Info
39-
* @property {Array<FileRow|Row>} rows
54+
* Result.
55+
* @property {Array<FileRow | MessageRow>} rows
56+
* Rows.
4057
* @property {Statistics} stats
58+
* Total statistics.
4159
* @property {Sizes} sizes
60+
* Sizes for message columns.
4261
*/
4362

4463
import width from 'string-width'
@@ -65,17 +84,14 @@ const labels = {
6584
/**
6685
* Create a report from an error, one file, or multiple files.
6786
*
68-
* @param {Error|VFile|Array<VFile>} [files]
87+
* @param {Error | VFile | Array<VFile> | null | undefined} [files]
6988
* File(s) or error to report.
70-
* @param {Options} [options]
89+
* @param {Options | null | undefined} [options]
7190
* Configuration.
7291
* @returns {string}
7392
* Report.
7493
*/
75-
export function reporter(files, options = {}) {
76-
/** @type {boolean|undefined} */
77-
let one
78-
94+
export function reporter(files, options) {
7995
if (!files) {
8096
return ''
8197
}
@@ -85,33 +101,39 @@ export function reporter(files, options = {}) {
85101
return String(files.stack || files)
86102
}
87103

104+
const options_ = options || {}
105+
88106
// One file.
89-
if (!Array.isArray(files)) {
90-
one = true
91-
files = [files]
107+
if (Array.isArray(files)) {
108+
return format(transform(files, options_), false, options_)
92109
}
93110

94-
return format(transform(files, options), one, options)
111+
return format(transform([files], options_), true, options_)
95112
}
96113

97114
/**
115+
* Parse a list of messages.
116+
*
98117
* @param {Array<VFile>} files
118+
* List of files.
99119
* @param {Options} options
120+
* Options.
100121
* @returns {Info}
122+
* Rows.
101123
*/
102124
function transform(files, options) {
103-
/** @type {Array<FileRow|Row>} */
125+
/** @type {Array<FileRow | MessageRow>} */
104126
const rows = []
105127
/** @type {Array<VFileMessage>} */
106128
const all = []
107129
/** @type {Sizes} */
108-
const sizes = {}
130+
const sizes = {place: 0, label: 0, reason: 0, ruleId: 0, source: 0}
109131
let index = -1
110132

111133
while (++index < files.length) {
112134
// @ts-expect-error it works fine.
113135
const messages = sort({messages: [...files[index].messages]}).messages
114-
/** @type {Array<Row>} */
136+
/** @type {Array<MessageRow>} */
115137
const messageRows = []
116138
let offset = -1
117139

@@ -137,7 +159,7 @@ function transform(files, options) {
137159
source: message.source || ''
138160
}
139161

140-
/** @type {keyof row} */
162+
/** @type {MessageColumn} */
141163
let key
142164

143165
for (key in row) {
@@ -164,8 +186,13 @@ function transform(files, options) {
164186

165187
/**
166188
* @param {Info} map
167-
* @param {boolean|undefined} one
189+
* Rows.
190+
* @param {boolean} one
191+
* Whether the input was explicitly one file (not an array).
168192
* @param {Options} options
193+
* Configuration.
194+
* @returns {string}
195+
* Report.
169196
*/
170197
// eslint-disable-next-line complexity
171198
function format(map, one, options) {
@@ -298,10 +325,12 @@ function format(map, one, options) {
298325
}
299326

300327
/**
301-
* Get the length of `value`, ignoring ANSI sequences.
328+
* Get the length of the first line of `value`, ignoring ANSI sequences.
302329
*
303330
* @param {string} value
331+
* Message.
304332
* @returns {number}
333+
* Width.
305334
*/
306335
function size(value) {
307336
const match = /\r?\n|\r/.exec(value)

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"string-width": "^5.0.0",
4848
"supports-color": "^9.0.0",
4949
"unist-util-stringify-position": "^3.0.0",
50+
"vfile": "^5.0.0",
51+
"vfile-message": "^3.0.0",
5052
"vfile-sort": "^3.0.0",
5153
"vfile-statistics": "^2.0.0"
5254
},
@@ -61,7 +63,6 @@
6163
"tape": "^5.0.0",
6264
"type-coverage": "^2.0.0",
6365
"typescript": "^4.0.0",
64-
"vfile": "^5.0.0",
6566
"xo": "^0.53.0"
6667
},
6768
"scripts": {

0 commit comments

Comments
 (0)
Please sign in to comment.