Skip to content

Commit 76f3c85

Browse files
fix: support async Babel plugin in eslint-parser (#17348)
* Attempt to fix #17331 * Split maybeParse * Apparently the async version of createConfigItem doesn't exist? * Add to mini-babel-core, and rename maybeParse to maybeParseSync and asyncMaybeParse to maybeParse * Use createConfigItemAsync * Hope this is sufficient for a test * Don't need an import * ope * review --------- Co-authored-by: Babel Bot <[email protected]>
1 parent f3fca59 commit 76f3c85

13 files changed

Lines changed: 151 additions & 7 deletions

File tree

eslint/babel-eslint-parser/src/worker/babel-core.cts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ function initialize(babel: typeof import("@babel/core")) {
99
exports.types = babel.types;
1010
exports.tokTypes = babel.tokTypes;
1111
exports.parseSync = babel.parseSync;
12+
exports.parseAsync = babel.parseAsync;
1213
exports.loadPartialConfigSync = babel.loadPartialConfigSync;
1314
exports.loadPartialConfigAsync = babel.loadPartialConfigAsync;
15+
exports.createConfigItemAsync = babel.createConfigItemAsync;
16+
1417
if (process.env.BABEL_8_BREAKING) {
1518
exports.createConfigItemSync = babel.createConfigItemSync;
1619
} else {

eslint/babel-eslint-parser/src/worker/handle-message.cts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import babel = require("./babel-core.cts");
22
import maybeParse = require("./maybeParse.cts");
3+
import maybeParseSync = require("./maybeParseSync.cts");
34
import astInfo = require("./ast-info.cts");
45
import config = require("./configuration.cts");
56

@@ -25,7 +26,7 @@ export = function handleMessage(action: ACTIONS, payload: any) {
2526
.then(options => maybeParse(payload.code, options));
2627
case ACTIONS.MAYBE_PARSE_SYNC:
2728
if (!USE_ESM) {
28-
return maybeParse(
29+
return maybeParseSync(
2930
payload.code,
3031
config.normalizeBabelParseConfigSync(payload.options),
3132
);

eslint/babel-eslint-parser/src/worker/maybeParse.cts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ let extractParserOptionsConfigItem: ConfigItem<any>;
1313

1414
const MULTIPLE_OVERRIDES = /More than one plugin attempted to override parsing/;
1515

16-
export = function maybeParse(
16+
export = async function asyncMaybeParse(
1717
code: string,
1818
options: InputOptions,
19-
): {
19+
): Promise<{
2020
ast: AST.Program | null;
2121
parserOptions: ParseResult | null;
22-
} {
22+
}> {
2323
if (!extractParserOptionsConfigItem) {
24-
extractParserOptionsConfigItem = babel.createConfigItemSync(
24+
extractParserOptionsConfigItem = await babel.createConfigItemAsync(
2525
[extractParserOptionsPlugin, ref],
2626
{ dirname: __dirname, type: "plugin" },
2727
);
@@ -33,7 +33,7 @@ export = function maybeParse(
3333

3434
try {
3535
return {
36-
parserOptions: babel.parseSync(code, options),
36+
parserOptions: await babel.parseAsync(code, options),
3737
ast: null,
3838
};
3939
} catch (err) {
@@ -46,7 +46,7 @@ export = function maybeParse(
4646
options.plugins = plugins;
4747

4848
try {
49-
ast = babel.parseSync(code, options);
49+
ast = await babel.parseAsync(code, options);
5050
} catch (err) {
5151
throw convert.convertError(err);
5252
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import babel = require("./babel-core.cts");
2+
import convert = require("../convert/index.cts");
3+
import astInfo = require("./ast-info.cts");
4+
import extractParserOptionsPlugin = require("./extract-parser-options-plugin.cjs");
5+
6+
import type { InputOptions, ConfigItem } from "@babel/core";
7+
import type { AST, ParseResult } from "../types.cts";
8+
9+
const { getVisitorKeys, getTokLabels } = astInfo;
10+
11+
const ref = {};
12+
let extractParserOptionsConfigItem: ConfigItem<any>;
13+
14+
const MULTIPLE_OVERRIDES = /More than one plugin attempted to override parsing/;
15+
16+
export = function maybeParseSync(
17+
code: string,
18+
options: InputOptions,
19+
): {
20+
ast: AST.Program | null;
21+
parserOptions: ParseResult | null;
22+
} {
23+
if (!extractParserOptionsConfigItem) {
24+
extractParserOptionsConfigItem = babel.createConfigItemSync(
25+
[extractParserOptionsPlugin, ref],
26+
{ dirname: __dirname, type: "plugin" },
27+
);
28+
}
29+
const { plugins } = options;
30+
options.plugins = plugins.concat(extractParserOptionsConfigItem);
31+
32+
let ast;
33+
34+
try {
35+
return {
36+
parserOptions: babel.parseSync(code, options),
37+
ast: null,
38+
};
39+
} catch (err) {
40+
if (!MULTIPLE_OVERRIDES.test(err.message)) {
41+
throw err;
42+
}
43+
}
44+
45+
// There was already a parserOverride, so remove our plugin.
46+
options.plugins = plugins;
47+
48+
try {
49+
ast = babel.parseSync(code, options);
50+
} catch (err) {
51+
throw convert.convertError(err);
52+
}
53+
54+
return {
55+
ast: convert.convertFile(ast, code, getTokLabels(), getVisitorKeys()),
56+
parserOptions: null,
57+
};
58+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@babel/eslint-parser/experimental-worker",
4+
parserOptions: {
5+
babelOptions: {
6+
configFile: __dirname + "/babel.config.json",
7+
sourceType: "module",
8+
},
9+
},
10+
rules: {
11+
"template-curly-spacing": "error",
12+
},
13+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function foo() {
2+
return "bar";
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["module:./plugin.mjs"]
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default async function () {
2+
await Promise.resolve();
3+
4+
return {
5+
visitor: {
6+
StringLiteral(path) {
7+
path.replaceWithSourceString("foo");
8+
}
9+
}
10+
};
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function foo() {
2+
return "bar";
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["module:./plugin.mjs"]
3+
}

0 commit comments

Comments
 (0)