Skip to content

Commit

Permalink
Make error message prefixes more descriptive
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
eps1lon and nicolo-ribaudo committed Aug 27, 2022
1 parent 32328de commit 7b28457
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/babel-core/src/config/full.ts
Expand Up @@ -220,7 +220,9 @@ function enhanceError<T extends Function>(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;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/transformation/index.ts
Expand Up @@ -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";
}
Expand All @@ -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";
}
Expand Down
6 changes: 3 additions & 3 deletions 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\\", {
Expand All @@ -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\\", {
Expand All @@ -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\\", {
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-core/test/api.js
Expand Up @@ -947,7 +947,9 @@ describe("api", function () {
},
],
}),
).toThrowErrorMatchingInlineSnapshot(`"unknown: Unknown helper fooBar"`);
).toThrowErrorMatchingInlineSnapshot(
`"unknown file: Unknown helper fooBar"`,
);
});
});
});
18 changes: 9 additions & 9 deletions packages/babel-core/test/async.js
Expand Up @@ -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`,
);
});
Expand All @@ -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."`,
);
Expand All @@ -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."`,
);
Expand All @@ -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."`,
);
Expand All @@ -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."`,
);
Expand All @@ -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`,
);
});
Expand Down Expand Up @@ -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`,
);
});
Expand All @@ -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".`,
Expand All @@ -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".`,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/test/errors-stacks.js
Expand Up @@ -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 */] });
\`\`\`
Expand Down
Expand Up @@ -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\(\) {}/);

0 comments on commit 7b28457

Please sign in to comment.