Skip to content

Commit a5b300b

Browse files
authored
Enable @typescript-eslint/no-redundant-type-constituents rule (#15794)
1 parent 3d47fb6 commit a5b300b

19 files changed

Lines changed: 104 additions & 93 deletions

File tree

eslint.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ module.exports = [
140140
"@typescript-eslint/no-explicit-any": "off",
141141
"@typescript-eslint/no-misused-promises": "off",
142142
"@typescript-eslint/no-namespace": "off",
143-
"@typescript-eslint/no-redundant-type-constituents": "off",
144143
"@typescript-eslint/no-this-alias": "off",
145144
"@typescript-eslint/no-unsafe-assignment": "off",
146145
"@typescript-eslint/no-unsafe-call": "off",

packages/babel-cli/src/babel-external-helpers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import commander from "commander";
22
import { buildExternalHelpers } from "@babel/core";
33

4-
function collect(
5-
value: string | any,
6-
previousValue: Array<string>,
7-
): Array<string> {
4+
function collect(value: unknown, previousValue: Array<string>): Array<string> {
85
// If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing.
96
if (typeof value !== "string") return previousValue;
107

packages/babel-cli/src/babel/file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { CmdOptions } from "./options";
99
import * as watcher from "./watcher";
1010

1111
import type {
12+
EncodedSourceMap,
1213
SectionedSourceMap,
1314
SourceMapInput,
1415
TraceMap,
@@ -40,7 +41,7 @@ export default async function ({
4041

4142
mapSections.push({
4243
offset: { line: offset, column: 0 },
43-
map: result.map || {
44+
map: (result.map as EncodedSourceMap) || {
4445
version: 3,
4546
names: [],
4647
sources: [],

packages/babel-cli/src/babel/options.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
359359
};
360360
}
361361

362-
function booleanify(val: any): boolean | any {
362+
function booleanify(val: "false" | 0 | ""): false;
363+
function booleanify(val: "true" | 1): true;
364+
function booleanify(val: any): any {
363365
if (val === undefined) return undefined;
364366

365367
if (val === "true" || val == 1) {
@@ -373,10 +375,7 @@ function booleanify(val: any): boolean | any {
373375
return val;
374376
}
375377

376-
function collect(
377-
value: string | any,
378-
previousValue: Array<string>,
379-
): Array<string> {
378+
function collect(value: unknown, previousValue: Array<string>): Array<string> {
380379
// If the user passed the option with no value, like "babel file.js --presets", do nothing.
381380
if (typeof value !== "string") return previousValue;
382381

packages/babel-core/src/config/config-chain.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { ReadonlyDeepArray } from "./helpers/deep-array";
1818

1919
import { endHiddenCallStack } from "../errors/rewrite-stack-trace";
2020
import ConfigError from "../errors/config-error";
21+
import type { PluginAPI, PresetAPI } from "./helpers/config-api";
2122

2223
const debug = buildDebug("babel:config:config-chain");
2324

@@ -42,8 +43,8 @@ import type {
4243
} from "./config-descriptors";
4344

4445
export type ConfigChain = {
45-
plugins: Array<UnloadedDescriptor>;
46-
presets: Array<UnloadedDescriptor>;
46+
plugins: Array<UnloadedDescriptor<PluginAPI>>;
47+
presets: Array<UnloadedDescriptor<PresetAPI>>;
4748
options: Array<ValidatedOptions>;
4849
files: Set<string>;
4950
};
@@ -760,12 +761,12 @@ function normalizeOptions(opts: ValidatedOptions): ValidatedOptions {
760761
return options;
761762
}
762763

763-
function dedupDescriptors(
764-
items: Array<UnloadedDescriptor>,
765-
): Array<UnloadedDescriptor> {
764+
function dedupDescriptors<API>(
765+
items: Array<UnloadedDescriptor<API>>,
766+
): Array<UnloadedDescriptor<API>> {
766767
const map: Map<
767768
Function,
768-
Map<string | void, { value: UnloadedDescriptor }>
769+
Map<string | void, { value: UnloadedDescriptor<API> }>
769770
> = new Map();
770771

771772
const descriptors = [];

packages/babel-core/src/config/config-descriptors.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,36 @@ import type {
1919
} from "./validation/options";
2020

2121
import { resolveBrowserslistConfigFile } from "./resolve-targets";
22+
import type { PluginAPI, PresetAPI } from "./helpers/config-api";
2223

2324
// Represents a config object and functions to lazily load the descriptors
2425
// for the plugins and presets so we don't load the plugins/presets unless
2526
// the options object actually ends up being applicable.
2627
export type OptionsAndDescriptors = {
2728
options: ValidatedOptions;
28-
plugins: () => Handler<Array<UnloadedDescriptor>>;
29-
presets: () => Handler<Array<UnloadedDescriptor>>;
29+
plugins: () => Handler<Array<UnloadedDescriptor<PluginAPI>>>;
30+
presets: () => Handler<Array<UnloadedDescriptor<PresetAPI>>>;
3031
};
3132

3233
// Represents a plugin or presets at a given location in a config object.
3334
// At this point these have been resolved to a specific object or function,
3435
// but have not yet been executed to call functions with options.
35-
export type UnloadedDescriptor = {
36+
export interface UnloadedDescriptor<API, Options = {} | undefined | false> {
3637
name: string | undefined;
37-
value: any | Function;
38-
options: {} | undefined | false;
38+
value: object | ((api: API, options: Options, dirname: string) => unknown);
39+
options: Options;
3940
dirname: string;
4041
alias: string;
4142
ownPass?: boolean;
4243
file?: {
4344
request: string;
4445
resolved: string;
4546
};
46-
};
47+
}
4748

48-
function isEqualDescriptor(
49-
a: UnloadedDescriptor,
50-
b: UnloadedDescriptor,
49+
function isEqualDescriptor<API>(
50+
a: UnloadedDescriptor<API>,
51+
b: UnloadedDescriptor<API>,
5152
): boolean {
5253
return (
5354
a.name === b.name &&
@@ -150,7 +151,7 @@ const createCachedPresetDescriptors = makeWeakCacheSync(
150151
return makeStrongCacheSync((alias: string) =>
151152
makeStrongCache(function* (
152153
passPerPreset: boolean,
153-
): Handler<Array<UnloadedDescriptor>> {
154+
): Handler<Array<UnloadedDescriptor<PresetAPI>>> {
154155
const descriptors = yield* createPresetDescriptors(
155156
items,
156157
dirname,
@@ -174,7 +175,7 @@ const createCachedPluginDescriptors = makeWeakCacheSync(
174175
const dirname = cache.using(dir => dir);
175176
return makeStrongCache(function* (
176177
alias: string,
177-
): Handler<Array<UnloadedDescriptor>> {
178+
): Handler<Array<UnloadedDescriptor<PluginAPI>>> {
178179
const descriptors = yield* createPluginDescriptors(items, dirname, alias);
179180
return descriptors.map(
180181
// Items are cached using the overall plugin array identity when
@@ -197,9 +198,9 @@ const DEFAULT_OPTIONS = {};
197198
* cache, or else returns the input descriptor and adds it to the cache for
198199
* next time.
199200
*/
200-
function loadCachedDescriptor(
201-
cache: WeakMap<{} | Function, WeakMap<{}, Array<UnloadedDescriptor>>>,
202-
desc: UnloadedDescriptor,
201+
function loadCachedDescriptor<API>(
202+
cache: WeakMap<{} | Function, WeakMap<{}, Array<UnloadedDescriptor<API>>>>,
203+
desc: UnloadedDescriptor<API>,
203204
) {
204205
const { value, options = DEFAULT_OPTIONS } = desc;
205206
if (options === false) return desc;
@@ -235,7 +236,7 @@ function* createPresetDescriptors(
235236
dirname: string,
236237
alias: string,
237238
passPerPreset: boolean,
238-
): Handler<Array<UnloadedDescriptor>> {
239+
): Handler<Array<UnloadedDescriptor<PresetAPI>>> {
239240
return yield* createDescriptors(
240241
"preset",
241242
items,
@@ -249,17 +250,17 @@ function* createPluginDescriptors(
249250
items: PluginList,
250251
dirname: string,
251252
alias: string,
252-
): Handler<Array<UnloadedDescriptor>> {
253+
): Handler<Array<UnloadedDescriptor<PluginAPI>>> {
253254
return yield* createDescriptors("plugin", items, dirname, alias);
254255
}
255256

256-
function* createDescriptors(
257+
function* createDescriptors<API>(
257258
type: "plugin" | "preset",
258259
items: PluginList,
259260
dirname: string,
260261
alias: string,
261262
ownPass?: boolean,
262-
): Handler<Array<UnloadedDescriptor>> {
263+
): Handler<Array<UnloadedDescriptor<API>>> {
263264
const descriptors = yield* gensync.all(
264265
items.map((item, index) =>
265266
createDescriptor(item, dirname, {
@@ -278,7 +279,7 @@ function* createDescriptors(
278279
/**
279280
* Given a plugin/preset item, resolve it into a standard format.
280281
*/
281-
export function* createDescriptor(
282+
export function* createDescriptor<API>(
282283
pair: PluginItem,
283284
dirname: string,
284285
{
@@ -290,7 +291,7 @@ export function* createDescriptor(
290291
alias: string;
291292
ownPass?: boolean;
292293
},
293-
): Handler<UnloadedDescriptor> {
294+
): Handler<UnloadedDescriptor<API>> {
294295
const desc = getItemDescriptor(pair);
295296
if (desc) {
296297
return desc;
@@ -365,7 +366,7 @@ export function* createDescriptor(
365366
};
366367
}
367368

368-
function assertNoDuplicates(items: Array<UnloadedDescriptor>): void {
369+
function assertNoDuplicates<API>(items: Array<UnloadedDescriptor<API>>): void {
369370
const map = new Map();
370371

371372
for (const item of items) {

packages/babel-core/src/config/full.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,22 @@ export default gensync(function* loadFullConfig(
8989

9090
const presetsDescriptors = presets.map(toDescriptor);
9191
const initialPluginsDescriptors = plugins.map(toDescriptor);
92-
const pluginDescriptorsByPass: Array<Array<UnloadedDescriptor>> = [[]];
92+
const pluginDescriptorsByPass: Array<Array<UnloadedDescriptor<PluginAPI>>> = [
93+
[],
94+
];
9395
const passes: Array<Array<Plugin>> = [];
9496

9597
const externalDependencies: DeepArray<string> = [];
9698

9799
const ignored = yield* enhanceError(
98100
context,
99101
function* recursePresetDescriptors(
100-
rawPresets: Array<UnloadedDescriptor>,
101-
pluginDescriptorsPass: Array<UnloadedDescriptor>,
102+
rawPresets: Array<UnloadedDescriptor<PresetAPI>>,
103+
pluginDescriptorsPass: Array<UnloadedDescriptor<PluginAPI>>,
102104
): Handler<true | void> {
103105
const presets: Array<{
104106
preset: ConfigChain | null;
105-
pass: Array<UnloadedDescriptor>;
107+
pass: Array<UnloadedDescriptor<PluginAPI>>;
106108
}> = [];
107109

108110
for (let i = 0; i < rawPresets.length; i++) {
@@ -178,7 +180,7 @@ export default gensync(function* loadFullConfig(
178180
passes.push(pass);
179181

180182
for (let i = 0; i < descs.length; i++) {
181-
const descriptor: UnloadedDescriptor = descs[i];
183+
const descriptor = descs[i];
182184
if (descriptor.options !== false) {
183185
try {
184186
// eslint-disable-next-line no-var
@@ -240,7 +242,7 @@ const makeDescriptorLoader = <Context, API>(
240242
) => API,
241243
) =>
242244
makeWeakCache(function* (
243-
{ value, options, dirname, alias }: UnloadedDescriptor,
245+
{ value, options, dirname, alias }: UnloadedDescriptor<API>,
244246
cache: CacheConfigurator<Context>,
245247
): Handler<LoadedDescriptor> {
246248
// Disabled presets should already have been filtered out
@@ -250,10 +252,10 @@ const makeDescriptorLoader = <Context, API>(
250252

251253
const externalDependencies: Array<string> = [];
252254

253-
let item = value;
255+
let item: unknown = value;
254256
if (typeof value === "function") {
255257
const factory = maybeAsync(
256-
value,
258+
value as (api: API, options: {}, dirname: string) => unknown,
257259
`You appear to be using an async plugin/preset, but Babel has been called synchronously`,
258260
);
259261

@@ -344,7 +346,7 @@ const instantiatePlugin = makeWeakCache(function* (
344346
}
345347

346348
if (plugin.inherits) {
347-
const inheritsDescriptor: UnloadedDescriptor = {
349+
const inheritsDescriptor: UnloadedDescriptor<PluginAPI> = {
348350
name: undefined,
349351
alias: `${alias}$inherits`,
350352
value: plugin.inherits,
@@ -387,7 +389,7 @@ const instantiatePlugin = makeWeakCache(function* (
387389
* Instantiate a plugin for the given descriptor, returning the plugin/options pair.
388390
*/
389391
function* loadPluginDescriptor(
390-
descriptor: UnloadedDescriptor,
392+
descriptor: UnloadedDescriptor<PluginAPI>,
391393
context: Context.SimplePlugin,
392394
): Handler<Plugin> {
393395
if (descriptor.value instanceof Plugin) {
@@ -410,7 +412,7 @@ const needsFilename = (val: unknown) => val && typeof val !== "function";
410412

411413
const validateIfOptionNeedsFilename = (
412414
options: ValidatedOptions,
413-
descriptor: UnloadedDescriptor,
415+
descriptor: UnloadedDescriptor<PresetAPI>,
414416
): void => {
415417
if (
416418
needsFilename(options.test) ||
@@ -435,7 +437,7 @@ const validateIfOptionNeedsFilename = (
435437
const validatePreset = (
436438
preset: PresetInstance,
437439
context: ConfigContext,
438-
descriptor: UnloadedDescriptor,
440+
descriptor: UnloadedDescriptor<PresetAPI>,
439441
): void => {
440442
if (!context.filename) {
441443
const { options } = preset;
@@ -466,7 +468,7 @@ const instantiatePreset = makeWeakCacheSync(
466468
* Generate a config object that will act as the root of a new nested config.
467469
*/
468470
function* loadPresetDescriptor(
469-
descriptor: UnloadedDescriptor,
471+
descriptor: UnloadedDescriptor<PresetAPI>,
470472
context: Context.FullPreset,
471473
): Handler<{
472474
chain: ConfigChain | null;

packages/babel-core/src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const createConfigItemAsync = createConfigItemRunner.async;
7373
export function createConfigItem(
7474
target: PluginTarget,
7575
options: Parameters<typeof createConfigItemImpl>[1],
76-
callback?: (err: Error, val: ConfigItem | null) => void,
76+
callback?: (err: Error, val: ConfigItem<PluginAPI> | null) => void,
7777
) {
7878
if (callback !== undefined) {
7979
createConfigItemRunner.errback(target, options, callback);

0 commit comments

Comments
 (0)