Merge pull request #7490 from loganfsmyth/sourcetype-helpful-errors
Give helpful errors if the wrong sourceType is detected
This commit is contained in:
commit
15a80f0df8
@ -89,6 +89,12 @@ function parser(pluginPasses, options, code) {
|
|||||||
}
|
}
|
||||||
throw new Error("More than one plugin attempted to override parsing.");
|
throw new Error("More than one plugin attempted to override parsing.");
|
||||||
} catch (err) {
|
} 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;
|
const { loc, missingPlugin } = err;
|
||||||
if (loc) {
|
if (loc) {
|
||||||
const codeFrame = codeFrameColumns(
|
const codeFrame = codeFrameColumns(
|
||||||
|
|||||||
@ -34,7 +34,10 @@ export default declare((api, options) => {
|
|||||||
|
|
||||||
const getAssertion = localName => template.expression.ast`
|
const getAssertion = localName => template.expression.ast`
|
||||||
(function(){
|
(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");
|
require("foo");
|
||||||
|
|
||||||
console.log(function () {
|
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 () {
|
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);
|
}().prop);
|
||||||
|
|
||||||
exports = function () {
|
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;
|
}() + 1;
|
||||||
|
|
||||||
exports = function () {
|
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;
|
}() + 4;
|
||||||
|
|
||||||
({
|
({
|
||||||
exports
|
exports
|
||||||
} = ({}, function () {
|
} = ({}, 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 () {
|
[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 = {};
|
exports = {};
|
||||||
(function () {
|
(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 = "";
|
})().prop = "";
|
||||||
console.log(function () {
|
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 () {
|
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);
|
}().exports);
|
||||||
|
|
||||||
module = function () {
|
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;
|
}() + 1;
|
||||||
|
|
||||||
module = function () {
|
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;
|
}() + 4;
|
||||||
|
|
||||||
({
|
({
|
||||||
module
|
module
|
||||||
} = ({}, function () {
|
} = ({}, 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 () {
|
[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 = {};
|
module = {};
|
||||||
(function () {
|
(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 = "";
|
})().prop = "";
|
||||||
|
|||||||
@ -949,6 +949,7 @@ export default class ExpressionParser extends LValParser {
|
|||||||
this.raise(
|
this.raise(
|
||||||
id.start,
|
id.start,
|
||||||
`import.meta may appear only with 'sourceType: "module"'`,
|
`import.meta may appear only with 'sourceType: "module"'`,
|
||||||
|
{ code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED" },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return this.parseMetaProperty(node, id, "meta");
|
return this.parseMetaProperty(node, id, "meta");
|
||||||
|
|||||||
@ -13,7 +13,13 @@ export default class LocationParser extends CommentsParser {
|
|||||||
raise(
|
raise(
|
||||||
pos: number,
|
pos: number,
|
||||||
message: string,
|
message: string,
|
||||||
|
{
|
||||||
|
missingPluginNames,
|
||||||
|
code,
|
||||||
|
}: {
|
||||||
missingPluginNames?: Array<string>,
|
missingPluginNames?: Array<string>,
|
||||||
|
code?: string,
|
||||||
|
} = {},
|
||||||
): empty {
|
): empty {
|
||||||
const loc = getLineInfo(this.input, pos);
|
const loc = getLineInfo(this.input, pos);
|
||||||
message += ` (${loc.line}:${loc.column})`;
|
message += ` (${loc.line}:${loc.column})`;
|
||||||
@ -26,6 +32,9 @@ export default class LocationParser extends CommentsParser {
|
|||||||
if (missingPluginNames) {
|
if (missingPluginNames) {
|
||||||
err.missingPlugin = missingPluginNames;
|
err.missingPlugin = missingPluginNames;
|
||||||
}
|
}
|
||||||
|
if (code !== undefined) {
|
||||||
|
err.code = code;
|
||||||
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -192,6 +192,9 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
this.raise(
|
this.raise(
|
||||||
node.start,
|
node.start,
|
||||||
`'import' and 'export' may appear only with 'sourceType: "module"'`,
|
`'import' and 'export' may appear only with 'sourceType: "module"'`,
|
||||||
|
{
|
||||||
|
code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED",
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -120,7 +120,7 @@ export default class UtilParser extends Tokenizer {
|
|||||||
throw this.raise(
|
throw this.raise(
|
||||||
pos != null ? pos : this.state.start,
|
pos != null ? pos : this.state.start,
|
||||||
`This experimental syntax requires enabling the parser plugin: '${name}'`,
|
`This experimental syntax requires enabling the parser plugin: '${name}'`,
|
||||||
[name],
|
{ missingPluginNames: [name] },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ export default class UtilParser extends Tokenizer {
|
|||||||
`This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(
|
`This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(
|
||||||
", ",
|
", ",
|
||||||
)}'`,
|
)}'`,
|
||||||
names,
|
{ missingPluginNames: names },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user