Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-parser): merge input estree options #12891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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