Skip to content

Commit

Permalink
Refactor some more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 22, 2023
1 parent 8f3b292 commit dae82ae
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 41 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions packages/esbuild/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @typedef {import('esbuild').Plugin} Plugin
* @typedef {import('esbuild').PluginBuild} PluginBuild
* @typedef {import('vfile').Value} Value
* @typedef {import('vfile-message').VFileMessage} VFileMessage
*/

/**
Expand Down Expand Up @@ -68,6 +67,7 @@ import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aw
import {extnamesToRegex} from '@mdx-js/mdx/internal-extnames-to-regex'
import {fetch} from 'undici'
import {VFile} from 'vfile'
import {VFileMessage} from 'vfile-message'

const eol = /\r\n|\r|\n|\u2028|\u2029/g

Expand Down Expand Up @@ -204,7 +204,7 @@ export function esbuild(options) {
let file = new VFile({path: data.path, value: doc})
/** @type {Value | undefined} */
let value
/** @type {Array<Error | VFileMessage>} */
/** @type {Array<VFileMessage>} */
let messages = []
/** @type {Array<Message>} */
const errors = []
Expand All @@ -216,13 +216,21 @@ export function esbuild(options) {
value = file.value
messages = file.messages
} catch (error_) {
const error = /** @type {Error | VFileMessage} */ (error_)
if ('fatal' in error) error.fatal = true
messages.push(error)
const cause = /** @type {VFileMessage | Error} */ (error_)
const message =
'reason' in cause
? cause
: new VFileMessage('Cannot process MDX file with esbuild', {
cause,
ruleId: 'process-error',
source: '@mdx-js/esbuild'
})
message.fatal = true
messages.push(message)
}

for (const message of messages) {
const list = !('fatal' in message) || message.fatal ? errors : warnings
const list = message.fatal ? errors : warnings
list.push(vfileMessageToEsbuild(state, message))
}

Expand All @@ -244,13 +252,13 @@ export function esbuild(options) {
/**
* @param {Readonly<State>} state
* Info passed around.
* @param {Readonly<Error | VFileMessage>} message
* @param {Readonly<VFileMessage>} message
* VFile message or error.
* @returns {Message}
* ESBuild message.
*/
function vfileMessageToEsbuild(state, message) {
const place = 'place' in message ? message.place : undefined
const place = message.place
const start = place ? ('start' in place ? place.start : place) : undefined
const end = place && 'end' in place ? place.end : undefined
let length = 0
Expand Down Expand Up @@ -288,11 +296,6 @@ function vfileMessageToEsbuild(state, message) {
},
notes: [],
pluginName: state.name,
text: String(
('reason' in message ? message.reason : undefined) ||
/* c8 ignore next 2 - errors should have stacks */
message.stack ||
message
)
text: message.reason
}
}
3 changes: 2 additions & 1 deletion packages/esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"@mdx-js/mdx": "^2.0.0",
"@types/unist": "^3.0.0",
"undici": "^5.0.0",
"vfile": "^6.0.0"
"vfile": "^6.0.0",
"vfile-message": "^4.0.0"
},
"peerDependencies": {
"esbuild": ">=0.14.0"
Expand Down
22 changes: 13 additions & 9 deletions packages/esbuild/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ test('@mdx-js/esbuild', async function (t) {
}
})

