Skip to content

Commit 84ec66e

Browse files
authored
Refactor to improve error conversion in esbuild
Closes GH-2595. Reviewed-by: Titus Wormer <[email protected]>
1 parent f0d20da commit 84ec66e

File tree

2 files changed

+62
-48
lines changed

2 files changed

+62
-48
lines changed

packages/esbuild/lib/index.js

+51-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @import {CompileOptions} from '@mdx-js/mdx'
33
* @import {
4+
Location,
45
Message,
56
OnLoadArgs,
67
OnLoadResult,
@@ -117,14 +118,15 @@ export function esbuild(options) {
117118
messages = file.messages
118119
} catch (error_) {
119120
const cause = /** @type {VFileMessage | Error} */ (error_)
120-
const message =
121-
'reason' in cause
122-
? cause
123-
: new VFileMessage('Cannot process MDX file with esbuild', {
124-
cause,
125-
ruleId: 'process-error',
126-
source: '@mdx-js/esbuild'
127-
})
121+
const message = new VFileMessage(
122+
'Cannot process MDX file with esbuild',
123+
{
124+
cause,
125+
place: 'reason' in cause ? cause.place : undefined,
126+
ruleId: 'process-error',
127+
source: '@mdx-js/esbuild'
128+
}
129+
)
128130
message.fatal = true
129131
messages.push(message)
130132
}
@@ -157,44 +159,57 @@ export function esbuild(options) {
157159
* ESBuild message.
158160
*/
159161
function vfileMessageToEsbuild(state, message) {
162+
/** @type {Location} */
163+
const location = {
164+
column: 0,
165+
file: state.path,
166+
length: 0,
167+
line: 0,
168+
lineText: '',
169+
namespace: 'file',
170+
suggestion: ''
171+
}
172+
160173
const place = message.place
161174
const start = place ? ('start' in place ? place.start : place) : undefined
162-
const end = place && 'end' in place ? place.end : undefined
163-
let length = 0
164-
let lineStart = 0
165-
let line = 0
166-
let column = 0
167-
168-
if (start && start.offset !== undefined) {
169-
line = start.line
170-
column = start.column - 1
171-
lineStart = start.offset - column
172-
length = 1
173-
174-
if (end && end.offset !== undefined) {
175-
length = end.offset - start.offset
175+
if (start) {
176+
location.column = start.column - 1
177+
location.line = start.line
178+
location.length = 1
179+
180+
const end = place && 'end' in place ? place.end : undefined
181+
if (end) {
182+
if (start.offset !== undefined && end.offset !== undefined) {
183+
location.length = end.offset - start.offset
184+
} else if (end.line === start.line) {
185+
location.length = end.column - start.column
186+
}
176187
}
177-
}
178188

179-
eol.lastIndex = lineStart
189+
if (start.offset !== undefined) {
190+
eol.lastIndex = start.offset
191+
const match = eol.exec(state.doc)
192+
const lineStart = start.offset - (start.column - 1)
193+
const lineEnd = match ? match.index : state.doc.length
194+
location.lineText = state.doc.slice(lineStart, lineEnd)
195+
location.length = Math.min(location.length, lineEnd - (start.offset || 0))
196+
}
197+
198+
const maxLength = state.doc.length - (start.offset || 0)
199+
location.length = Math.min(location.length, maxLength)
200+
}
180201

181-
const match = eol.exec(state.doc)
182-
const lineEnd = match ? match.index : state.doc.length
202+
let text = message.reason
203+
if (message.cause) {
204+
text = `${text}:\n ${message.cause}`
205+
}
183206

184207
return {
185208
detail: message,
186209
id: '',
187-
location: {
188-
column,
189-
file: state.path,
190-
length: Math.min(length, lineEnd),
191-
line,
192-
lineText: state.doc.slice(lineStart, lineEnd),
193-
namespace: 'file',
194-
suggestion: ''
195-
},
210+
location,
196211
notes: [],
197212
pluginName: state.name,
198-
text: message.reason
213+
text
199214
}
200215
}

packages/esbuild/test/index.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ test('@mdx-js/esbuild', async function (t) {
231231
},
232232
notes: [],
233233
pluginName: '@mdx-js/esbuild',
234-
text: 'Unexpected character `/` (U+002F) before local name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a link in MDX, use `[text](url)`)'
234+
text: 'Cannot process MDX file with esbuild:\n 1:12: Unexpected character `/` (U+002F) before local name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a link in MDX, use `[text](url)`)'
235235
})
236236
}
237237

@@ -271,7 +271,7 @@ test('@mdx-js/esbuild', async function (t) {
271271
location: {
272272
column: 20,
273273
file: 'test/esbuild.mdx',
274-
length: 1,
274+
length: 0,
275275
line: 3,
276276
lineText: '# Hello, <Message />',
277277
namespace: 'file',
@@ -290,7 +290,7 @@ test('@mdx-js/esbuild', async function (t) {
290290
file: 'test/esbuild.mdx',
291291
length: 0,
292292
line: 0,
293-
lineText: 'export function Message() { return <>World!</> }',
293+
lineText: '',
294294
namespace: 'file',
295295
suggestion: ''
296296
},
@@ -305,7 +305,7 @@ test('@mdx-js/esbuild', async function (t) {
305305
file: 'test/esbuild.mdx',
306306
length: 0,
307307
line: 0,
308-
lineText: 'export function Message() { return <>World!</> }',
308+
lineText: '',
309309
namespace: 'file',
310310
suggestion: ''
311311
},
@@ -365,7 +365,7 @@ test('@mdx-js/esbuild', async function (t) {
365365
file: 'test/esbuild.mdx',
366366
length: 11,
367367
line: 3,
368-
lineText: '# Hello, <Message />',
368+
lineText: '',
369369
namespace: 'file',
370370
suggestion: ''
371371
},
@@ -414,7 +414,10 @@ test('@mdx-js/esbuild', async function (t) {
414414
file.message('3', tree)
415415
file.message('4', esm)
416416
file.message('5', text)
417-
file.message('6', jsx)
417+
const m6 = file.message('6', jsx)
418+
assert(m6.place)
419+
assert('start' in m6.place)
420+
delete m6.place.start.offset
418421
file.message('7', head.position.end).fatal = true // End of heading
419422
}
420423
}
@@ -439,10 +442,6 @@ test('@mdx-js/esbuild', async function (t) {
439442
/** @type {BuildFailure} */
440443
const result = JSON.parse(JSON.stringify(error))
441444

442-
for (const message of [...result.errors, ...result.warnings]) {
443-
message.text = message.text.split('\n')[0]
444-
}
445-
446445
assert.deepEqual(result, {
447446
errors: [
448447
{
@@ -461,13 +460,13 @@ test('@mdx-js/esbuild', async function (t) {
461460
file: 'test/esbuild.mdx',
462461
length: 0,
463462
line: 0,
464-
lineText: '# hi',
463+
lineText: '',
465464
namespace: 'file',
466465
suggestion: ''
467466
},
468467
notes: [],
469468
pluginName: '@mdx-js/esbuild',
470-
text: 'Cannot process MDX file with esbuild'
469+
text: 'Cannot process MDX file with esbuild:\n Error: Something went wrong'
471470
}
472471
],
473472
warnings: []

0 commit comments

Comments
 (0)