Skip to content

Commit

Permalink
fix: ensure defaults are set in @babel/eslint-parser (#11970)
Browse files Browse the repository at this point in the history
* fix: ensure defaults are set in @babel/eslint-parser

* Address feedback

* Update eslint config normalization
  • Loading branch information
kaicataldo committed Aug 19, 2020
1 parent cdada58 commit 180e9c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
25 changes: 17 additions & 8 deletions eslint/babel-eslint-parser/src/configuration.js
@@ -1,15 +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 Object.assign(defaultOptions, options);
return {
babelOptions,
ecmaVersion,
sourceType,
allowImportExportEverywhere,
requireConfigFile,
...otherOptions,
};
}

export function normalizeBabelParseConfig(options) {
Expand Down
3 changes: 2 additions & 1 deletion eslint/babel-eslint-tests/package.json
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions 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);
});
});

0 comments on commit 180e9c0

Please sign in to comment.