diff --git a/eslint/babel-eslint-parser/src/configuration.js b/eslint/babel-eslint-parser/src/configuration.js index 1ad06e670320..94e131e9b73b 100644 --- a/eslint/babel-eslint-parser/src/configuration.js +++ b/eslint/babel-eslint-parser/src/configuration.js @@ -9,7 +9,19 @@ export function normalizeESLintConfig(options) { requireConfigFile: true, }; - return Object.assign(defaultOptions, options); + // ESLint sets ecmaVersion: undefined when ecmaVersion is not set in the config. + // Prune to ensure that defaults are respected. + const prunedOptions = Object.entries(options).reduce( + (options, [key, value]) => { + if (value !== undefined) { + options[key] = value; + } + return options; + }, + {}, + ); + + return Object.assign(defaultOptions, prunedOptions); } export function normalizeBabelParseConfig(options) { diff --git a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js b/eslint/babel-eslint-tests/test/integration/eslint/eslint.js index 5a8ed44b23b9..54d54a83fc2e 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/eslint.js @@ -1,3 +1,5 @@ +import eslint from "eslint"; +import * as parser from "../../../../babel-eslint-parser"; import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages"; describe("verify", () => { @@ -1879,3 +1881,23 @@ describe("verify", () => { ); }); }); + +describe("ESLint config", () => { + it('should set ecmaVersion to latest and sourceType to "module" by default', () => { + const linter = new eslint.Linter(); + linter.defineParser("@babel/eslint-parser", parser); + + // ImportDeclarations result in a parser error if ecmaVersion < 2015 and sourceType != "module". + const messages = linter.verify('import { hello } from "greetings"', { + parser: "@babel/eslint-parser", + parserOptions: { + babelOptions: { + configFile: require.resolve( + "../../../../babel-eslint-shared-fixtures/config/babel.config.js", + ), + }, + }, + }); + expect(messages.length).toEqual(0); + }); +});