Skip to content

Commit 741939c

Browse files
Suggest using BABEL_SHOW_CONFIG_FOR for config problems (#12428)
Co-authored-by: Babel Bot <30521560+liuxingbaoyu@users.noreply.github.com>
1 parent d8dc333 commit 741939c

24 files changed

Lines changed: 265 additions & 34 deletions

File tree

‎packages/babel-core/src/parser/index.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export default function* parser(
6666
if (missingPlugin) {
6767
err.message =
6868
`${filename}: ` +
69-
generateMissingPluginMessage(missingPlugin[0], loc, codeFrame);
69+
generateMissingPluginMessage(
70+
missingPlugin[0],
71+
loc,
72+
codeFrame,
73+
filename,
74+
);
7075
} else {
7176
err.message = `${filename}: ${err.message}\n\n` + codeFrame;
7277
}

‎packages/babel-core/src/parser/util/missing-plugin-helper.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export default function generateMissingPluginMessage(
318318
column: number;
319319
},
320320
codeFrame: string,
321+
filename: string,
321322
): string {
322323
let helpMessage =
323324
`Support for the experimental syntax '${missingPluginName}' isn't currently enabled ` +
@@ -342,5 +343,17 @@ If you want to leave it as-is, add ${syntaxPluginInfo} to the 'plugins' section
342343
}
343344
}
344345
}
346+
347+
const msgFilename =
348+
filename === "unknown" ? "<name of the input file>" : filename;
349+
helpMessage += `
350+
351+
If you already added the plugin for this syntax to your config, it's possible that your config \
352+
isn't being loaded.
353+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded \
354+
configuration:
355+
\tnpx cross-env BABEL_SHOW_CONFIG_FOR=${msgFilename} <your build command>
356+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.
357+
`;
345358
return helpMessage;
346359
}

‎packages/babel-core/test/api.js‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -906,23 +906,29 @@ describe("api", function () {
906906
});
907907
});
908908

909-
it("both syntax and transform plugin available", function () {
910-
return new Promise(resolve => {
909+
it("both syntax and transform plugin available", async function () {
910+
const promise = new Promise((resolve, reject) => {
911911
transformFile(
912912
cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js",
913913
options,
914-
function (err) {
915-
expect(err.message).toMatch(
916-
"Support for the experimental syntax 'doExpressions' isn't currently enabled (1:2):",
917-
);
918-
expect(err.message).toMatch(
919-
"Add @babel/plugin-proposal-do-expressions (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-do-expressions) to the " +
920-
"'plugins' section of your Babel config to enable transformation.",
921-
);
922-
resolve();
923-
},
914+
(err, result) => (err ? reject(err) : resolve(result)),
924915
);
925916
});
917+
918+
await expect(promise).rejects.toThrow();
919+
920+
const err = await promise.catch(e => e);
921+
922+
expect(err.message).toMatch(
923+
"Support for the experimental syntax 'doExpressions' isn't currently enabled (1:2):",
924+
);
925+
expect(err.message).toMatch(
926+
"Add @babel/plugin-proposal-do-expressions (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-do-expressions) to the " +
927+
"'plugins' section of your Babel config to enable transformation.",
928+
);
929+
expect(err.message).toMatch(
930+
/npx cross-env BABEL_SHOW_CONFIG_FOR=(.*?)[\\/]parsing-errors[\\/]syntax-and-transform[\\/]file.js/,
931+
);
926932
});
927933
});
928934

‎packages/babel-helper-create-class-features-plugin/src/features.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export function enableFeature(file: File, feature: number, loose: boolean) {
9494
throw new Error(
9595
"'loose' mode configuration must be the same for @babel/plugin-transform-class-properties, " +
9696
"@babel/plugin-transform-private-methods and " +
97-
"@babel/plugin-transform-private-property-in-object (when they are enabled).",
97+
"@babel/plugin-transform-private-property-in-object (when they are enabled)." +
98+
"\n\n" +
99+
getBabelShowConfigForHint(file),
98100
);
99101
} else {
100102
resolvedLoose = loose;
@@ -118,13 +120,29 @@ export function enableFeature(file: File, feature: number, loose: boolean) {
118120
`and @babel/plugin-transform-private-property-in-object (when they are enabled): you can ` +
119121
`silence this warning by explicitly adding\n` +
120122
`\t["${name}", { "loose": ${resolvedLoose} }]\n` +
121-
`to the "plugins" section of your Babel config.`,
123+
`to the "plugins" section of your Babel config.` +
124+
"\n\n" +
125+
getBabelShowConfigForHint(file),
122126
);
123127
}
124128
}
125129
}
126130
}
127131

