Give users helpful feedback if they are detected as using the wrong sourceType.
This commit is contained in:
parent
5c3092d86f
commit
5f6e3122a0
@ -89,6 +89,12 @@ function parser(pluginPasses, options, code) {
|
||||
}
|
||||
throw new Error("More than one plugin attempted to override parsing.");
|
||||
} catch (err) {
|
||||
if (err.code === "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED") {
|
||||
err.message +=
|
||||
"\nConsider renaming the file to '.mjs', or setting sourceType:module " +
|
||||
"or sourceType:unambiguous in your Babel config for this file.";
|
||||
}
|
||||
|
||||
const { loc, missingPlugin } = err;
|
||||
if (loc) {
|
||||
const codeFrame = codeFrameColumns(
|
||||
|
||||
@ -31,7 +31,10 @@ export default function(api, options) {
|
||||
|
||||
const getAssertion = localName => template.expression.ast`
|
||||
(function(){
|
||||
throw new Error("The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules.");
|
||||
throw new Error(
|
||||
"The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
|
||||
"Consider setting setting sourceType:script or sourceType:unambiguous in your " +
|
||||
"Babel config for this file.");
|
||||
})()
|
||||
`;
|
||||
|
||||
|
||||
@ -3,56 +3,56 @@
|
||||
require("foo");
|
||||
|
||||
console.log(function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}());
|
||||
console.log(function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}().prop);
|
||||
|
||||
exports = function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}() + 1;
|
||||
|
||||
exports = function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}() + 4;
|
||||
|
||||
({
|
||||
exports
|
||||
} = ({}, function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}()));
|
||||
[exports] = ([], function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}());
|
||||
exports = {};
|
||||
(function () {
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
})().prop = "";
|
||||
console.log(function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}());
|
||||
console.log(function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}().exports);
|
||||
|
||||
module = function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}() + 1;
|
||||
|
||||
module = function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}() + 4;
|
||||
|
||||
({
|
||||
module
|
||||
} = ({}, function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}()));
|
||||
[module] = ([], function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
}());
|
||||
module = {};
|
||||
(function () {
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules.");
|
||||
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
|
||||
})().prop = "";
|
||||
|
||||
@ -949,6 +949,7 @@ export default class ExpressionParser extends LValParser {
|
||||
this.raise(
|
||||
id.start,
|
||||
`import.meta may appear only with 'sourceType: "module"'`,
|
||||
{ code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED" },
|
||||
);
|
||||
}
|
||||
return this.parseMetaProperty(node, id, "meta");
|
||||
|
||||
@ -15,8 +15,10 @@ export default class LocationParser extends CommentsParser {
|
||||
message: string,
|
||||
{
|
||||
missingPluginNames,
|
||||
code,
|
||||
}: {
|
||||
missingPluginNames?: Array<string>,
|
||||
code?: string,
|
||||
} = {},
|
||||
): empty {
|
||||
const loc = getLineInfo(this.input, pos);
|
||||
@ -30,6 +32,9 @@ export default class LocationParser extends CommentsParser {
|
||||
if (missingPluginNames) {
|
||||
err.missingPlugin = missingPluginNames;
|
||||
}
|
||||
if (code !== undefined) {
|
||||
err.code = code;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,6 +192,9 @@ export default class StatementParser extends ExpressionParser {
|
||||
this.raise(
|
||||
node.start,
|
||||
`'import' and 'export' may appear only with 'sourceType: "module"'`,
|
||||
{
|
||||
code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED",
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user