Skip to content

Commit

Permalink
Add support for causes with causes
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 16, 2023
1 parent c6f23be commit 40c1a1d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
24 changes: 16 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,28 @@ function createByline(state, stats) {
*/
function createCauseLines(state, cause) {
const lines = [' ' + state.bold + '[cause]' + state.normalIntensity + ':']
/** @type {string | undefined} */
let stackValue
let foundReasonableCause = false

if (cause !== null && typeof cause === 'object') {
stackValue =
const stackValue =
('stack' in cause ? String(cause.stack) : undefined) ||
('message' in cause ? String(cause.message) : undefined)

if (typeof stackValue === 'string') {
foundReasonableCause = true

const stackLines = stackValue.split(eol)
stackLines[0] = ' ' + stackLines[0]

lines.push(...stackLines)

if ('cause' in cause && cause.cause) {
lines.push(...createCauseLines(state, cause.cause))
}
}
}

if (typeof stackValue === 'string') {
const stackLines = stackValue.split(eol)
stackLines[0] = ' ' + stackLines[0]
lines.push(...stackLines)
} else {
if (!foundReasonableCause) {
lines.push(' ' + cause)
}

Expand Down
32 changes: 30 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ try {
}
/* eslint-enable no-undef */

// @ts-expect-error: it’s assigned.
const causedCause = new Error('Boom!', {cause: exception})
causedCause.stack = cleanStack(causedCause.stack, 3)

test('reporter', async function () {
const mod = await import('./index.js')

Expand Down Expand Up @@ -413,6 +417,30 @@ test('reporter', async function () {
'should support a `message.cause`'
)

file = new VFile()

file.message('Something failed terribly', {
cause: causedCause
})

assert.equal(
strip(reporter(file)),
[
' warning Something failed terribly',
' [cause]:',
' Error: Boom!',
' at test.js:1:1',
' at ModuleJob.run (module_job:1:1)',
' [cause]:',
' ReferenceError: variable is not defined',
' at test.js:1:1',
' at ModuleJob.run (module_job:1:1)',
'',
'⚠ 1 warning'
].join('\n'),
'should support a `message.cause`, w/ another cause'
)

file = new VFile()
let message = file.message('Something failed terribly')
message.cause = 'Boom!'
Expand Down Expand Up @@ -442,7 +470,7 @@ test('reporter', async function () {
'',
'⚠ 1 warning'
].join('\n'),
'should support a `message.cause` set to a n object w/o stack'
'should support a `message.cause` set to an object w/o stack'
)

file = new VFile()
Expand All @@ -458,7 +486,7 @@ test('reporter', async function () {
'',
'⚠ 1 warning'
].join('\n'),
'should support a `message.cause` set to a n object w/o stack or message'
'should support a `message.cause` set to an object w/o stack or message'
)

/** @type {Text} */
Expand Down

0 comments on commit 40c1a1d

Please sign in to comment.