Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename to transform error #10511

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/file.js
Expand Up @@ -259,7 +259,7 @@ export default class File {
): Error {
let loc = node && (node.loc || node._loc);

msg = `${this.opts.filename}: ${msg}`;
msg = `${this.opts.filename ?? "unknown"}: ${msg}`;

if (!loc && node) {
const state = {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/generate.js
Expand Up @@ -41,7 +41,7 @@ export default function generateCode(

if (typeof result.then === "function") {
throw new Error(
`You appear to be using an async parser plugin, ` +
`You appear to be using an async codegen plugin, ` +
`which your current version of Babel does not support. ` +
`If you're using a published plugin, ` +
`you may need to upgrade your @babel/core version.`,
Expand Down
26 changes: 22 additions & 4 deletions packages/babel-core/src/transformation/index.js
Expand Up @@ -55,11 +55,29 @@ export function runSync(
ast,
);

transformFile(file, config.passes);

const opts = file.opts;
const { outputCode, outputMap } =
opts.code !== false ? generateCode(config.passes, file) : {};
try {
transformFile(file, config.passes);
} catch (e) {
e.message = `${opts.filename ?? "unknown"}: ${e.message}`;
if (!e.code) {
e.code = "BABEL_TRANSFORM_ERROR";
}
throw e;
}

let outputCode, outputMap;
try {
if (opts.code !== false) {
({ outputCode, outputMap } = generateCode(config.passes, file));
}
} catch (e) {
e.message = `${opts.filename ?? "unknown"}: ${e.message}`;
if (!e.code) {
e.code = "BABEL_GENERATE_ERROR";
}
throw e;
}

return {
metadata: file.metadata,
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-core/src/transformation/normalize-file.js
Expand Up @@ -107,7 +107,7 @@ function parser(
} else if (results.length === 1) {
if (typeof results[0].then === "function") {
throw new Error(
`You appear to be using an async codegen plugin, ` +
`You appear to be using an async parser plugin, ` +
`which your current version of Babel does not support. ` +
`If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`,
Expand All @@ -121,6 +121,7 @@ function parser(
err.message +=
"\nConsider renaming the file to '.mjs', or setting sourceType:module " +
"or sourceType:unambiguous in your Babel config for this file.";
// err.code will be changed to BABEL_PARSE_ERROR later.
}

const { loc, missingPlugin } = err;
Expand Down
@@ -1,3 +1,3 @@
{
"throws": "undefined: someMsg\n> 1 | function f() {}"
"throws": "unknown: someMsg\n> 1 | function f() {}"
}
15 changes: 15 additions & 0 deletions packages/babel-core/test/fixtures/plugins/transform-error/exec.js
@@ -0,0 +1,15 @@
var code = "function f() {}";
transform(code, {
plugins: [
function() {
return {
visitor: {
FunctionDeclaration: function(path) {
throw new Error("someMsg");
},
},
};
},
],
filename: "/fake/path/example.js"
});
@@ -0,0 +1,4 @@
{
"throws": "/fake/path/example.js: someMsg",
"os": ["linux", "darwin"]
}