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

Add requireConfigFile option #743

Merged
merged 2 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some examples of using overrides in the ESLint config to target babel-eslint for specific files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea!

- `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"`.
Expand Down
2 changes: 1 addition & 1 deletion lib/analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback,
};

Expand Down
3 changes: 1 addition & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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@${
Expand All @@ -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";
Expand Down
26 changes: 24 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but do we want to throw a helpful error if this ends up returning null? Otherwise I assume it will throw if someone tries to run ESLint on a file that is in the user's .babelrc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about this: It looks like babelCore#parseSync returns null when there isn't a config, but since we have a default configuration we pass through if a file isn't found with requireConfig: false and it warns when a config isn't found with requireConfig: true, I'm not seeing how babel-eslint can get into this state.

} catch (err) {
Expand Down