diff --git a/eslint/babel-eslint-parser/src/worker/configuration.cjs b/eslint/babel-eslint-parser/src/worker/configuration.cjs index 0908a8e03074..d8eb2e546fd1 100644 --- a/eslint/babel-eslint-parser/src/worker/configuration.cjs +++ b/eslint/babel-eslint-parser/src/worker/configuration.cjs @@ -20,11 +20,18 @@ function getParserPlugins(babelOptions) { return [["estree", estreeOptions], ...babelParserPlugins]; } -function normalizeParserOptions(options) { +// @private +// exported for unit test purpose only +exports.normalizeParserOptions = normalizeParserOptions; + +function normalizeParserOptions(options = {}) { + const { babelOptions = {} } = options; return { sourceType: options.sourceType, filename: options.filePath, - ...options.babelOptions, + babelrc: options.requireConfigFile ? undefined : false, + configFile: options.requireConfigFile ? undefined : false, + ...babelOptions, parserOpts: { ...(process.env.BABEL_8_BREAKING ? {} @@ -36,8 +43,8 @@ function normalizeParserOptions(options) { allowReturnOutsideFunction: options.ecmaFeatures?.globalReturn ?? (process.env.BABEL_8_BREAKING ? false : true), - ...options.babelOptions.parserOpts, - plugins: getParserPlugins(options.babelOptions), + ...babelOptions.parserOpts, + plugins: getParserPlugins(babelOptions), // skip comment attaching for parsing performance attachComment: false, ranges: true, @@ -45,7 +52,7 @@ function normalizeParserOptions(options) { }, caller: { name: "@babel/eslint-parser", - ...options.babelOptions.caller, + ...babelOptions.caller, }, }; } diff --git a/eslint/babel-eslint-parser/test/worker/configuration.skip-bundled.js b/eslint/babel-eslint-parser/test/worker/configuration.skip-bundled.js new file mode 100644 index 000000000000..9699f125e726 --- /dev/null +++ b/eslint/babel-eslint-parser/test/worker/configuration.skip-bundled.js @@ -0,0 +1,43 @@ +import { normalizeParserOptions } from "../../lib/worker/configuration.cjs"; + +describe("normalizeParserOptions", () => { + describe("requireConfigFile", () => { + it("should imply 'babelrc' and 'configFile' from 'requireConfigFile'", () => { + const result = normalizeParserOptions({ requireConfigFile: false }); + expect(result).toHaveProperty("babelrc", false); + expect(result).toHaveProperty("configFile", false); + }); + it("The implied result can be overriden by 'babelOptions'", () => { + const result = normalizeParserOptions({ + requireConfigFile: false, + babelOptions: { + babelrc: undefined, + configFile: undefined, + }, + }); + expect(result).toHaveProperty("babelrc", undefined); + expect(result).toHaveProperty("configFile", undefined); + }); + }); + it("default parserOpts", () => { + const result = normalizeParserOptions({ requireConfigFile: false }); + expect(result.parserOpts).toMatchInlineSnapshot(` + Object { + "allowImportExportEverywhere": false, + "allowReturnOutsideFunction": true, + "allowSuperOutsideMethod": true, + "attachComment": false, + "plugins": Array [ + Array [ + "estree", + Object { + "classFeatures": false, + }, + ], + ], + "ranges": true, + "tokens": true, + } + `); + }); +});