Skip to content

Commit cf0b1e7

Browse files
Make error message prefixes more descriptive (#11612)
Co-authored-by: Nicolò Ribaudo <[email protected]> Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent 32328de commit cf0b1e7

File tree

7 files changed

+22
-18
lines changed

7 files changed

+22
-18
lines changed

packages/babel-core/src/config/full.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ function enhanceError<T extends Function>(context: ConfigContext, fn: T): T {
220220
// There are a few case where thrown errors will try to annotate themselves multiple times, so
221221
// to keep things simple we just bail out if re-wrapping the message.
222222
if (!/^\[BABEL\]/.test(e.message)) {
223-
e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
223+
e.message = `[BABEL] ${context.filename ?? "unknown file"}: ${
224+
e.message
225+
}`;
224226
}
225227

226228
throw e;

packages/babel-core/src/transformation/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function* run(
4646
try {
4747
yield* transformFile(file, config.passes);
4848
} catch (e) {
49-
e.message = `${opts.filename ?? "unknown"}: ${e.message}`;
49+
e.message = `${opts.filename ?? "unknown file"}: ${e.message}`;
5050
if (!e.code) {
5151
e.code = "BABEL_TRANSFORM_ERROR";
5252
}
@@ -59,7 +59,7 @@ export function* run(
5959
({ outputCode, outputMap } = generateCode(config.passes, file));
6060
}
6161
} catch (e) {
62-
e.message = `${opts.filename ?? "unknown"}: ${e.message}`;
62+
e.message = `${opts.filename ?? "unknown file"}: ${e.message}`;
6363
if (!e.code) {
6464
e.code = "BABEL_GENERATE_ERROR";
6565
}

packages/babel-core/test/__snapshots__/option-manager.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`option-manager config plugin/preset flattening and overriding should throw when an option is following a preset 1`] = `
4-
"[BABEL] unknown: Unknown option: .useSpread. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
4+
"[BABEL] unknown file: Unknown option: .useSpread. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
55
- Maybe you meant to use
66
\\"presets\\": [
77
[\\"./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
1212
`;
1313

1414
exports[`option-manager config plugin/preset flattening and overriding should throw when an option is provided as a plugin 1`] = `
15-
"[BABEL] unknown: .useSpread is not a valid Plugin property
15+
"[BABEL] unknown file: .useSpread is not a valid Plugin property
1616
- Maybe you meant to use
1717
\\"plugins\\": [
1818
[\\"./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
2323
`;
2424

2525
exports[`option-manager config plugin/preset flattening and overriding should throw when an option is provided as a preset 1`] = `
26-
"[BABEL] unknown: Unknown option: .useBuiltIns. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
26+
"[BABEL] unknown file: Unknown option: .useBuiltIns. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
2727
- Maybe you meant to use
2828
\\"presets\\": [
2929
[\\"./fixtures/option-manager/babel-preset-bar\\", {

packages/babel-core/test/api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,9 @@ describe("api", function () {
947947
},
948948
],
949949
}),
950-
).toThrowErrorMatchingInlineSnapshot(`"unknown: Unknown helper fooBar"`);
950+
).toThrowErrorMatchingInlineSnapshot(
951+
`"unknown file: Unknown helper fooBar"`,
952+
);
951953
});
952954
});
953955
});

packages/babel-core/test/async.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("asynchronicity", () => {
123123
process.chdir("plugin");
124124

125125
expect(() => babel.transformSync("")).toThrow(
126-
`[BABEL] unknown: You appear to be using an async plugin/preset, but Babel` +
126+
`[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel` +
127127
` has been called synchronously`,
128128
);
129129
});
@@ -144,7 +144,7 @@ describe("asynchronicity", () => {
144144
expect(() =>
145145
babel.transformSync(""),
146146
).toThrowErrorMatchingInlineSnapshot(
147-
`"unknown: You appear to be using an plugin with an async .pre, which your current version` +
147+
`"unknown file: You appear to be using an plugin with an async .pre, which your current version` +
148148
` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
149149
` @babel/core version."`,
150150
);
@@ -156,7 +156,7 @@ describe("asynchronicity", () => {
156156
await expect(
157157
babel.transformAsync(""),
158158
).rejects.toThrowErrorMatchingInlineSnapshot(
159-
`"unknown: You appear to be using an plugin with an async .pre, which your current version` +
159+
`"unknown file: You appear to be using an plugin with an async .pre, which your current version` +
160160
` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
161161
` @babel/core version."`,
162162
);
@@ -170,7 +170,7 @@ describe("asynchronicity", () => {
170170
expect(() =>
171171
babel.transformSync(""),
172172
).toThrowErrorMatchingInlineSnapshot(
173-
`"unknown: You appear to be using an plugin with an async .post, which your current version` +
173+
`"unknown file: You appear to be using an plugin with an async .post, which your current version` +
174174
` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
175175
` @babel/core version."`,
176176
);
@@ -182,7 +182,7 @@ describe("asynchronicity", () => {
182182
await expect(
183183
babel.transformAsync(""),
184184
).rejects.toThrowErrorMatchingInlineSnapshot(
185-
`"unknown: You appear to be using an plugin with an async .post, which your current version` +
185+
`"unknown file: You appear to be using an plugin with an async .post, which your current version` +
186186
` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
187187
` @babel/core version."`,
188188
);
@@ -194,7 +194,7 @@ describe("asynchronicity", () => {
194194
process.chdir("plugin-inherits");
195195

196196
expect(() => babel.transformSync("")).toThrow(
197-
`[BABEL] unknown: You appear to be using an async plugin/preset, but Babel has been` +
197+
`[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel has been` +
198198
` called synchronously`,
199199
);
200200
});
@@ -234,7 +234,7 @@ describe("asynchronicity", () => {
234234
process.chdir("preset");
235235

236236
expect(() => babel.transformSync("")).toThrow(
237-
`[BABEL] unknown: You appear to be using an async plugin/preset, ` +
237+
`[BABEL] unknown file: You appear to be using an async plugin/preset, ` +
238238
`but Babel has been called synchronously`,
239239
);
240240
});
@@ -253,7 +253,7 @@ describe("asynchronicity", () => {
253253
process.chdir("preset-plugin-promise");
254254

255255
expect(() => babel.transformSync("")).toThrow(
256-
`[BABEL] unknown: You appear to be using a promise as a plugin, which your` +
256+
`[BABEL] unknown file: You appear to be using a promise as a plugin, which your` +
257257
` current version of Babel does not support. If you're using a published` +
258258
` plugin, you may need to upgrade your @babel/core version. As an` +
259259
` alternative, you can prefix the promise with "await".`,
@@ -264,7 +264,7 @@ describe("asynchronicity", () => {
264264
process.chdir("preset-plugin-promise");
265265

266266
await expect(babel.transformAsync("")).rejects.toThrow(
267-
`[BABEL] unknown: You appear to be using a promise as a plugin, which your` +
267+
`[BABEL] unknown file: You appear to be using a promise as a plugin, which your` +
268268
` current version of Babel does not support. If you're using a published` +
269269
` plugin, you may need to upgrade your @babel/core version. As an` +
270270
` alternative, you can prefix the promise with "await".`,

packages/babel-core/test/errors-stacks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe("@babel/core errors", function () {
207207
root: fixture("use-exclude-in-preset"),
208208
});
209209
}).toMatchInlineSnapshot(`
210-
"Error: [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly,
210+
"Error: [BABEL] unknown file: Preset /* your preset */ requires a filename to be set when babel is called directly,
211211
\`\`\`
212212
babel.transformSync(code, { filename: 'file.ts', presets: [/* your preset */] });
213213
\`\`\`

packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ expect(() => {
1515
// hard to assert on ANSI escape codes
1616
highlightCode: false,
1717
});
18-
}).toThrow(/^unknown: someMsg\s+> 1 \| function f\(\) {}/);
18+
}).toThrow(/^unknown file: someMsg\s+> 1 \| function f\(\) {}/);

0 commit comments

Comments
 (0)