Skip to content

Commit

Permalink
Fix edge cases with errors in esbuild integration
Browse files Browse the repository at this point in the history
Related to GH-68.
  • Loading branch information
wooorm committed Oct 17, 2021
1 parent 79ceb9a commit 051b49f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/integration/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function esbuild(options = {}) {
let file = new VFile({value: doc, path: data.path})
/** @type {VFileValue|undefined} */
let value
/** @type {VFileMessage[]} */
/** @type {(VFileMessage|Error)[]} */
let messages = []
/** @type {Message[]} */
const errors = []
Expand All @@ -134,20 +134,16 @@ export function esbuild(options = {}) {
file = await process(file)
value = file.value
messages = file.messages
} catch (error) {
const exception = /** @type {VFileMessage} */ (error)
exception.fatal = true
messages.push(exception)
} catch (error_) {
const error = /** @type {VFileMessage|Error} */ (error_)
if ('fatal' in error) error.fatal = true
messages.push(error)
}

for (const message of messages) {
/** @type {{start?: Point, end?: Point}} */
// Non-message errors stored on `vfile.messages`.
/* c8 ignore next */
const location = message.position || {}

const start = location.start
const end = location.end
const location = 'position' in message ? message.position : undefined
const start = location ? location.start : undefined
const end = location ? location.end : undefined
let length = 0
let lineStart = 0
let line = 0
Expand Down Expand Up @@ -179,9 +175,14 @@ export function esbuild(options = {}) {
const match = eol.exec(doc)
const lineEnd = match ? match.index : doc.length

;(message.fatal ? errors : warnings).push({
;(!('fatal' in message) || message.fatal ? errors : warnings).push({
pluginName: name,
text: message.reason,
text:
'reason' in message
? message.reason
: /* Extra fallback to make sure weird values are definitely strings */
/* c8 ignore next */
message.stack || String(message),
notes: [],
location: {
namespace: 'file',
Expand Down
58 changes: 58 additions & 0 deletions test/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,64 @@ test('xdm (esbuild)', async (t) => {

console.log('\nnote: the preceding errors and warnings are expected!\n')

await fs.writeFile(path.join(base, 'esbuild-plugin-crash.mdx'), '# hi')

try {
await esbuild.build({
entryPoints: [path.join(base, 'esbuild-plugin-crash.mdx')],
outfile: path.join(base, 'esbuild-plugin-crash.js'),
format: 'esm',
plugins: [
esbuildXdm({
rehypePlugins: [
function () {
return () => {
throw new Error('Something went wrong')
}
}
]
})
]
})
t.fail('esbuild should throw')
} catch (error) {
/** @type {BuildFailure} */
const result = JSON.parse(JSON.stringify(error))

for (const message of [...result.errors, ...result.warnings]) {
delete message.detail
message.text = message.text.split('\n')[0]
}

t.deepEqual(
result,
{
errors: [
{
location: {
column: 0,
file: 'test/context/esbuild-plugin-crash.mdx',
length: 0,
line: 0,
lineText: '# hi',
namespace: 'file',
suggestion: ''
},
notes: [],
pluginName: 'esbuild-xdm',
text: 'Error: Something went wrong'
}
],
warnings: []
},
'should pass errors'
)
}

await fs.unlink(path.join(base, 'esbuild-plugin-crash.mdx'))

console.log('\nnote: the preceding errors and warnings are expected!\n')

/** @type {(contents: string) => import('esbuild').Plugin} */
const inlinePlugin = (contents) => ({
name: 'inline plugin',
Expand Down

0 comments on commit 051b49f

Please sign in to comment.