Skip to content

Commit

Permalink
Change to remove support for errors, nullish files
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 6, 2023
1 parent e0a164b commit d42d584
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
35 changes: 20 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,38 @@ const labels = {
}

/**
* Create a report from an error, one file, or multiple files.
* Create a report from one or more files.
*
* @param {Array<VFile> | Error | VFile | null | undefined} [files]
* Files or error to report (default: `undefined`).
* @param {Array<VFile> | VFile} files
* Files or error to report.
* @param {Options | null | undefined} [options]
* Configuration.
* @returns {string}
* Report.
*/
export function reporter(files, options) {
if (!files) {
return ''
const settings = options || {}
let one = false

if (
// Nothing.
!files ||
// Error.
('name' in files && 'message' in files)
) {
throw new TypeError(
'Unexpected value for `files`, expected one or more `VFile`s'
)
}

// Error.
if ('name' in files && 'message' in files) {
return String(files.stack || files)
}

const options_ = options || {}

// One file.
if (Array.isArray(files)) {
return format(transform(files, options_), false, options_)
// Empty.
} else {
one = true
files = [files]
}

return format(transform([files], options_), true, options_)
return format(transform(files, settings), one, settings)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ That value is also the `default` export.

### `reporter(files[, options])`

Create a report from an error, one file, or multiple files.
Create a report from one or more files.

###### Parameters

* `files` ([`Array<VFile>`][vfile], `VFile`, or `Error`, optional)
— files or error to report
* `files` ([`Array<VFile>`][vfile] or `VFile`)
— files to report
* `options` ([`Options`][api-options], optional)
— configuration

Expand Down
30 changes: 16 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,24 @@ test('reporter', async function () {
'should expose `reporter` as a named and a default export'
)

assert.equal(reporter(), '', 'should return empty without a file')

assert.equal(reporter([]), '', 'should return empty when not given files')

assert.equal(reporter(exception), exception.stack, 'should support an error')

let file = new VFile({path: 'a.js'})

try {
file.fail('Error!')
} catch {}
assert.throws(
function () {
// @ts-expect-error: Removed support for passing nullish, which used to be supported.
reporter()
},
/Unexpected value for `files`, expected one or more `VFile`s/,
'should display a runtime error when an error is passed'
)

assert.equal(
reporter(file.messages[0]),
'a.js:1:1: Error!',
'should support a fatal message'
assert.throws(
function () {
// @ts-expect-error: Removed support for passing an error, which used to be supported.
reporter(exception)
},
/Unexpected value for `files`, expected one or more `VFile`s/,
'should display a runtime error when an error is passed'
)

assert.equal(
Expand All @@ -93,7 +95,7 @@ test('reporter', async function () {
'should work on files without messages'
)

file = new VFile({path: 'a.js'})
let file = new VFile({path: 'a.js'})
file.message('Warning!')

assert.equal(
Expand Down

0 comments on commit d42d584

Please sign in to comment.