Add filename to transform error (#10511)

* polish: use “unknown” as a default filename in buildCodeFrameError

* feat: add filename to transform error

* fix: incorrect warning message

* fix: add filename to generate phrase error message

* address review comment from Nicolò
This commit is contained in:
Huáng Jùnliàng 2019-10-01 11:19:29 -04:00 committed by Nicolò Ribaudo
parent 80d99b4d4e
commit 298c9a6c33
7 changed files with 46 additions and 8 deletions

View File

@ -259,7 +259,7 @@ export default class File {
): Error { ): Error {
let loc = node && (node.loc || node._loc); let loc = node && (node.loc || node._loc);
msg = `${this.opts.filename}: ${msg}`; msg = `${this.opts.filename ?? "unknown"}: ${msg}`;
if (!loc && node) { if (!loc && node) {
const state = { const state = {

View File

@ -41,7 +41,7 @@ export default function generateCode(
if (typeof result.then === "function") { if (typeof result.then === "function") {
throw new Error( 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. ` + `which your current version of Babel does not support. ` +
`If you're using a published plugin, ` + `If you're using a published plugin, ` +
`you may need to upgrade your @babel/core version.`, `you may need to upgrade your @babel/core version.`,

View File

@ -55,11 +55,29 @@ export function runSync(
ast, ast,
); );
transformFile(file, config.passes);
const opts = file.opts; const opts = file.opts;
const { outputCode, outputMap } = try {
opts.code !== false ? generateCode(config.passes, file) : {}; 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 { return {
metadata: file.metadata, metadata: file.metadata,

View File

@ -107,7 +107,7 @@ function parser(
} else if (results.length === 1) { } else if (results.length === 1) {
if (typeof results[0].then === "function") { if (typeof results[0].then === "function") {
throw new Error( 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. ` + `which your current version of Babel does not support. ` +
`If you're using a published plugin, you may need to upgrade ` + `If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`, `your @babel/core version.`,
@ -121,6 +121,7 @@ function parser(
err.message += err.message +=
"\nConsider renaming the file to '.mjs', or setting sourceType:module " + "\nConsider renaming the file to '.mjs', or setting sourceType:module " +
"or sourceType:unambiguous in your Babel config for this file."; "or sourceType:unambiguous in your Babel config for this file.";
// err.code will be changed to BABEL_PARSE_ERROR later.
} }
const { loc, missingPlugin } = err; const { loc, missingPlugin } = err;

View File

@ -1,3 +1,3 @@
{ {
"throws": "undefined: someMsg\n> 1 | function f() {}" "throws": "unknown: someMsg\n> 1 | function f() {}"
} }

View File

@ -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"
});

View File

@ -0,0 +1,4 @@
{
"throws": "/fake/path/example.js: someMsg",
"os": ["linux", "darwin"]
}