await t.test('should pass errors (2)', async function () {
await t.test('should pass errors', async function () {
const mdxUrl = new URL('esbuild.mdx', import.meta.url)
const jsUrl = new URL('esbuild.js', import.meta.url)

Expand All @@ -439,11 +439,7 @@ test('@mdx-js/esbuild', async function (t) {
entryPoints: [fileURLToPath(mdxUrl)],
outfile: fileURLToPath(jsUrl),
format: 'esm',
plugins: [
esbuildMdx({
rehypePlugins: [crash]
})
]
plugins: [esbuildMdx({rehypePlugins: [crash]})]
})
assert.fail()
} catch (error) {
Expand All @@ -457,7 +453,16 @@ test('@mdx-js/esbuild', async function (t) {
assert.deepEqual(result, {
errors: [
{
detail: {},
detail: {
cause: {},
fatal: true,
message: 'Cannot process MDX file with esbuild',
name: '1:1',
reason: 'Cannot process MDX file with esbuild',
ruleId: 'process-error',
source: '@mdx-js/esbuild'
},
id: '',
location: {
column: 0,
file: 'test/esbuild.mdx',
Expand All @@ -469,8 +474,7 @@ test('@mdx-js/esbuild', async function (t) {
},
notes: [],
pluginName: '@mdx-js/esbuild',
text: 'Error: Something went wrong',
id: ''
text: 'Cannot process MDX file with esbuild'
}
],
warnings: []
Expand Down
31 changes: 22 additions & 9 deletions packages/mdx/lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@ export function recmaDocument(options) {
if (child.type === 'ExportDefaultDeclaration') {
if (layout) {
file.fail(
'Cannot specify multiple layouts (previous: ' +
'Unexpected duplicate layout, expected a single layout (previous: ' +
stringifyPosition(positionFromEstree(layout)) +
')',
positionFromEstree(child),
'recma-document:duplicate-layout'
{
ancestors: [tree, child],
place: positionFromEstree(child),
ruleId: 'duplicate-layout',
source: 'recma-document'
}
)
}

Expand Down Expand Up @@ -169,11 +173,15 @@ export function recmaDocument(options) {
if (specifier.exported.name === 'default') {
if (layout) {
file.fail(
'Cannot specify multiple layouts (previous: ' +
'Unexpected duplicate layout, expected a single layout (previous: ' +
stringifyPosition(positionFromEstree(layout)) +
')',
positionFromEstree(child),
'recma-document:duplicate-layout'
{
ancestors: [tree, child, specifier],
place: positionFromEstree(child),
ruleId: 'duplicate-layout',
source: 'recma-document'
}
)
}

Expand Down Expand Up @@ -413,9 +421,14 @@ export function recmaDocument(options) {
) {
if (!useDynamicImport) {
file.fail(
'Cannot use `import` or `export … from` in `evaluate` (outputting a function body) by default: please set `useDynamicImport: true` (and probably specify a `baseUrl`)',
positionFromEstree(node),
'recma-document:invalid-esm-statement'
'Unexpected `import` or `export … from` in `evaluate` (outputting a function body) by default: please set `useDynamicImport: true` (and probably specify a `baseUrl`)',
{
// Results of this function end up in `tree` again.
ancestors: [tree, node],
place: positionFromEstree(node),
ruleId: 'invalid-esm-statement',
source: 'recma-document'
}
)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ test('@mdx-js/mdx: compile', async function (t) {
} catch (error) {
assert.match(
String(error),
/Cannot specify multiple layouts \(previous: 1:1-1:31\)/
/Unexpected duplicate layout, expected a single layout \(previous: 1:1-1:31\)/
)
}
})
Expand All @@ -618,7 +618,7 @@ test('@mdx-js/mdx: compile', async function (t) {
} catch (error) {
assert.match(
String(error),
/Cannot specify multiple layouts \(previous: 1:1-1:31\)/
/Unexpected duplicate layout, expected a single layout \(previous: 1:1-1:31\)/
)
}
})
Expand Down
10 changes: 5 additions & 5 deletions packages/mdx/test/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,14 @@ test('@mdx-js/mdx: evaluate', async function (t) {
)
})

await t.test('should throw on an export from', async function () {
await t.test('should throw on an `export * from`', async function () {
try {
await evaluate('export {a} from "b"', runtime)
assert.fail()
} catch (error) {
assert.match(
String(error),
/Cannot use `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
/Unexpected `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
)
}
})
Expand Down Expand Up @@ -379,7 +379,7 @@ test('@mdx-js/mdx: evaluate', async function (t) {
} catch (error) {
assert.match(
String(error),
/Cannot use `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
/Unexpected `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
)
}
})
Expand All @@ -391,7 +391,7 @@ test('@mdx-js/mdx: evaluate', async function (t) {
} catch (error) {
assert.match(
String(error),
/Cannot use `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
/Unexpected `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
)
}
})
Expand All @@ -403,7 +403,7 @@ test('@mdx-js/mdx: evaluate', async function (t) {
} catch (error) {
assert.match(
String(error),
/Cannot use `import` or `export … from` in `evaluate` \(outputting a function body\) by default/
/Unexpected `import` or `export … from` in `evaluate` \(outputting a function body\) by default:/
)
}
})
Expand Down

1 comment on commit dae82ae

@vercel
Copy link

@vercel vercel bot commented on dae82ae Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdx-git-main-mdx.vercel.app
mdxjs.com
v2.mdxjs.com

Please sign in to comment.