From f32377f86ede718528bc401ce466bd7dec67c497 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Sun, 16 Aug 2020 21:41:19 -0400 Subject: [PATCH 1/3] fix: ensure defaults are set in @babel/eslint-parser --- .../babel-eslint-parser/src/configuration.js | 14 +++++++++++- .../test/integration/eslint/eslint.js | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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 5ec83416f5ac..068b2d37200a 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", () => { @@ -1915,3 +1917,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); + }); +}); From d35eaaf7c9d2865f72c11fd359a2bc87e3347b97 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Tue, 18 Aug 2020 18:25:09 -0400 Subject: [PATCH 2/3] Address feedback --- eslint/babel-eslint-tests/package.json | 3 ++- .../test/integration/eslint/config.js | 21 ++++++++++++++++++ .../eslint/{eslint.js => verify.js} | 22 ------------------- 3 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 eslint/babel-eslint-tests/test/integration/eslint/config.js rename eslint/babel-eslint-tests/test/integration/eslint/{eslint.js => verify.js} (98%) diff --git a/eslint/babel-eslint-tests/package.json b/eslint/babel-eslint-tests/package.json index 73c48105aee9..05c78c1eef47 100644 --- a/eslint/babel-eslint-tests/package.json +++ b/eslint/babel-eslint-tests/package.json @@ -4,7 +4,8 @@ "description": "Tests for babel/eslint-* packages", "license": "MIT", "private": true, - "devDependencies": { + "dependencies": { + "@babel/eslint-parser": "^7.11.0", "dedent": "^0.7.0", "eslint": "^7.5.0", "eslint-plugin-import": "^2.22.0" diff --git a/eslint/babel-eslint-tests/test/integration/eslint/config.js b/eslint/babel-eslint-tests/test/integration/eslint/config.js new file mode 100644 index 000000000000..e82465b86f9b --- /dev/null +++ b/eslint/babel-eslint-tests/test/integration/eslint/config.js @@ -0,0 +1,21 @@ +import eslint from "eslint"; +import * as parser from "@babel/eslint-parser"; + +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); + }); +}); diff --git a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js b/eslint/babel-eslint-tests/test/integration/eslint/verify.js similarity index 98% rename from eslint/babel-eslint-tests/test/integration/eslint/eslint.js rename to eslint/babel-eslint-tests/test/integration/eslint/verify.js index 068b2d37200a..5ec83416f5ac 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/verify.js @@ -1,5 +1,3 @@ -import eslint from "eslint"; -import * as parser from "../../../../babel-eslint-parser"; import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages"; describe("verify", () => { @@ -1917,23 +1915,3 @@ 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); - }); -}); From 41df4ed1076a3b1b5393431fbb2d56ca0552fbaa Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Tue, 18 Aug 2020 18:27:45 -0400 Subject: [PATCH 3/3] Update eslint config normalization --- .../babel-eslint-parser/src/configuration.js | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/eslint/babel-eslint-parser/src/configuration.js b/eslint/babel-eslint-parser/src/configuration.js index 94e131e9b73b..0e4cf5b3209e 100644 --- a/eslint/babel-eslint-parser/src/configuration.js +++ b/eslint/babel-eslint-parser/src/configuration.js @@ -1,27 +1,24 @@ import { loadPartialConfig } from "@babel/core"; export function normalizeESLintConfig(options) { - const defaultOptions = { - babelOptions: {}, - ecmaVersion: 2020, - sourceType: "module", - allowImportExportEverywhere: false, - requireConfigFile: true, + const { + babelOptions = {}, + // ESLint sets ecmaVersion: undefined when ecmaVersion is not set in the config. + ecmaVersion = 2020, + sourceType = "module", + allowImportExportEverywhere = false, + requireConfigFile = true, + ...otherOptions + } = options; + + return { + babelOptions, + ecmaVersion, + sourceType, + allowImportExportEverywhere, + requireConfigFile, + ...otherOptions, }; - - // 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) {