Add requireConfigFile option (babel/babel-eslint#743)

* Add requireConfigFile option

* Update README.md
This commit is contained in:
Kai Cataldo 2019-01-21 11:06:29 -05:00
parent 0581ce1559
commit 37cf65c6f8
4 changed files with 27 additions and 5 deletions

View File

@ -53,6 +53,7 @@ With the parser set, your configuration can be configured as described in the [C
Additional configuration options can be set in your ESLint configuration under the `parserOptions` key. Please note that the `ecmaFeatures` config property may still be required for ESLint to work properly with features not in ECMAScript 5 by default. Additional configuration options can be set in your ESLint configuration under the `parserOptions` key. Please note that the `ecmaFeatures` config property may still be required for ESLint to work properly with features not in ECMAScript 5 by default.
- `requireConfigFile` (default `true`) can be set to `false` to allow babel-eslint to run on files that do not have a Babel configuration associated with them. This can be useful for linting files that are not transformed by Babel (such as tooling configuration files), though we recommend using the default parser via [glob-based configuration](https://eslint.org/docs/user-guide/configuring#configuration-based-on-glob-patterns). Note: babel-eslint will not parse any experimental syntax when no configuration file is found.
- `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules. - `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules.
- `allowImportExportEverywhere` (default `false`) can be set to `true` to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. Otherwise import and export declarations can only appear at a program's top level. - `allowImportExportEverywhere` (default `false`) can be set to `true` to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. Otherwise import and export declarations can only appear at a program's top level.
- `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`. - `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`.

View File

@ -323,7 +323,7 @@ module.exports = function(ast, parserOptions) {
parserOptions.ecmaFeatures.globalReturn) === true, parserOptions.ecmaFeatures.globalReturn) === true,
impliedStrict: false, impliedStrict: false,
sourceType: ast.sourceType, sourceType: ast.sourceType,
ecmaVersion: parserOptions.ecmaVersion || 2018, ecmaVersion: parserOptions.ecmaVersion,
fallback, fallback,
}; };

View File

@ -16,7 +16,7 @@ exports.parse = function(code, options) {
return exports.parseForESLint(code, options).ast; return exports.parseForESLint(code, options).ast;
}; };
exports.parseForESLint = function(code, options) { exports.parseForESLint = function(code, options = {}) {
if (!IS_RUNNING_SUPPORTED_VERSION) { if (!IS_RUNNING_SUPPORTED_VERSION) {
throw new Error( throw new Error(
`babel-eslint@${ `babel-eslint@${
@ -25,7 +25,6 @@ exports.parseForESLint = function(code, options) {
); );
} }
options = options || {};
options.babelOptions = options.babelOptions || {}; options.babelOptions = options.babelOptions || {};
options.ecmaVersion = options.ecmaVersion || 2018; options.ecmaVersion = options.ecmaVersion || 2018;
options.sourceType = options.sourceType || "module"; options.sourceType = options.sourceType || "module";

View File

@ -1,10 +1,15 @@
"use strict"; "use strict";
const babylonToEspree = require("./babylon-to-espree"); const babylonToEspree = require("./babylon-to-espree");
const { parseSync: parse, tokTypes: tt, traverse } = require("@babel/core"); const {
parseSync: parse,
tokTypes: tt,
traverse,
loadPartialConfig,
} = require("@babel/core");
module.exports = function(code, options) { module.exports = function(code, options) {
const opts = { let opts = {
sourceType: options.sourceType, sourceType: options.sourceType,
filename: options.filePath, filename: options.filePath,
cwd: options.babelOptions.cwd, cwd: options.babelOptions.cwd,
@ -35,7 +40,24 @@ module.exports = function(code, options) {
}, },
}; };
if (options.requireConfigFile !== false) {
const config = loadPartialConfig(opts);
if (config !== null) {
if (!config.hasFilesystemConfig()) {
throw new Error(
`No Babel config file detected for ${
config.options.filename
}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`
);
}
opts = config.options;
}
}
let ast; let ast;
try { try {
ast = parse(code, opts); ast = parse(code, opts);
} catch (err) { } catch (err) {