From 298c9a6c3304cdf48b0a57f6787d9956d9548e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Oct 2019 11:19:29 -0400 Subject: [PATCH] Add filename to transform error (#10511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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ò --- .../src/transformation/file/file.js | 2 +- .../src/transformation/file/generate.js | 2 +- .../babel-core/src/transformation/index.js | 26 ++++++++++++++++--- .../src/transformation/normalize-file.js | 3 ++- .../build-code-frame-error/options.json | 2 +- .../fixtures/plugins/transform-error/exec.js | 15 +++++++++++ .../plugins/transform-error/options.json | 4 +++ 7 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 packages/babel-core/test/fixtures/plugins/transform-error/exec.js create mode 100644 packages/babel-core/test/fixtures/plugins/transform-error/options.json diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index a0a9f65bcdf4..2adb6e775763 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -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 = { diff --git a/packages/babel-core/src/transformation/file/generate.js b/packages/babel-core/src/transformation/file/generate.js index 9aff0188405c..b07851fe9cba 100644 --- a/packages/babel-core/src/transformation/file/generate.js +++ b/packages/babel-core/src/transformation/file/generate.js @@ -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.`, diff --git a/packages/babel-core/src/transformation/index.js b/packages/babel-core/src/transformation/index.js index 2412dd9e4981..e2eccd894537 100644 --- a/packages/babel-core/src/transformation/index.js +++ b/packages/babel-core/src/transformation/index.js @@ -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, diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index 0bc88c8a0c1b..6326dd385e66 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -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.`, @@ -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; diff --git a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json index b24f27d786a8..96fe5df0c4e2 100644 --- a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json +++ b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json @@ -1,3 +1,3 @@ { - "throws": "undefined: someMsg\n> 1 | function f() {}" + "throws": "unknown: someMsg\n> 1 | function f() {}" } diff --git a/packages/babel-core/test/fixtures/plugins/transform-error/exec.js b/packages/babel-core/test/fixtures/plugins/transform-error/exec.js new file mode 100644 index 000000000000..a5d11f514dea --- /dev/null +++ b/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" +}); diff --git a/packages/babel-core/test/fixtures/plugins/transform-error/options.json b/packages/babel-core/test/fixtures/plugins/transform-error/options.json new file mode 100644 index 000000000000..056950478dcf --- /dev/null +++ b/packages/babel-core/test/fixtures/plugins/transform-error/options.json @@ -0,0 +1,4 @@ +{ + "throws": "/fake/path/example.js: someMsg", + "os": ["linux", "darwin"] +}