Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit b9e40ab

Browse files
authored
Add requireConfigFile option (#743)
* Add requireConfigFile option * Update README.md
1 parent 85ca8dc commit b9e40ab

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ With the parser set, your configuration can be configured as described in the [C
5353

5454
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.
5555

56+
- `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.
5657
- `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules.
5758
- `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.
5859
- `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`.

lib/analyze-scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ module.exports = function(ast, parserOptions) {
323323
parserOptions.ecmaFeatures.globalReturn) === true,
324324
impliedStrict: false,
325325
sourceType: ast.sourceType,
326-
ecmaVersion: parserOptions.ecmaVersion || 2018,
326+
ecmaVersion: parserOptions.ecmaVersion,
327327
fallback,
328328
};
329329

lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.parse = function(code, options) {
1616
return exports.parseForESLint(code, options).ast;
1717
};
1818

19-
exports.parseForESLint = function(code, options) {
19+
exports.parseForESLint = function(code, options = {}) {
2020
if (!IS_RUNNING_SUPPORTED_VERSION) {
2121
throw new Error(
2222
`babel-eslint@${
@@ -25,7 +25,6 @@ exports.parseForESLint = function(code, options) {
2525
);
2626
}
2727

28-
options = options || {};
2928
options.babelOptions = options.babelOptions || {};
3029
options.ecmaVersion = options.ecmaVersion || 2018;
3130
options.sourceType = options.sourceType || "module";

lib/parse.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"use strict";
22

33
const babylonToEspree = require("./babylon-to-espree");
4-
const { parseSync: parse, tokTypes: tt, traverse } = require("@babel/core");
4+
const {
5+
parseSync: parse,
6+
tokTypes: tt,
7+
traverse,
8+
loadPartialConfig,
9+
} = require("@babel/core");
510

611
module.exports = function(code, options) {
7-
const opts = {
12+
let opts = {
813
sourceType: options.sourceType,
914
filename: options.filePath,
1015
cwd: options.babelOptions.cwd,
@@ -35,7 +40,24 @@ module.exports = function(code, options) {
3540
},
3641
};
3742

43+
if (options.requireConfigFile !== false) {
44+
const config = loadPartialConfig(opts);
45+
46+
if (config !== null) {
47+
if (!config.hasFilesystemConfig()) {
48+
throw new Error(
49+
`No Babel config file detected for ${
50+
config.options.filename
51+
}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`
52+
);
53+
}
54+
55+
opts = config.options;
56+
}
57+
}
58+
3859
let ast;
60+
3961
try {
4062
ast = parse(code, opts);
4163
} catch (err) {

0 commit comments

Comments
 (0)