diff --git a/eslint/babel-eslint-parser/src/configuration.js b/eslint/babel-eslint-parser/src/configuration.js new file mode 100644 index 0000000000..e6d036372f --- /dev/null +++ b/eslint/babel-eslint-parser/src/configuration.js @@ -0,0 +1,61 @@ +import { loadPartialConfig } from "@babel/core"; + +export function normalizeESLintConfig(options) { + const defaultOptions = { + babelOptions: {}, + ecmaVersion: 2020, + sourceType: "module", + allowImportExportEverywhere: false, + }; + + return Object.assign(defaultOptions, options); +} + +export function normalizeBabelParseConfig(options) { + const parseOptions = { + sourceType: options.sourceType, + filename: options.filePath, + cwd: options.babelOptions.cwd, + root: options.babelOptions.root, + rootMode: options.babelOptions.rootMode, + envName: options.babelOptions.envName, + configFile: options.babelOptions.configFile, + babelrc: options.babelOptions.babelrc, + babelrcRoots: options.babelOptions.babelrcRoots, + extends: options.babelOptions.extends, + env: options.babelOptions.env, + overrides: options.babelOptions.overrides, + test: options.babelOptions.test, + include: options.babelOptions.include, + exclude: options.babelOptions.exclude, + ignore: options.babelOptions.ignore, + only: options.babelOptions.only, + parserOpts: { + allowImportExportEverywhere: options.allowImportExportEverywhere, + allowReturnOutsideFunction: true, + allowSuperOutsideMethod: true, + ranges: true, + tokens: true, + plugins: ["estree"], + }, + caller: { + name: "@babel/eslint-parser", + }, + }; + + if (options.requireConfigFile !== false) { + const config = loadPartialConfig(parseOptions); + + 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.`, + ); + } + + return config.options; + } + } + + return parseOptions; +} diff --git a/eslint/babel-eslint-parser/src/index.js b/eslint/babel-eslint-parser/src/index.js index cbb9f65abd..4709fd4aa8 100644 --- a/eslint/babel-eslint-parser/src/index.js +++ b/eslint/babel-eslint-parser/src/index.js @@ -1,6 +1,7 @@ import semver from "semver"; import { version as CURRENT_BABEL_VERSION } from "@babel/core"; import parseWithScope from "./parse-with-scope"; +import { normalizeESLintConfig } from "./configuration"; import packageJson from "../package.json"; const SUPPORTED_BABEL_VERSION_RANGE = @@ -21,11 +22,5 @@ export function parseForESLint(code, options = {}) { ); } - options.babelOptions = options.babelOptions || {}; - options.ecmaVersion = options.ecmaVersion || 2018; - options.sourceType = options.sourceType || "module"; - options.allowImportExportEverywhere = - options.allowImportExportEverywhere || false; - - return parseWithScope(code, options); + return parseWithScope(code, normalizeESLintConfig(options)); } diff --git a/eslint/babel-eslint-parser/src/parse.js b/eslint/babel-eslint-parser/src/parse.js index fc8a27abd1..e38e9a94fe 100644 --- a/eslint/babel-eslint-parser/src/parse.js +++ b/eslint/babel-eslint-parser/src/parse.js @@ -1,61 +1,13 @@ +import { parseSync as babelParse, tokTypes as tt, traverse } from "@babel/core"; import babylonToEspree from "./babylon-to-espree"; -import { - parseSync as parse, - tokTypes as tt, - traverse, - loadPartialConfig, -} from "@babel/core"; - -export default function(code, options) { - let opts = { - sourceType: options.sourceType, - filename: options.filePath, - cwd: options.babelOptions.cwd, - root: options.babelOptions.root, - rootMode: options.babelOptions.rootMode, - envName: options.babelOptions.envName, - configFile: options.babelOptions.configFile, - babelrc: options.babelOptions.babelrc, - babelrcRoots: options.babelOptions.babelrcRoots, - extends: options.babelOptions.extends, - env: options.babelOptions.env, - overrides: options.babelOptions.overrides, - test: options.babelOptions.test, - include: options.babelOptions.include, - exclude: options.babelOptions.exclude, - ignore: options.babelOptions.ignore, - only: options.babelOptions.only, - parserOpts: { - allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree - allowReturnOutsideFunction: true, - allowSuperOutsideMethod: true, - ranges: true, - tokens: true, - plugins: ["estree"], - }, - caller: { - name: "@babel/eslint-parser", - }, - }; - - 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; - } - } +import { normalizeBabelParseConfig } from "./configuration"; +export default function parse(code, options) { + const parseOptions = normalizeBabelParseConfig(options); let ast; try { - ast = parse(code, opts); + ast = babelParse(code, parseOptions); } catch (err) { if (err instanceof SyntaxError) { err.lineNumber = err.loc.line; diff --git a/eslint/babel-eslint-parser/test/non-regression.js b/eslint/babel-eslint-parser/test/non-regression.js index 63aadea4b3..dca0b489d9 100644 --- a/eslint/babel-eslint-parser/test/non-regression.js +++ b/eslint/babel-eslint-parser/test/non-regression.js @@ -1614,7 +1614,7 @@ describe("verify", () => { "var leakedGlobal = 1;", { "no-implicit-globals": 1 }, [], - null, + undefined, { env: {}, parserOptions: { ecmaVersion: 6 },