From b9e40ab020353cc7bef5f6182ce414467a55c415 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Mon, 21 Jan 2019 11:06:29 -0500 Subject: [PATCH] Add requireConfigFile option (#743) * Add requireConfigFile option * Update README.md --- README.md | 1 + lib/analyze-scope.js | 2 +- lib/index.js | 3 +-- lib/parse.js | 26 ++++++++++++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ad8fc01d..25373821 100644 --- a/README.md +++ b/README.md @@ -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"`. diff --git a/lib/analyze-scope.js b/lib/analyze-scope.js index ed2dab65..38561521 100644 --- a/lib/analyze-scope.js +++ b/lib/analyze-scope.js @@ -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, }; diff --git a/lib/index.js b/lib/index.js index c9478419..459444cf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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"; diff --git a/lib/parse.js b/lib/parse.js index f6b55a14..c83fcb9c 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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) {