132+
function getBabelShowConfigForHint(file: File) {
133+
let { filename } = file.opts;
134+
if (!filename || filename === "unknown") {
135+
filename = "[name of the input file]";
136+
}
137+
return `\
138+
If you already set the same 'loose' mode for these plugins in your config, it's possible that they \
139+
are enabled multiple times with different options.
140+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded \
141+
configuration:
142+
\tnpx cross-env BABEL_SHOW_CONFIG_FOR=${filename} <your build command>
143+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.`;
144+
}
145+
128146
function hasFeature(file: File, feature: number) {
129147
return !!(file.get(featuresKey) & feature);
130148
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A {
2+
x = 2;
3+
4+
#foo() {}
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"BABEL_8_BREAKING": false,
3+
"os": ["win32"],
4+
"validateLogs": true,
5+
"presets": [
6+
[
7+
"env",
8+
{
9+
"targets": { "node": 10 },
10+
"shippedProposals": true
11+
}
12+
]
13+
],
14+
"plugins": [["transform-private-methods", { "loose": true }]]
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo");
2+
class A {
3+
constructor() {
4+
Object.defineProperty(this, _foo, {
5+
value: _foo2
6+
});
7+
this.x = 2;
8+
}
9+
}
10+
function _foo2() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Though the "loose" option was set to "false" in your @babel/preset-env config, it will not be used for @babel/plugin-transform-private-property-in-object since the "loose" mode option was set to "true" for @babel/plugin-transform-private-methods.
2+
The "loose" option must be the same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods and @babel/plugin-transform-private-property-in-object (when they are enabled): you can silence this warning by explicitly adding
3+
["@babel/plugin-transform-private-property-in-object", { "loose": true }]
4+
to the "plugins" section of your Babel config.
5+
6+
If you already set the same 'loose' mode for these plugins in your config, it's possible that they are enabled multiple times with different options.
7+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
8+
npx cross-env BABEL_SHOW_CONFIG_FOR=<CWD>/packages/babel-preset-env/test/fixtures/loose-class-features-precedence/methods-loose-preset-not-loose-win/input.js <your build command>
9+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.
10+
Though the "loose" option was set to "false" in your @babel/preset-env config, it will not be used for @babel/plugin-transform-class-properties since the "loose" mode option was set to "true" for @babel/plugin-transform-private-property-in-object.
11+
The "loose" option must be the same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods and @babel/plugin-transform-private-property-in-object (when they are enabled): you can silence this warning by explicitly adding
12+
["@babel/plugin-transform-class-properties", { "loose": true }]
13+
to the "plugins" section of your Babel config.
14+
15+
If you already set the same 'loose' mode for these plugins in your config, it's possible that they are enabled multiple times with different options.
16+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
17+
npx cross-env BABEL_SHOW_CONFIG_FOR=<CWD>/packages/babel-preset-env/test/fixtures/loose-class-features-precedence/methods-loose-preset-not-loose-win/input.js <your build command>
18+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"BABEL_8_BREAKING": false,
3+
"os": ["darwin", "linux"],
34
"validateLogs": true,
45
"presets": [
5-
["env", {
6-
"targets": { "node": 10 }
7-
}]
6+
[
7+
"env",
8+
{
9+
"targets": { "node": 10 }
10+
}
11+
]
812
],
9-
"plugins": [
10-
["transform-private-methods", { "loose": true }]
11-
]
13+
"plugins": [["transform-private-methods", { "loose": true }]]
1214
}

‎packages/babel-preset-env/test/fixtures/loose-class-features-precedence/methods-loose-preset-not-loose/stderr.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ Though the "loose" option was set to "false" in your @babel/preset-env config, i
22
The "loose" option must be the same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods and @babel/plugin-transform-private-property-in-object (when they are enabled): you can silence this warning by explicitly adding
33
["@babel/plugin-transform-private-property-in-object", { "loose": true }]
44
to the "plugins" section of your Babel config.
5+
6+
If you already set the same 'loose' mode for these plugins in your config, it's possible that they are enabled multiple times with different options.
7+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
8+
npx cross-env BABEL_SHOW_CONFIG_FOR=<CWD>/packages/babel-preset-env/test/fixtures/loose-class-features-precedence/methods-loose-preset-not-loose/input.js <your build command>
9+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.
510
Though the "loose" option was set to "false" in your @babel/preset-env config, it will not be used for @babel/plugin-transform-class-properties since the "loose" mode option was set to "true" for @babel/plugin-transform-private-property-in-object.
611
The "loose" option must be the same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods and @babel/plugin-transform-private-property-in-object (when they are enabled): you can silence this warning by explicitly adding
712
["@babel/plugin-transform-class-properties", { "loose": true }]
813
to the "plugins" section of your Babel config.
14+
15+
If you already set the same 'loose' mode for these plugins in your config, it's possible that they are enabled multiple times with different options.
16+
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
17+
npx cross-env BABEL_SHOW_CONFIG_FOR=<CWD>/packages/babel-preset-env/test/fixtures/loose-class-features-precedence/methods-loose-preset-not-loose/input.js <your build command>
18+
See https://babeljs.io/docs/configuration#print-effective-configs for more info.

0 commit comments

Comments
 (0)