Skip to content

Commit 1a23c67

Browse files
committed
Fix to show vfile message causes nicely
1 parent 1e8c62f commit 1a23c67

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

lib/index.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ function createByline(state, stats) {
236236
* Info passed around.
237237
* @param {unknown} cause
238238
* Cause.
239-
* @returns {Array<string>}
239+
* @returns {Array<Array<string> | string>}
240240
* Lines.
241241
*/
242242
function createCauseLines(state, cause) {
243+
/** @type {Array<Array<string> | string>} */
243244
const lines = [' ' + state.bold + '[cause]' + state.normalIntensity + ':']
244245
let foundReasonableCause = false
245246

@@ -250,15 +251,34 @@ function createCauseLines(state, cause) {
250251

251252
if (typeof stackValue === 'string') {
252253
foundReasonableCause = true
254+
/** @type {Array<Array<string> | string>} */
255+
let causeLines
256+
257+
// Looks like a message.
258+
if ('file' in cause && 'fatal' in cause) {
259+
causeLines = createMessageLine(
260+
state,
261+
/** @type {VFileMessage} */ (cause)
262+
)
263+
}
264+
// Regular error.
265+
else {
266+
causeLines = stackValue.split(eol)
253267

254-
const stackLines = stackValue.split(eol)
255-
stackLines[0] = ' ' + stackLines[0]
256-
257-
lines.push(...stackLines)
268+
// Recurse.
269+
if ('cause' in cause && cause.cause) {
270+
causeLines.push(...createCauseLines(state, cause.cause))
271+
}
272+
}
258273

259-
if ('cause' in cause && cause.cause) {
260-
lines.push(...createCauseLines(state, cause.cause))
274+
const head = causeLines[0]
275+
if (typeof head === 'string') {
276+
causeLines[0] = ' ' + head
277+
} else {
278+
head[0] = ' ' + head[0]
261279
}
280+
281+
lines.push(...causeLines)
262282
}
263283
}
264284

@@ -345,7 +365,7 @@ function createMessageLine(state, message) {
345365
let reason = message.stack || message.message
346366

347367
const match = eol.exec(reason)
348-
/** @type {Array<string>} */
368+
/** @type {Array<Array<string> | string>} */
349369
let rest = []
350370

351371
if (match) {

test.js

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import assert from 'node:assert/strict'
1010
import test from 'node:test'
1111
import strip from 'strip-ansi'
1212
import {VFile} from 'vfile'
13+
import {VFileMessage} from 'vfile-message'
1314
import {reporter} from 'vfile-reporter'
1415

1516
/* eslint-disable no-undef */
@@ -441,6 +442,24 @@ test('reporter', async function () {
441442
'should support a `message.cause`, w/ another cause'
442443
)
443444

445+
file = new VFile()
446+
447+
file.message('Something failed terribly', {
448+
cause: new VFileMessage('Boom!', {ruleId: 'foo', source: 'bar'})
449+
})
450+
451+
assert.equal(
452+
strip(reporter(file)),
453+
[
454+
' warning Something failed terribly',
455+
' [cause]:',
456+
' info Boom! foo bar',
457+
'',
458+
'⚠ 1 warning'
459+
].join('\n'),
460+
'should support a `message.cause` w/ a message'
461+
)
462+
444463
file = new VFile()
445464
let message = file.message('Something failed terribly')
446465
message.cause = 'Boom!'

0 commit comments

Comments
 (0)