Skip to content

Commit c90add7

Browse files
Provide plugin/preset typings from plugin-utils (#14499)
Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent 51c8c02 commit c90add7

99 files changed

Lines changed: 742 additions & 294 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"semver": "^6.3.0",
7575
"test262-stream": "^1.4.0",
7676
"through2": "^4.0.0",
77-
"typescript": "~4.5.0"
77+
"typescript": "~4.6.3"
7878
},
7979
"workspaces": [
8080
"codemods/*",

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

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

12-
import type { CallerMetadata } from "../validation/options";
12+
import type { AssumptionName, CallerMetadata } from "../validation/options";
1313

1414
import * as Context from "../cache-contexts";
1515

@@ -24,7 +24,7 @@ type CallerFactory = (
2424
extractor: (callerMetadata: CallerMetadata | void) => unknown,
2525
) => SimpleType;
2626
type TargetsFunction = () => Targets;
27-
type AssumptionFunction = (name: string) => boolean | void;
27+
type AssumptionFunction = (name: AssumptionName) => boolean | void;
2828

2929
export type ConfigAPI = {
3030
version: string;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ export type {
99

1010
import type { PluginTarget } from "./validation/options";
1111

12+
import type {
13+
PluginAPI as basePluginAPI,
14+
PresetAPI as basePresetAPI,
15+
} from "./helpers/config-api";
16+
export type { PluginObject } from "./validation/plugins";
17+
type PluginAPI = basePluginAPI & typeof import("..");
18+
type PresetAPI = basePresetAPI & typeof import("..");
19+
export type { PluginAPI, PresetAPI };
20+
// todo: may need to refine PresetObject to be a subset of ValidatedOptions
21+
export type { ValidatedOptions as PresetObject } from "./validation/options";
22+
1223
import loadFullConfig from "./full";
1324
import { loadPartialConfig as loadPartialConfigRunner } from "./partial";
1425

packages/babel-core/src/config/validation/option-assertions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
CallerMetadata,
2121
RootMode,
2222
TargetsListOrObject,
23+
AssumptionName,
2324
} from "./options";
2425

2526
import { assumptionsNames } from "./options";
@@ -462,7 +463,7 @@ export function assertAssumptions(
462463

463464
for (const name of Object.keys(value)) {
464465
const subLoc = access(loc, name);
465-
if (!assumptionsNames.has(name)) {
466+
if (!assumptionsNames.has(name as AssumptionName)) {
466467
throw new Error(`${msg(subLoc)} is not a supported assumption.`);
467468
}
468469
if (typeof value[name] !== "boolean") {

packages/babel-core/src/config/validation/options.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
} from "./option-assertions";
2929
import type { ValidatorSet, Validator, OptionPath } from "./option-assertions";
3030
import type { UnloadedDescriptor } from "../config-descriptors";
31+
import type { ParserOptions } from "@babel/parser";
32+
import type { GeneratorOptions } from "@babel/generator";
3133

3234
const ROOT_VALIDATORS: ValidatorSet = {
3335
cwd: assertString as Validator<ValidatedOptions["cwd"]>,
@@ -181,9 +183,9 @@ export type ValidatedOptions = {
181183
sourceFileName?: string;
182184
sourceRoot?: string;
183185
// Deprecate top level parserOpts
184-
parserOpts?: {};
186+
parserOpts?: ParserOptions;
185187
// Deprecate top level generatorOpts
186-
generatorOpts?: {};
188+
generatorOpts?: GeneratorOptions;
187189
};
188190

189191
export type NormalizedOptions = {
@@ -254,7 +256,7 @@ type EnvPath = Readonly<{
254256

255257
export type NestingPath = RootPath | OverridesPath | EnvPath;
256258

257-
export const assumptionsNames = new Set<string>([
259+
const knownAssumptions = [
258260
"arrayLikeIsIterable",
259261
"constantReexports",
260262
"constantSuper",
@@ -276,7 +278,9 @@ export const assumptionsNames = new Set<string>([
276278
"setSpreadProperties",
277279
"skipForOfIteratorClosing",
278280
"superIsCallableConstructor",
279-
]);
281+
] as const;
282+
export type AssumptionName = typeof knownAssumptions[number];
283+
export const assumptionsNames = new Set(knownAssumptions);
280284

281285
function getSource(loc: NestingPath): OptionsSource {
282286
return loc.type === "root" ? loc.source : getSource(loc.parent);

packages/babel-core/src/config/validation/plugins.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import type {
1111
OptionPath,
1212
RootPath,
1313
} from "./option-assertions";
14+
import type { ParserOptions } from "@babel/parser";
15+
import type { Visitor } from "@babel/traverse";
16+
import type PluginPass from "../../transformation/plugin-pass";
17+
import type { ValidatedOptions } from "./options";
1418

1519
// Note: The casts here are just meant to be static assertions to make sure
1620
// that the assertion functions actually assert that the value's type matches
@@ -31,7 +35,7 @@ const VALIDATORS: ValidatorSet = {
3135
>,
3236
};
3337

34-
function assertVisitorMap(loc: OptionPath, value: unknown): VisitorMap {
38+
function assertVisitorMap(loc: OptionPath, value: unknown): Visitor {
3539
const obj = assertObject(loc, value);
3640
if (obj) {
3741
Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop]));
@@ -45,7 +49,7 @@ function assertVisitorMap(loc: OptionPath, value: unknown): VisitorMap {
4549
);
4650
}
4751
}
48-
return obj as VisitorMap;
52+
return obj as Visitor;
4953
}
5054

5155
function assertVisitorHandler(
@@ -74,17 +78,16 @@ type VisitorHandler =
7478
exit?: Function;
7579
};
7680

77-
export type VisitorMap = {
78-
[x: string]: VisitorHandler;
79-
};
80-
81-
export type PluginObject = {
81+
export type PluginObject<S = PluginPass> = {
8282
name?: string;
83-
manipulateOptions?: (options: unknown, parserOpts: unknown) => void;
83+
manipulateOptions?: (
84+
options: ValidatedOptions,
85+
parserOpts: ParserOptions,
86+
) => void;
8487
pre?: Function;
8588
post?: Function;
8689
inherits?: Function;
87-
visitor?: VisitorMap;
90+
visitor?: Visitor<S>;
8891
parserOverride?: Function;
8992
generatorOverride?: Function;
9093
};

packages/babel-core/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export {
2929
loadOptionsAsync,
3030
} from "./config";
3131

32+
export type {
33+
PluginAPI,
34+
PluginObject,
35+
PresetAPI,
36+
PresetObject,
37+
} from "./config";
38+
3239
export { transform, transformSync, transformAsync } from "./transform";
3340
export {
3441
transformFile,

packages/babel-generator/src/generators/methods.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ function hasTypesOrComments(
146146
return !!(
147147
node.typeParameters ||
148148
node.returnType ||
149-
// @ts-expect-error
150149
node.predicate ||
151150
param.typeAnnotation ||
152151
param.optional ||

packages/babel-generator/src/index.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SourceMap from "./source-map";
22
import Printer from "./printer";
33
import type * as t from "@babel/types";
4-
4+
import type { Opts as jsescOptions } from "jsesc";
55
import type { Format } from "./printer";
66
import type { DecodedSourceMap, Mapping } from "@jridgewell/gen-mapping";
77

@@ -195,19 +195,12 @@ export interface GeneratorOptions {
195195
/**
196196
* Options for outputting jsesc representation.
197197
*/
198-
jsescOption?: {
199-
/**
200-
* The type of quote to use in the output. If omitted, autodetects based on `ast.tokens`.
201-
*/
202-
quotes?: "single" | "double";
203-
204-
/**
205-
* When enabled, the output is a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified through the quotes setting.
206-
* Defaults to `true`.
207-
*/
208-
wrap?: boolean;
209-
};
198+
jsescOption?: jsescOptions;
210199

200+
/**
201+
* For use with the recordAndTuple token.
202+
*/
203+
recordAndTupleSyntaxType?: "hash" | "bar";
211204
/**
212205
* For use with the Hack-style pipe operator.
213206
* Changes what token is used for pipe bodies’ topic references.

packages/babel-helper-create-class-features-plugin/src/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { types as t } from "@babel/core";
2-
import type { File } from "@babel/core";
2+
import type { File, PluginAPI, PluginObject } from "@babel/core";
33
import type { NodePath } from "@babel/traverse";
44
import nameFunction from "@babel/helper-function-name";
55
import splitExportDeclaration from "@babel/helper-split-export-declaration";
@@ -14,7 +14,6 @@ import { buildDecoratedClass, hasDecorators } from "./decorators";
1414
import { injectInitialization, extractComputedKeys } from "./misc";
1515
import { enableFeature, FEATURES, isLoose, shouldTransform } from "./features";
1616
import { assertFieldTransformed } from "./typescript";
17-
import type { ParserOptions } from "@babel/parser";
1817

1918
export { FEATURES, enableFeature, injectInitialization };
2019

@@ -33,19 +32,17 @@ interface Options {
3332
name: string;
3433
feature: number;
3534
loose?: boolean;
36-
inherits?: (api: any, options: any) => any;
37-
// same as PluginObject.manipulateOptions
38-
manipulateOptions?: (options: unknown, parserOpts: ParserOptions) => void;
39-
// TODO(flow->ts): change to babel api
40-
api?: { assumption: (key?: string) => boolean | undefined };
35+
inherits?: PluginObject["inherits"];
36+
manipulateOptions?: PluginObject["manipulateOptions"];
37+
api?: PluginAPI;
4138
}
4239

4340
export function createClassFeaturePlugin({
4441
name,
4542
feature,
4643
loose,
4744
manipulateOptions,
48-
// TODO(Babel 8): Remove the default value
45+
// @ts-ignore TODO(Babel 8): Remove the default value
4946
api = { assumption: () => void 0 },
5047
inherits,
5148
}: Options) {
@@ -188,7 +185,7 @@ export function createClassFeaturePlugin({
188185
const privateNamesMap = buildPrivateNamesMap(props);
189186
const privateNamesNodes = buildPrivateNamesNodes(
190187
privateNamesMap,
191-
privateFieldsAsProperties ?? loose,
188+
(privateFieldsAsProperties ?? loose) as boolean,
192189
state,
193190
);
194191

@@ -227,9 +224,9 @@ export function createClassFeaturePlugin({
227224
props,
228225
privateNamesMap,
229226
state,
230-
setPublicClassFields ?? loose,
231-
privateFieldsAsProperties ?? loose,
232-
constantSuper ?? loose,
227+
(setPublicClassFields ?? loose) as boolean,
228+
(privateFieldsAsProperties ?? loose) as boolean,
229+
(constantSuper ?? loose) as boolean,
233230
innerBinding,
234231
));
235232
}

0 commit comments

Comments
 (0)