Skip to content

Commit

Permalink
fix: imply babelrc and configFile from requireConfigFile
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 4, 2022
1 parent 9e9301d commit ac78f15
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
17 changes: 12 additions & 5 deletions eslint/babel-eslint-parser/src/worker/configuration.cjs
Expand Up @@ -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
? {}
Expand All @@ -36,16 +43,16 @@ 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,
tokens: true,
},
caller: {
name: "@babel/eslint-parser",
...options.babelOptions.caller,
...babelOptions.caller,
},
};
}
Expand Down
@@ -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,
}
`);
});
});

0 comments on commit ac78f15

Please sign in to comment.