From cf0b1e74a2bd2c3f631459869ae856d146f70020 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sat, 27 Aug 2022 12:03:13 +0200 Subject: [PATCH] Make error message prefixes more descriptive (#11612) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo Co-authored-by: Nicolò Ribaudo --- packages/babel-core/src/config/full.ts | 4 +++- .../babel-core/src/transformation/index.ts | 4 ++-- .../test/__snapshots__/option-manager.js.snap | 6 +++--- packages/babel-core/test/api.js | 4 +++- packages/babel-core/test/async.js | 18 +++++++++--------- packages/babel-core/test/errors-stacks.js | 2 +- .../plugins/build-code-frame-error/exec.js | 2 +- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/babel-core/src/config/full.ts b/packages/babel-core/src/config/full.ts index ebb41cf34991..e431fb72ebd7 100644 --- a/packages/babel-core/src/config/full.ts +++ b/packages/babel-core/src/config/full.ts @@ -220,7 +220,9 @@ function enhanceError(context: ConfigContext, fn: T): T { // There are a few case where thrown errors will try to annotate themselves multiple times, so // to keep things simple we just bail out if re-wrapping the message. if (!/^\[BABEL\]/.test(e.message)) { - e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`; + e.message = `[BABEL] ${context.filename ?? "unknown file"}: ${ + e.message + }`; } throw e; diff --git a/packages/babel-core/src/transformation/index.ts b/packages/babel-core/src/transformation/index.ts index 78d587c8e265..e5e64b10a925 100644 --- a/packages/babel-core/src/transformation/index.ts +++ b/packages/babel-core/src/transformation/index.ts @@ -46,7 +46,7 @@ export function* run( try { yield* transformFile(file, config.passes); } catch (e) { - e.message = `${opts.filename ?? "unknown"}: ${e.message}`; + e.message = `${opts.filename ?? "unknown file"}: ${e.message}`; if (!e.code) { e.code = "BABEL_TRANSFORM_ERROR"; } @@ -59,7 +59,7 @@ export function* run( ({ outputCode, outputMap } = generateCode(config.passes, file)); } } catch (e) { - e.message = `${opts.filename ?? "unknown"}: ${e.message}`; + e.message = `${opts.filename ?? "unknown file"}: ${e.message}`; if (!e.code) { e.code = "BABEL_GENERATE_ERROR"; } diff --git a/packages/babel-core/test/__snapshots__/option-manager.js.snap b/packages/babel-core/test/__snapshots__/option-manager.js.snap index 47335a8deb35..ccb041b41bf7 100644 --- a/packages/babel-core/test/__snapshots__/option-manager.js.snap +++ b/packages/babel-core/test/__snapshots__/option-manager.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`option-manager config plugin/preset flattening and overriding should throw when an option is following a preset 1`] = ` -"[BABEL] unknown: Unknown option: .useSpread. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options. +"[BABEL] unknown file: Unknown option: .useSpread. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options. - Maybe you meant to use \\"presets\\": [ [\\"./fixtures/option-manager/babel-preset-bar\\", { @@ -12,7 +12,7 @@ To be a valid preset, its name and options should be wrapped in a pair of bracke `; exports[`option-manager config plugin/preset flattening and overriding should throw when an option is provided as a plugin 1`] = ` -"[BABEL] unknown: .useSpread is not a valid Plugin property +"[BABEL] unknown file: .useSpread is not a valid Plugin property - Maybe you meant to use \\"plugins\\": [ [\\"./fixtures/option-manager/babel-plugin-foo\\", { @@ -23,7 +23,7 @@ To be a valid plugin, its name and options should be wrapped in a pair of bracke `; exports[`option-manager config plugin/preset flattening and overriding should throw when an option is provided as a preset 1`] = ` -"[BABEL] unknown: Unknown option: .useBuiltIns. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options. +"[BABEL] unknown file: Unknown option: .useBuiltIns. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options. - Maybe you meant to use \\"presets\\": [ [\\"./fixtures/option-manager/babel-preset-bar\\", { diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index d810caf65127..8528e39a204c 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -947,7 +947,9 @@ describe("api", function () { }, ], }), - ).toThrowErrorMatchingInlineSnapshot(`"unknown: Unknown helper fooBar"`); + ).toThrowErrorMatchingInlineSnapshot( + `"unknown file: Unknown helper fooBar"`, + ); }); }); }); diff --git a/packages/babel-core/test/async.js b/packages/babel-core/test/async.js index a7cb2856a27f..053741a21c78 100644 --- a/packages/babel-core/test/async.js +++ b/packages/babel-core/test/async.js @@ -123,7 +123,7 @@ describe("asynchronicity", () => { process.chdir("plugin"); expect(() => babel.transformSync("")).toThrow( - `[BABEL] unknown: You appear to be using an async plugin/preset, but Babel` + + `[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel` + ` has been called synchronously`, ); }); @@ -144,7 +144,7 @@ describe("asynchronicity", () => { expect(() => babel.transformSync(""), ).toThrowErrorMatchingInlineSnapshot( - `"unknown: You appear to be using an plugin with an async .pre, which your current version` + + `"unknown file: You appear to be using an plugin with an async .pre, 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."`, ); @@ -156,7 +156,7 @@ describe("asynchronicity", () => { await expect( babel.transformAsync(""), ).rejects.toThrowErrorMatchingInlineSnapshot( - `"unknown: You appear to be using an plugin with an async .pre, which your current version` + + `"unknown file: You appear to be using an plugin with an async .pre, 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."`, ); @@ -170,7 +170,7 @@ describe("asynchronicity", () => { expect(() => babel.transformSync(""), ).toThrowErrorMatchingInlineSnapshot( - `"unknown: You appear to be using an plugin with an async .post, which your current version` + + `"unknown file: You appear to be using an plugin with an async .post, 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."`, ); @@ -182,7 +182,7 @@ describe("asynchronicity", () => { await expect( babel.transformAsync(""), ).rejects.toThrowErrorMatchingInlineSnapshot( - `"unknown: You appear to be using an plugin with an async .post, which your current version` + + `"unknown file: You appear to be using an plugin with an async .post, 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."`, ); @@ -194,7 +194,7 @@ describe("asynchronicity", () => { process.chdir("plugin-inherits"); expect(() => babel.transformSync("")).toThrow( - `[BABEL] unknown: You appear to be using an async plugin/preset, but Babel has been` + + `[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel has been` + ` called synchronously`, ); }); @@ -234,7 +234,7 @@ describe("asynchronicity", () => { process.chdir("preset"); expect(() => babel.transformSync("")).toThrow( - `[BABEL] unknown: You appear to be using an async plugin/preset, ` + + `[BABEL] unknown file: You appear to be using an async plugin/preset, ` + `but Babel has been called synchronously`, ); }); @@ -253,7 +253,7 @@ describe("asynchronicity", () => { process.chdir("preset-plugin-promise"); expect(() => babel.transformSync("")).toThrow( - `[BABEL] unknown: You appear to be using a promise as a plugin, which your` + + `[BABEL] unknown file: You appear to be using a promise as a 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. As an` + ` alternative, you can prefix the promise with "await".`, @@ -264,7 +264,7 @@ describe("asynchronicity", () => { process.chdir("preset-plugin-promise"); await expect(babel.transformAsync("")).rejects.toThrow( - `[BABEL] unknown: You appear to be using a promise as a plugin, which your` + + `[BABEL] unknown file: You appear to be using a promise as a 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. As an` + ` alternative, you can prefix the promise with "await".`, diff --git a/packages/babel-core/test/errors-stacks.js b/packages/babel-core/test/errors-stacks.js index e044c3310ece..8aff2183e688 100644 --- a/packages/babel-core/test/errors-stacks.js +++ b/packages/babel-core/test/errors-stacks.js @@ -207,7 +207,7 @@ describe("@babel/core errors", function () { root: fixture("use-exclude-in-preset"), }); }).toMatchInlineSnapshot(` - "Error: [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly, + "Error: [BABEL] unknown file: Preset /* your preset */ requires a filename to be set when babel is called directly, \`\`\` babel.transformSync(code, { filename: 'file.ts', presets: [/* your preset */] }); \`\`\` diff --git a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js index 0b56fb9bfb43..428d4b56a0c5 100644 --- a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js +++ b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js @@ -15,4 +15,4 @@ expect(() => { // hard to assert on ANSI escape codes highlightCode: false, }); -}).toThrow(/^unknown: someMsg\s+> 1 \| function f\(\) {}/); +}).toThrow(/^unknown file: someMsg\s+> 1 \| function f\(\) {}/);