Skip to content

Commit

Permalink
Use gensync types from DefinitelyTyped (#14710)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 11, 2022
1 parent 2b163d8 commit a34a5ae
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 276 deletions.
1 change: 1 addition & 0 deletions packages/babel-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@jridgewell/trace-mapping": "^0.3.8",
"@types/convert-source-map": "^1.5.1",
"@types/debug": "^4.1.0",
"@types/gensync": "^1.0.0",
"@types/resolve": "^1.3.2",
"@types/semver": "^5.4.0",
"rimraf": "^3.0.0"
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-core/src/config/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type CacheEntry<ResultT, SideChannel> = Array<{
valid: (channel: SideChannel) => Handler<boolean>;
}>;

const synchronize = <ArgsT extends any[], ResultT>(
const synchronize = <ArgsT extends unknown[], ResultT>(
gen: (...args: ArgsT) => Handler<ResultT>,
): ((...args: ArgsT) => ResultT) => {
return gensync(gen).sync;
Expand Down Expand Up @@ -128,9 +128,7 @@ function makeCachedFunction<ArgT, ResultT, SideChannel>(
let value: ResultT;

if (isIterableIterator(handlerResult)) {
const gen = handlerResult as Generator<unknown, ResultT, unknown>;

value = yield* onFirstPause(gen, () => {
value = yield* onFirstPause(handlerResult, () => {
finishLock = setupAsyncLocks(cache, futureCache, arg);
});
} else {
Expand Down
9 changes: 5 additions & 4 deletions packages/babel-core/src/config/files/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import buildDebug from "debug";
import path from "path";
import gensync, { type Gensync, type Handler } from "gensync";
import gensync, { type Handler } from "gensync";
import { isAsync } from "../../gensync-utils/async";
import loadCjsOrMjsDefault, { supportsESM } from "./module-types";
import { fileURLToPath, pathToFileURL } from "url";
Expand Down Expand Up @@ -170,9 +170,10 @@ async function resolveStandardizedNameForImport(
return fileURLToPath(res.value);
}

const resolveStandardizedName: Gensync<
(type: "plugin" | "preset", name: string, dirname?: string) => string
> = gensync({
const resolveStandardizedName = gensync<
[type: "plugin" | "preset", name: string, dirname?: string],
string
>({
sync(type, name, dirname = process.cwd()) {
return resolveStandardizedNameForRequire(type, name, dirname);
},
Expand Down
287 changes: 136 additions & 151 deletions packages/babel-core/src/config/full.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import gensync from "gensync";
import type { Handler } from "gensync";
import gensync, { type Handler } from "gensync";
import { forwardAsync, maybeAsync, isThenable } from "../gensync-utils/async";

import { mergeOptions } from "./util";
Expand Down Expand Up @@ -52,176 +51,165 @@ export type { Plugin };
export type PluginPassList = Array<Plugin>;
export type PluginPasses = Array<PluginPassList>;

export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
function* loadFullConfig(inputOpts) {
const result = yield* loadPrivatePartialConfig(inputOpts);
if (!result) {
return null;
}
const { options, context, fileHandling } = result;
export default gensync(function* loadFullConfig(
inputOpts: unknown,
): Handler<ResolvedConfig | null> {
const result = yield* loadPrivatePartialConfig(inputOpts);
if (!result) {
return null;
}
const { options, context, fileHandling } = result;

if (fileHandling === "ignored") {
return null;
}
if (fileHandling === "ignored") {
return null;
}

const optionDefaults = {};
const optionDefaults = {};

const { plugins, presets } = options;
const { plugins, presets } = options;

if (!plugins || !presets) {
throw new Error("Assertion failure - plugins and presets exist");
}
if (!plugins || !presets) {
throw new Error("Assertion failure - plugins and presets exist");
}

const presetContext: Context.FullPreset = {
...context,
targets: options.targets,
};
const presetContext: Context.FullPreset = {
...context,
targets: options.targets,
};

const toDescriptor = (item: PluginItem) => {
const desc = getItemDescriptor(item);
if (!desc) {
throw new Error("Assertion failure - must be config item");
}
const toDescriptor = (item: PluginItem) => {
const desc = getItemDescriptor(item);
if (!desc) {
throw new Error("Assertion failure - must be config item");
}

return desc;
};
return desc;
};

const presetsDescriptors = presets.map(toDescriptor);
const initialPluginsDescriptors = plugins.map(toDescriptor);
const pluginDescriptorsByPass: Array<Array<UnloadedDescriptor>> = [[]];
const passes: Array<Array<Plugin>> = [];

const externalDependencies: DeepArray<string> = [];

const ignored = yield* enhanceError(
context,
function* recursePresetDescriptors(
rawPresets: Array<UnloadedDescriptor>,
pluginDescriptorsPass: Array<UnloadedDescriptor>,
): Handler<true | void> {
const presets: Array<{
preset: ConfigChain | null;
pass: Array<UnloadedDescriptor>;
}> = [];

for (let i = 0; i < rawPresets.length; i++) {
const descriptor = rawPresets[i];
if (descriptor.options !== false) {
try {
// eslint-disable-next-line no-var
var preset = yield* loadPresetDescriptor(
descriptor,
presetContext,
);
} catch (e) {
if (e.code === "BABEL_UNKNOWN_OPTION") {
checkNoUnwrappedItemOptionPairs(rawPresets, i, "preset", e);
}
throw e;
}
const presetsDescriptors = presets.map(toDescriptor);
const initialPluginsDescriptors = plugins.map(toDescriptor);
const pluginDescriptorsByPass: Array<Array<UnloadedDescriptor>> = [[]];
const passes: Array<Array<Plugin>> = [];

externalDependencies.push(preset.externalDependencies);

// Presets normally run in reverse order, but if they
// have their own pass they run after the presets
// in the previous pass.
if (descriptor.ownPass) {
presets.push({ preset: preset.chain, pass: [] });
} else {
presets.unshift({
preset: preset.chain,
pass: pluginDescriptorsPass,
});
const externalDependencies: DeepArray<string> = [];

const ignored = yield* enhanceError(
context,
function* recursePresetDescriptors(
rawPresets: Array<UnloadedDescriptor>,
pluginDescriptorsPass: Array<UnloadedDescriptor>,
): Handler<true | void> {
const presets: Array<{
preset: ConfigChain | null;
pass: Array<UnloadedDescriptor>;
}> = [];

for (let i = 0; i < rawPresets.length; i++) {
const descriptor = rawPresets[i];
if (descriptor.options !== false) {
try {
// eslint-disable-next-line no-var
var preset = yield* loadPresetDescriptor(descriptor, presetContext);
} catch (e) {
if (e.code === "BABEL_UNKNOWN_OPTION") {
checkNoUnwrappedItemOptionPairs(rawPresets, i, "preset", e);
}
throw e;
}
}

// resolve presets
if (presets.length > 0) {
// The passes are created in the same order as the preset list, but are inserted before any
// existing additional passes.
pluginDescriptorsByPass.splice(
1,
0,
...presets
.map(o => o.pass)
.filter(p => p !== pluginDescriptorsPass),
);

for (const { preset, pass } of presets) {
if (!preset) return true;

pass.push(...preset.plugins);

const ignored = yield* recursePresetDescriptors(
preset.presets,
pass,
);
if (ignored) return true;

preset.options.forEach(opts => {
mergeOptions(optionDefaults, opts);
externalDependencies.push(preset.externalDependencies);

// Presets normally run in reverse order, but if they
// have their own pass they run after the presets
// in the previous pass.
if (descriptor.ownPass) {
presets.push({ preset: preset.chain, pass: [] });
} else {
presets.unshift({
preset: preset.chain,
pass: pluginDescriptorsPass,
});
}
}
},
)(presetsDescriptors, pluginDescriptorsByPass[0]);
}

if (ignored) return null;
// resolve presets
if (presets.length > 0) {
// The passes are created in the same order as the preset list, but are inserted before any
// existing additional passes.
pluginDescriptorsByPass.splice(
1,
0,
...presets.map(o => o.pass).filter(p => p !== pluginDescriptorsPass),
);

const opts: any = optionDefaults;
mergeOptions(opts, options);
for (const { preset, pass } of presets) {
if (!preset) return true;

const pluginContext: Context.FullPlugin = {
...presetContext,
assumptions: opts.assumptions ?? {},
};
pass.push(...preset.plugins);

yield* enhanceError(context, function* loadPluginDescriptors() {
pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);

for (const descs of pluginDescriptorsByPass) {
const pass: Plugin[] = [];
passes.push(pass);

for (let i = 0; i < descs.length; i++) {
const descriptor: UnloadedDescriptor = descs[i];
if (descriptor.options !== false) {
try {
// eslint-disable-next-line no-var
var plugin = yield* loadPluginDescriptor(
descriptor,
pluginContext,
);
} catch (e) {
if (e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") {
// print special message for `plugins: ["@babel/foo", { foo: "option" }]`
checkNoUnwrappedItemOptionPairs(descs, i, "plugin", e);
}
throw e;
}
pass.push(plugin);
const ignored = yield* recursePresetDescriptors(preset.presets, pass);
if (ignored) return true;

externalDependencies.push(plugin.externalDependencies);
preset.options.forEach(opts => {
mergeOptions(optionDefaults, opts);
});
}
}
},
)(presetsDescriptors, pluginDescriptorsByPass[0]);

if (ignored) return null;

const opts: any = optionDefaults;
mergeOptions(opts, options);

const pluginContext: Context.FullPlugin = {
...presetContext,
assumptions: opts.assumptions ?? {},
};

yield* enhanceError(context, function* loadPluginDescriptors() {
pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);

for (const descs of pluginDescriptorsByPass) {
const pass: Plugin[] = [];
passes.push(pass);

for (let i = 0; i < descs.length; i++) {
const descriptor: UnloadedDescriptor = descs[i];
if (descriptor.options !== false) {
try {
// eslint-disable-next-line no-var
var plugin = yield* loadPluginDescriptor(descriptor, pluginContext);
} catch (e) {
if (e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") {
// print special message for `plugins: ["@babel/foo", { foo: "option" }]`
checkNoUnwrappedItemOptionPairs(descs, i, "plugin", e);
}
throw e;
}
pass.push(plugin);

externalDependencies.push(plugin.externalDependencies);
}
}
})();
}
})();

opts.plugins = passes[0];
opts.presets = passes
.slice(1)
.filter(plugins => plugins.length > 0)
.map(plugins => ({ plugins }));
opts.passPerPreset = opts.presets.length > 0;
opts.plugins = passes[0];
opts.presets = passes
.slice(1)
.filter(plugins => plugins.length > 0)
.map(plugins => ({ plugins }));
opts.passPerPreset = opts.presets.length > 0;

return {
options: opts,
passes: passes,
externalDependencies: freezeDeepArray(externalDependencies),
};
},
);
return {
options: opts,
passes: passes,
externalDependencies: freezeDeepArray(externalDependencies),
};
});

function enhanceError<T extends Function>(context: ConfigContext, fn: T): T {
return function* (arg1: unknown, arg2: unknown) {
Expand Down Expand Up @@ -384,10 +372,7 @@ const instantiatePlugin = makeWeakCache(function* (
dirname,
};

const inherits = yield* forwardAsync<
(d: UnloadedDescriptor, c: Context.SimplePlugin) => Plugin,
Plugin
>(loadPluginDescriptor, run => {
const inherits = yield* forwardAsync(loadPluginDescriptor, run => {
// If the inherited plugin changes, reinstantiate this plugin.
return cache.invalidate(data => run(inheritsDescriptor, data));
});
Expand Down

0 comments on commit a34a5ae

Please sign in to comment.