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

Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
This commit is contained in:
Huáng Jùnliàng 2021-02-24 14:13:47 -05:00 committed by GitHub
parent 6a471decc3
commit 227f881f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -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,
@ -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,
},

View File

@ -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");
});