Fix parserOverride support in @babel/eslint-parser (#13918)

This commit is contained in:
Nicolò Ribaudo 2021-11-04 01:15:26 +01:00 committed by GitHub
parent 6393e60a68
commit 531db5dc4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 2 deletions

View File

@ -15,7 +15,8 @@ module.exports = function maybeParse(code, options) {
{ dirname: __dirname, type: "plugin" }, { dirname: __dirname, type: "plugin" },
); );
} }
options.plugins.push(extractParserOptionsConfigItem); const { plugins } = options;
options.plugins = plugins.concat(extractParserOptionsConfigItem);
try { try {
return { return {
@ -28,8 +29,10 @@ module.exports = function maybeParse(code, options) {
} }
} }
let ast; // There was already a parserOverride, so remove our plugin.
options.plugins = plugins;
let ast;
try { try {
ast = babel.parseSync(code, options); ast = babel.parseSync(code, options);
} catch (err) { } catch (err) {

View File

@ -0,0 +1,3 @@
{
"plugins": ["./plugin.js"]
}

View File

@ -0,0 +1,9 @@
const parser = require("@babel/parser");
module.exports = function () {
return {
parserOverride(code, opts) {
return parser.parse(`foo;`, opts);
},
};
};

View File

@ -0,0 +1,51 @@
import path from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import * as babelESLint from "@babel/eslint-parser";
describe("parserOverride", () => {
const expectedAST = {
type: "Program",
body: [
{
type: "ExpressionStatement",
expression: {
type: "Identifier",
name: "foo",
},
},
],
};
it("works when parsing in the main thread", () => {
const { ast } = babelESLint.parseForESLint(`27`, {
filename: "input.js",
babelOptions: {
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../fixtures/parser-override/babel.config.json",
),
},
});
expect(ast).toMatchObject(expectedAST);
});
const babel7node12 = parseInt(process.versions.node) < 12 ? it.skip : it;
babel7node12("works when parsing in a worker", async () => {
const require = createRequire(import.meta.url);
const babelESLintWorker = require("@babel/eslint-parser/experimental-worker");
const { ast } = babelESLintWorker.parseForESLint(`27`, {
filename: "input.js",
babelOptions: {
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../fixtures/parser-override/babel.config.json",
),
},
});
expect(ast).toMatchObject(expectedAST);
});
});