Skip to content

Commit

Permalink
feat: add isAsync on PluginPass
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Oct 20, 2024
1 parent 22e893c commit 166c7ff
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/babel-core/src/transformation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import generateCode from "./file/generate.ts";
import type File from "./file/file.ts";

import { flattenToSet } from "../config/helpers/deep-array.ts";
import { maybeAsync } from "../gensync-utils/async.ts";
import { isAsync, maybeAsync } from "../gensync-utils/async.ts";

export type FileResultCallback = {
(err: Error, file: null): void;
Expand Down Expand Up @@ -80,13 +80,15 @@ export function* run(
}

function* transformFile(file: File, pluginPasses: PluginPasses): Handler<void> {
const async = yield* isAsync();

for (const pluginPairs of pluginPasses) {
const passPairs: [Plugin, PluginPass][] = [];
const passes = [];
const visitors = [];

for (const plugin of pluginPairs.concat([loadBlockHoistPlugin()])) {
const pass = new PluginPass(file, plugin.key, plugin.options);
const pass = new PluginPass(file, plugin.key, plugin.options, async);

passPairs.push([plugin, pass]);
passes.push(pass);
Expand Down
21 changes: 17 additions & 4 deletions packages/babel-core/src/transformation/plugin-pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,32 @@ export default class PluginPass<Options = object> {
file: File;
opts: Partial<Options>;

// The working directory that Babel's programmatic options are loaded
// relative to.
/**
* The working directory that Babel's programmatic options are loaded
* relative to.
*/
cwd: string;

// The absolute path of the file being compiled.
/** The absolute path of the file being compiled. */
filename: string | void;

constructor(file: File, key?: string | null, options?: Options) {
/**
* Is Babel executed in async mode or not.
*/
isAsync: boolean | undefined;

constructor(
file: File,
key?: string | null,
options?: Options,
isAsync?: boolean,
) {
this.key = key;
this.file = file;
this.opts = options || {};
this.cwd = file.opts.cwd;
this.filename = file.opts.filename;
this.isAsync = isAsync;
}

set(key: unknown, val: unknown) {
Expand Down
28 changes: 27 additions & 1 deletion packages/babel-core/test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ describe("asynchronicity", () => {
});
});

describe("PluginPass.isAsync", () => {
nodeGte8("called synchronously", () => {
process.chdir("plugin-pass-is-async");

expect(babel.transformSync("")).toMatchObject({
code: `"sync"`,
});
});

nodeGte8("called asynchronously", async () => {
process.chdir("plugin-pass-is-async");

await expect(babel.transformAsync("")).resolves.toMatchObject({
code: `"async"`,
});
});

nodeGte8("should await inherited .pre", async () => {
process.chdir("plugin-pre-chaining");

await expect(babel.transformAsync("")).resolves.toMatchObject({
code: `"pluginC,pluginB,pluginA"`,
});
});
});

describe("inherits", () => {
nodeGte8("called synchronously", () => {
process.chdir("plugin-inherits");
Expand Down Expand Up @@ -312,7 +338,7 @@ describe("asynchronicity", () => {
});

describe("misc", () => {
it("unknown preset in config file does not trigget unhandledRejection if caught", async () => {
it("unknown preset in config file does not trigger unhandledRejection if caught", async () => {
process.chdir("unknown-preset");
const handler = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ["./plugin"],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function pluginA({ types: t }) {
return {
visitor: {
Program(path) {
const label = this.isAsync ? "async" : "sync";

path.pushContainer("body", t.stringLiteral(label));
},
},
};
};

0 comments on commit 166c7ff

Please sign in to comment.