Skip to content

Commit

Permalink
chore: Improve loadFullConfig types (babel#16872)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Oct 1, 2024
1 parent d68ebeb commit 2ee4982
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/babel-core/src/config/full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
validate,
checkNoUnwrappedItemOptionPairs,
} from "./validation/options.ts";
import type { PluginItem } from "./validation/options.ts";
import type { InputOptions, PluginItem } from "./validation/options.ts";
import { validatePluginObject } from "./validation/plugins.ts";
import { makePluginAPI, makePresetAPI } from "./helpers/config-api.ts";
import type { PluginAPI, PresetAPI } from "./helpers/config-api.ts";
Expand Down Expand Up @@ -57,7 +57,7 @@ export type PluginPassList = Array<Plugin>;
export type PluginPasses = Array<PluginPassList>;

export default gensync(function* loadFullConfig(
inputOpts: unknown,
inputOpts: InputOptions,
): Handler<ResolvedConfig | null> {
const result = yield* loadPrivatePartialConfig(inputOpts);
if (!result) {
Expand All @@ -69,7 +69,7 @@ export default gensync(function* loadFullConfig(
return null;
}

const optionDefaults = {};
const optionDefaults: ValidatedOptions = {};

const { plugins, presets } = options;

Expand Down Expand Up @@ -168,7 +168,7 @@ export default gensync(function* loadFullConfig(

if (ignored) return null;

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

const pluginContext: Context.FullPlugin = {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type {
Plugin,
} from "./full.ts";

import type { PluginTarget } from "./validation/options.ts";
import type { InputOptions, PluginTarget } from "./validation/options.ts";

import type {
PluginAPI as basePluginAPI,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function loadPartialConfig(
}
}

function* loadOptionsImpl(opts: unknown): Handler<ResolvedConfig | null> {
function* loadOptionsImpl(opts: InputOptions): Handler<ResolvedConfig | null> {
const config = yield* loadFullConfig(opts);
// NOTE: We want to return "null" explicitly, while ?. alone returns undefined
return config?.options ?? null;
Expand Down
16 changes: 7 additions & 9 deletions packages/babel-core/src/config/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ValidatedOptions,
NormalizedOptions,
RootMode,
InputOptions,
} from "./validation/options.ts";

import {
Expand Down Expand Up @@ -54,18 +55,19 @@ function resolveRootMode(rootDir: string, rootMode: RootMode): string {
}
}

type PrivPartialConfig = {
export type PrivPartialConfig = {
showIgnoredFiles?: boolean;
options: NormalizedOptions;
context: ConfigContext;
fileHandling: FileHandling;
ignore: IgnoreFile | void;
babelrc: ConfigFile | void;
config: ConfigFile | void;
ignore: IgnoreFile | void;
fileHandling: FileHandling;
files: Set<string>;
};

export default function* loadPrivatePartialConfig(
inputOpts: unknown,
inputOpts: InputOptions,
): Handler<PrivPartialConfig | null> {
if (
inputOpts != null &&
Expand Down Expand Up @@ -154,12 +156,8 @@ export default function* loadPrivatePartialConfig(
};
}

type LoadPartialConfigOpts = {
showIgnoredFiles?: boolean;
};

export function* loadPartialConfig(
opts?: LoadPartialConfigOpts,
opts?: InputOptions,
): Handler<PartialConfig | null> {
let showIgnoredFiles = false;
// We only extract showIgnoredFiles if opts is an object, so that
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export type ValidatedOptions = {
ignore?: IgnoreList;
only?: IgnoreList;
overrides?: OverridesList;
showIgnoredFiles?: boolean;
// Generally verify if a given config object should be applied to the given file.
test?: ConfigApplicableTest;
include?: ConfigApplicableTest;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
// eslint-disable-next-line no-restricted-globals
exports.OptionManager = class OptionManager {
init(opts: unknown) {
init(opts: any) {
return loadOptionsSync(opts);
}
};
Expand Down

0 comments on commit 2ee4982

Please sign in to comment.