Skip to content

Commit

Permalink
fix: ensure defaults are set in @babel/eslint-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Aug 17, 2020
1 parent 028a051 commit 8d444b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion eslint/babel-eslint-parser/src/configuration.js
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions 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", () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 8d444b8

Please sign in to comment.