Skip to content

Commit 18d88b8

Browse files
authored
Improve @babel/core typings (#17471)
1 parent 41d9651 commit 18d88b8

File tree

30 files changed

+180
-110
lines changed

30 files changed

+180
-110
lines changed

Makefile.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

benchmark/babel-parser/real-case-ts/bench.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { copyFileSync, readFileSync, rmSync } from "node:fs";
2-
// eslint-disable-next-line import/no-extraneous-dependencies
32
import { commonJS } from "$repo-utils";
43
import { Benchmark } from "../../util.mjs";
54

benchmark/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"type": "module",
55
"devDependencies": {
6+
"$repo-utils": "link:../scripts/repo-utils",
67
"@babel-baseline/cli": "npm:@babel/[email protected]",
78
"@babel-baseline/core": "npm:@babel/[email protected]",
89
"@babel-baseline/generator": "npm:@babel/[email protected]",

eslint.config.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ export default [
362362
"unicorn/prefer-includes": "off",
363363
},
364364
},
365+
{
366+
files: ["packages/babel-helpers/scripts/**.{js,ts}"],
367+
rules: {
368+
"import/no-extraneous-dependencies": [
369+
"error",
370+
{ packageDir: ["./packages/babel-helpers", "./"] },
371+
],
372+
},
373+
},
365374
{
366375
files: ["packages/babel-traverse/scripts/**/*.js"],
367376
rules: {
@@ -389,10 +398,19 @@ export default [
389398
],
390399
},
391400
},
401+
{
402+
files: ["packages/babel-types/scripts/**/*.js"],
403+
rules: {
404+
"import/no-extraneous-dependencies": [
405+
"error",
406+
{ packageDir: ["./packages/babel-types", "./"] },
407+
],
408+
},
409+
},
392410
{
393411
files: ["scripts/**/*.{js,cjs,mjs}"],
394412
rules: {
395-
"import/no-extraneous-dependencies": ["error", { packageDir: "." }],
413+
"import/no-extraneous-dependencies": ["error", { packageDir: "./" }],
396414
},
397415
},
398416
{

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"rollup-plugin-polyfill-node": "^0.13.0",
8484
"semver": "^6.3.1",
8585
"shelljs": "^0.8.5",
86+
"terser": "^5.43.1",
8687
"test262-stream": "^1.4.0",
8788
"typescript": "5.9.2",
8889
"typescript-eslint": "8.39.1"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ export type { CacheConfigurator };
1313

1414
export type SimpleCacheConfigurator = {
1515
(forever: boolean): void;
16-
<T>(handler: () => T): T;
16+
<T extends SimpleType>(handler: () => T): T;
1717

1818
forever: () => void;
1919
never: () => void;
20-
using: <T>(handler: () => T) => T;
21-
invalidate: <T>(handler: () => T) => T;
20+
using: <T extends SimpleType>(handler: () => T) => T;
21+
invalidate: <T extends SimpleType>(handler: () => T) => T;
2222
};
2323

2424
export type CacheEntry<ResultT, SideChannel> = Array<{

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
ValidatedOptions,
1717
PluginList,
1818
PluginItem,
19+
PluginOptions,
1920
} from "./validation/options.ts";
2021

2122
import { resolveBrowserslistConfigFile } from "./resolve-targets.ts";
@@ -301,9 +302,8 @@ export function* createDescriptor<API>(
301302
}
302303

303304
let name;
304-
let options;
305-
// todo(flow->ts) better type annotation
306-
let value: any = pair;
305+
let options: PluginOptions;
306+
let value = pair;
307307
if (Array.isArray(value)) {
308308
if (value.length === 3) {
309309
[value, options, name] = value;
@@ -323,6 +323,7 @@ export function* createDescriptor<API>(
323323
const resolver = type === "plugin" ? loadPlugin : loadPreset;
324324
const request = value;
325325

326+
// @ts-expect-error value must be a PluginItem
326327
({ filepath, value } = yield* resolver(value, dirname));
327328

328329
file = {
@@ -335,8 +336,11 @@ export function* createDescriptor<API>(
335336
throw new Error(`Unexpected falsy value: ${String(value)}`);
336337
}
337338

339+
// @ts-expect-error Handle transpiled ES6 modules.
338340
if (typeof value === "object" && value.__esModule) {
341+
// @ts-expect-error Handle transpiled ES6 modules.
339342
if (value.default) {
343+
// @ts-expect-error Handle transpiled ES6 modules.
340344
value = value.default;
341345
} else {
342346
throw new Error("Must export a default export when using ES6 modules.");

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
validate,
2626
checkNoUnwrappedItemOptionPairs,
2727
} from "./validation/options.ts";
28-
import type { InputOptions, PluginItem } from "./validation/options.ts";
28+
import type {
29+
InputOptions,
30+
NormalizedOptions,
31+
PluginItem,
32+
} from "./validation/options.ts";
2933
import { validatePluginObject } from "./validation/plugins.ts";
3034
import { makePluginAPI, makePresetAPI } from "./helpers/config-api.ts";
3135
import type { PluginAPI, PresetAPI } from "./helpers/config-api.ts";
@@ -47,7 +51,7 @@ type LoadedDescriptor = {
4751
export type { InputOptions } from "./validation/options.ts";
4852

4953
export type ResolvedConfig = {
50-
options: any;
54+
options: NormalizedOptions;
5155
passes: PluginPasses;
5256
externalDependencies: ReadonlyDeepArray<string>;
5357
};
@@ -168,7 +172,9 @@ export default gensync(function* loadFullConfig(
168172

169173
if (ignored) return null;
170174

171-
const opts: ValidatedOptions = optionDefaults;
175+
// Cast the `optionDefaults` to NormalizedOptions,
176+
// since we will merge the normalized `options` to `optionDefaults`
177+
const opts = optionDefaults as NormalizedOptions;
172178
mergeOptions(opts, options);
173179

174180
const pluginContext: Context.FullPlugin = {

packages/babel-core/src/config/helpers/config-api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import type {
99
SimpleType,
1010
} from "../caching.ts";
1111

12-
import type { AssumptionName, CallerMetadata } from "../validation/options.ts";
12+
import type {
13+
AssumptionName,
14+
CallerMetadata,
15+
InputOptions,
16+
} from "../validation/options.ts";
1317

1418
import type * as Context from "../cache-contexts";
1519

20+
type EnvName = NonNullable<InputOptions["envName"]>;
1621
type EnvFunction = {
1722
(): string;
18-
<T>(extractor: (babelEnv: string) => T): T;
23+
<T extends SimpleType>(extractor: (envName: EnvName) => T): T;
1924
(envVar: string): boolean;
2025
(envVars: Array<string>): boolean;
2126
};

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export type {
77
Plugin,
88
} from "./full.ts";
99

10-
import type { InputOptions, PluginTarget } from "./validation/options.ts";
10+
import type {
11+
InputOptions,
12+
NormalizedOptions,
13+
PluginTarget,
14+
} from "./validation/options.ts";
1115
export type { ConfigAPI } from "./helpers/config-api.ts";
1216
import type {
1317
PluginAPI as basePluginAPI,
@@ -24,7 +28,7 @@ export type {
2428
ValidatedOptions as PresetObject,
2529
} from "./validation/options.ts";
2630

27-
import loadFullConfig, { type ResolvedConfig } from "./full.ts";
31+
import loadFullConfig from "./full.ts";
2832
import {
2933
type PartialConfig,
3034
loadPartialConfig as loadPartialConfigImpl,
@@ -72,7 +76,9 @@ export function loadPartialConfig(
7276
}
7377
}
7478

75-
function* loadOptionsImpl(opts: InputOptions): Handler<ResolvedConfig | null> {
79+
function* loadOptionsImpl(
80+
opts: InputOptions,
81+
): Handler<NormalizedOptions | null> {
7682
const config = yield* loadFullConfig(opts);
7783
// NOTE: We want to return "null" explicitly, while ?. alone returns undefined
7884
return config?.options ?? null;
@@ -90,14 +96,14 @@ export function loadOptionsSync(
9096
}
9197
export function loadOptions(
9298
opts: Parameters<typeof loadOptionsImpl>[0],
93-
callback?: (err: Error, val: ResolvedConfig | null) => void,
99+
callback?: (err: Error, val: NormalizedOptions | null) => void,
94100
) {
95101
if (callback !== undefined) {
96102
beginHiddenCallStack(loadOptionsRunner.errback)(opts, callback);
97103
} else if (typeof opts === "function") {
98104
beginHiddenCallStack(loadOptionsRunner.errback)(
99105
undefined,
100-
opts as (err: Error, val: ResolvedConfig | null) => void,
106+
opts as (err: Error, val: NormalizedOptions | null) => void,
101107
);
102108
} else {
103109
if (process.env.BABEL_8_BREAKING) {

0 commit comments

Comments
 (0)