Skip to content

Commit

Permalink
Fix parserOverride support in @babel/eslint-parser (#13918)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 4, 2021
1 parent 6393e60 commit 531db5d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
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);
});
});

0 comments on commit 531db5d

Please sign in to comment.