Fix parserOverride support in @babel/eslint-parser (#13918)
This commit is contained in:
parent
6393e60a68
commit
531db5dc4e
@ -15,7 +15,8 @@ module.exports = function maybeParse(code, options) {
|
||||
{ dirname: __dirname, type: "plugin" },
|
||||
);
|
||||
}
|
||||
options.plugins.push(extractParserOptionsConfigItem);
|
||||
const { plugins } = options;
|
||||
options.plugins = plugins.concat(extractParserOptionsConfigItem);
|
||||
|
||||
try {
|
||||
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 {
|
||||
ast = babel.parseSync(code, options);
|
||||
} catch (err) {
|
||||
|
||||
3
eslint/babel-eslint-tests/test/fixtures/parser-override/babel.config.json
vendored
Normal file
3
eslint/babel-eslint-tests/test/fixtures/parser-override/babel.config.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["./plugin.js"]
|
||||
}
|
||||
9
eslint/babel-eslint-tests/test/fixtures/parser-override/plugin.js
vendored
Normal file
9
eslint/babel-eslint-tests/test/fixtures/parser-override/plugin.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
const parser = require("@babel/parser");
|
||||
|
||||
module.exports = function () {
|
||||
return {
|
||||
parserOverride(code, opts) {
|
||||
return parser.parse(`foo;`, opts);
|
||||
},
|
||||
};
|
||||
};
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user