Add requireConfigFile option (babel/babel-eslint#743)
* Add requireConfigFile option * Update README.md
This commit is contained in:
parent
0581ce1559
commit
37cf65c6f8
@ -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.
|
||||
|
||||
- `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.
|
||||
- `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"`.
|
||||
|
||||
@ -323,7 +323,7 @@ module.exports = function(ast, parserOptions) {
|
||||
parserOptions.ecmaFeatures.globalReturn) === true,
|
||||
impliedStrict: false,
|
||||
sourceType: ast.sourceType,
|
||||
ecmaVersion: parserOptions.ecmaVersion || 2018,
|
||||
ecmaVersion: parserOptions.ecmaVersion,
|
||||
fallback,
|
||||
};
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ exports.parse = function(code, options) {
|
||||
return exports.parseForESLint(code, options).ast;
|
||||
};
|
||||
|
||||
exports.parseForESLint = function(code, options) {
|
||||
exports.parseForESLint = function(code, options = {}) {
|
||||
if (!IS_RUNNING_SUPPORTED_VERSION) {
|
||||
throw new Error(
|
||||
`babel-eslint@${
|
||||
@ -25,7 +25,6 @@ exports.parseForESLint = function(code, options) {
|
||||
);
|
||||
}
|
||||
|
||||
options = options || {};
|
||||
options.babelOptions = options.babelOptions || {};
|
||||
options.ecmaVersion = options.ecmaVersion || 2018;
|
||||
options.sourceType = options.sourceType || "module";
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
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) {
|
||||
const opts = {
|
||||
let opts = {
|
||||
sourceType: options.sourceType,
|
||||
filename: options.filePath,
|
||||
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;
|
||||
|
||||
try {
|
||||
ast = parse(code, opts);
|
||||
} catch (err) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user