Skip to content

Commit

Permalink
fix(eslint-parser): merge input estree options (#12891)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
  • Loading branch information
JLHwung and kaicataldo committed Feb 24, 2021
1 parent 6a471de commit 227f881
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
25 changes: 21 additions & 4 deletions eslint/babel-eslint-parser/src/configuration.js
Expand Up @@ -21,6 +21,26 @@ export function normalizeESLintConfig(options) {
};
}

/**
* Merge user supplied estree plugin options to default estree plugin options
*
* @param {*} babelOptions
* @returns {Array} Merged parser plugin descriptors
*/
function getParserPlugins(babelOptions) {
const babelParserPlugins = babelOptions.parserOpts?.plugins ?? [];
// todo: enable classFeatures when it is supported by ESLint
const estreeOptions = { classFeatures: false };
for (const plugin of babelParserPlugins) {
if (Array.isArray(plugin) && plugin[0] === "estree") {
Object.assign(estreeOptions, plugin[1]);
break;
}
}
// estree must be the first parser plugin to work with other parser plugins
return [["estree", estreeOptions], ...babelParserPlugins];
}

export function normalizeBabelParseConfig(options) {
const parseOptions = {
sourceType: options.sourceType,
Expand All @@ -31,10 +51,7 @@ export function normalizeBabelParseConfig(options) {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
...options.babelOptions.parserOpts,
plugins: [
["estree", { classFeatures: false }],
...(options.babelOptions.parserOpts?.plugins ?? []),
],
plugins: getParserPlugins(options.babelOptions),
ranges: true,
tokens: true,
},
Expand Down
20 changes: 20 additions & 0 deletions eslint/babel-eslint-parser/test/index.js
Expand Up @@ -327,6 +327,26 @@ describe("Babel and Espree", () => {
expect(babylonAST.tokens[3].value).toEqual("#");
});

it("parse to PropertyDeclaration when `classFeatures: true`", () => {
const code = "class A { #x }";
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions: {
filename: "test.js",
parserOpts: {
plugins: [
["estree", { classFeatures: true }],
"classPrivateProperties",
"classProperties",
],
},
},
}).ast;
const classDeclaration = babylonAST.body[0];
expect(classDeclaration.body.body[0].type).toEqual("PropertyDefinition");
});

it("empty program with line comment", () => {
parseAndAssertSame("// single comment");
});
Expand Down

0 comments on commit 227f881

Please sign in to comment.