From 5c3092d86f16680c9cfb3a1f8ce339359c69a82b Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sat, 3 Mar 2018 13:10:58 -0800 Subject: [PATCH 1/2] Expand `.raise()` to allow more options. --- packages/babylon/src/parser/location.js | 6 +++++- packages/babylon/src/parser/util.js | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/babylon/src/parser/location.js b/packages/babylon/src/parser/location.js index f3b0fb656d..24ffe0168c 100644 --- a/packages/babylon/src/parser/location.js +++ b/packages/babylon/src/parser/location.js @@ -13,7 +13,11 @@ export default class LocationParser extends CommentsParser { raise( pos: number, message: string, - missingPluginNames?: Array, + { + missingPluginNames, + }: { + missingPluginNames?: Array, + } = {}, ): empty { const loc = getLineInfo(this.input, pos); message += ` (${loc.line}:${loc.column})`; diff --git a/packages/babylon/src/parser/util.js b/packages/babylon/src/parser/util.js index c89e40bd8f..dd5a14a167 100644 --- a/packages/babylon/src/parser/util.js +++ b/packages/babylon/src/parser/util.js @@ -120,7 +120,7 @@ export default class UtilParser extends Tokenizer { throw this.raise( pos != null ? pos : this.state.start, `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( ", ", )}'`, - names, + { missingPluginNames: names }, ); } } From 5f6e3122a0e7bf0bdde0a5d03e20ea93b041a39b Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sat, 3 Mar 2018 13:21:38 -0800 Subject: [PATCH 2/2] Give users helpful feedback if they are detected as using the wrong sourceType. --- .../src/transformation/normalize-file.js | 6 ++++ .../src/index.js | 5 +++- .../fixtures/misc/module-exports/output.js | 28 +++++++++---------- packages/babylon/src/parser/expression.js | 1 + packages/babylon/src/parser/location.js | 5 ++++ packages/babylon/src/parser/statement.js | 3 ++ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index 2ff27aadaf..2ea1e034ed 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -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( diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.js b/packages/babel-plugin-transform-modules-commonjs/src/index.js index 6ed08a8abc..6edc09b96c 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.js @@ -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."); })() `; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/module-exports/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/module-exports/output.js index e1138b612d..3e9254c59b 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/module-exports/output.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/module-exports/output.js @@ -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 = ""; diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 73dc458d0e..1712baf40f 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -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"); diff --git a/packages/babylon/src/parser/location.js b/packages/babylon/src/parser/location.js index 24ffe0168c..79db584a49 100644 --- a/packages/babylon/src/parser/location.js +++ b/packages/babylon/src/parser/location.js @@ -15,8 +15,10 @@ export default class LocationParser extends CommentsParser { message: string, { missingPluginNames, + code, }: { missingPluginNames?: Array, + 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; } } diff --git a/packages/babylon/src/parser/statement.js b/packages/babylon/src/parser/statement.js index ab917a22db..d736b46184 100644 --- a/packages/babylon/src/parser/statement.js +++ b/packages/babylon/src/parser/statement.js @@ -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", + }, ); } }