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 parserOverride support in @babel/eslint-parser #13918

Merged
merged 2 commits into from Nov 4, 2021
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
7 changes: 5 additions & 2 deletions eslint/babel-eslint-parser/src/worker/maybeParse.cjs
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin.js"]
}
@@ -0,0 +1,9 @@
const parser = require("@babel/parser");

module.exports = function () {
return {
parserOverride(code, opts) {
return parser.parse(`foo;`, opts);
},
};
};
51 changes: 51 additions & 0 deletions eslint/babel-eslint-tests/test/integration/parser-override.js
@@ -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);
});
});