Skip to content

Commit 47ad54a

Browse files
convert @babel/preset-env to typescript (#13217)
* babel-preset-env flowts rename * babel-preset-env flowts convert * babel-preset-env fixes * babel-preset-env * make generate-tsconfig * Minimize diff * Fix many type errors Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 22e5afc commit 47ad54a

16 files changed

Lines changed: 117 additions & 124 deletions

packages/babel-preset-env/src/available-plugins.js renamed to packages/babel-preset-env/src/available-plugins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
/* eslint sort-keys: "error" */
32

43
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
// @flow
1+
import { getInclusionReasons } from "@babel/helper-compilation-targets";
22

3-
import {
4-
getInclusionReasons,
5-
type Targets,
6-
} from "@babel/helper-compilation-targets";
3+
import type { Targets } from "@babel/helper-compilation-targets";
74

85
// Outputs a message that shows which target(s) caused an item to be included:
96
// transform-foo { "edge":"13", "firefox":"49", "ie":"10" }

packages/babel-preset-env/src/filter-items.js renamed to packages/babel-preset-env/src/filter-items.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
import { lt } from "semver";
42
import { minVersions } from "./available-plugins";
53

packages/babel-preset-env/src/get-option-specific-excludes.js renamed to packages/babel-preset-env/src/get-option-specific-excludes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
const defaultExcludesForLooseMode = ["transform-typeof-symbol"];
42

53
export default function ({ loose }: { loose: boolean }): null | string[] {
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//@flow
2-
3-
import { SemVer, lt } from "semver";
1+
import { lt } from "semver";
2+
import type { SemVer } from "semver";
43
import { logPlugin } from "./debug";
54
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
65
import { removeUnnecessaryItems, removeUnsupportedItems } from "./filter-items";
@@ -27,13 +26,13 @@ import getTargets, {
2726
prettifyTargets,
2827
filterItems,
2928
isRequired,
30-
type Targets,
31-
type InputTargets,
3229
} from "@babel/helper-compilation-targets";
30+
import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
3331
import availablePlugins from "./available-plugins";
3432
import { declare } from "@babel/helper-plugin-utils";
3533

36-
import typeof ModuleTransformationsType from "./module-transformations";
34+
type ModuleTransformationsType =
35+
typeof import("./module-transformations").default;
3736
import type { BuiltInsOption, ModuleOption } from "./types";
3837

3938
// TODO: Remove in Babel 8
@@ -92,7 +91,7 @@ const getPlugin = (pluginName: string) => {
9291
return plugin;
9392
};
9493

95-
export const transformIncludesAndExcludes = (opts: Array<string>): Object => {
94+
export const transformIncludesAndExcludes = (opts: Array<string>): any => {
9695
return opts.reduce(
9796
(result, opt) => {
9897
const target = opt.match(/^(es|es6|es7|esnext|web)\./)
@@ -116,14 +115,14 @@ export const getModulesPluginNames = ({
116115
shouldTransformDynamicImport,
117116
shouldTransformExportNamespaceFrom,
118117
shouldParseTopLevelAwait,
119-
}: {|
120-
modules: ModuleOption,
121-
transformations: ModuleTransformationsType,
122-
shouldTransformESM: boolean,
123-
shouldTransformDynamicImport: boolean,
124-
shouldTransformExportNamespaceFrom: boolean,
125-
shouldParseTopLevelAwait: boolean,
126-
|}) => {
118+
}: {
119+
modules: ModuleOption;
120+
transformations: ModuleTransformationsType;
121+
shouldTransformESM: boolean;
122+
shouldTransformDynamicImport: boolean;
123+
shouldTransformExportNamespaceFrom: boolean;
124+
shouldParseTopLevelAwait: boolean;
125+
}) => {
127126
const modulesPluginNames = [];
128127
if (modules !== false && transformations[modules]) {
129128
if (shouldTransformESM) {
@@ -173,15 +172,15 @@ export const getPolyfillPlugins = ({
173172
regenerator,
174173
debug,
175174
}: {
176-
useBuiltIns: BuiltInsOption,
177-
corejs: typeof SemVer | null | false,
178-
polyfillTargets: Targets,
179-
include: Set<string>,
180-
exclude: Set<string>,
181-
proposals: boolean,
182-
shippedProposals: boolean,
183-
regenerator: boolean,
184-
debug: boolean,
175+
useBuiltIns: BuiltInsOption;
176+
corejs: SemVer | null | false;
177+
polyfillTargets: Targets;
178+
include: Set<string>;
179+
exclude: Set<string>;
180+
proposals: boolean;
181+
shippedProposals: boolean;
182+
regenerator: boolean;
183+
debug: boolean;
185184
}) => {
186185
const polyfillPlugins = [];
187186
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
@@ -249,7 +248,7 @@ function getLocalTargets(
249248
`);
250249
}
251250

252-
return getTargets((optionsTargets: InputTargets), {
251+
return getTargets(optionsTargets as InputTargets, {
253252
ignoreBrowserslistConfig,
254253
configPath,
255254
browserslistEnv,

packages/babel-preset-env/src/module-transformations.js renamed to packages/babel-preset-env/src/module-transformations.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// @flow
1+
type AvailablePlugins = typeof import("./available-plugins").default;
22

3-
import typeof AvailablePlugins from "./available-plugins";
4-
5-
export default ({
3+
export default {
64
auto: "transform-modules-commonjs",
75
amd: "transform-modules-amd",
86
commonjs: "transform-modules-commonjs",
97
cjs: "transform-modules-commonjs",
108
systemjs: "transform-modules-systemjs",
119
umd: "transform-modules-umd",
12-
}: { [transform: string]: $Keys<AvailablePlugins> });
10+
} as { [transform: string]: keyof AvailablePlugins };

packages/babel-preset-env/src/normalize-options.js renamed to packages/babel-preset-env/src/normalize-options.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// @flow
21
import corejs3Polyfills from "core-js-compat/data.json";
3-
import { coerce, SemVer } from "semver";
2+
import { coerce } from "semver";
3+
import type { SemVer } from "semver";
44
import corejs2Polyfills from "@babel/compat-data/corejs2-built-ins";
55
import { plugins as pluginsList } from "./plugins-compat-data";
66
import moduleTransformations from "./module-transformations";
@@ -22,6 +22,8 @@ import type {
2222
PluginListOption,
2323
} from "./types";
2424

25+
declare const PACKAGE_JSON: { name: string; version: string };
26+
2527
const v = new OptionValidator(PACKAGE_JSON.name);
2628

2729
const allPluginsList = Object.keys(pluginsList);
@@ -111,9 +113,10 @@ export const checkDuplicateIncludeExcludes = (
111113
);
112114
};
113115

114-
const normalizeTargets = (targets): $PropertyType<Options, "targets"> => {
116+
const normalizeTargets = (targets): Options["targets"] => {
115117
// TODO: Allow to use only query or strings as a targets from next breaking change.
116118
if (typeof targets === "string" || Array.isArray(targets)) {
119+
// @ts-expect-error
117120
return { browsers: targets };
118121
}
119122
return { ...targets };
@@ -150,12 +153,12 @@ export const validateUseBuiltInsOption = (
150153
};
151154

152155
export type NormalizedCorejsOption = {
153-
proposals: boolean,
154-
version: typeof SemVer | null | false,
156+
proposals: boolean;
157+
version: SemVer | null | false;
155158
};
156159

157160
export function normalizeCoreJSOption(
158-
corejs?: CorejsOption,
161+
corejs: CorejsOption | undefined | null,
159162
useBuiltIns: BuiltInsOption,
160163
): NormalizedCorejsOption {
161164
let proposals = false;
@@ -249,7 +252,7 @@ export default function normalizeOptions(opts: Options) {
249252
opts.ignoreBrowserslistConfig,
250253
false,
251254
),
252-
loose: v.validateBooleanOption(TopLevelOptions.loose, opts.loose),
255+
loose: v.validateBooleanOption<boolean>(TopLevelOptions.loose, opts.loose),
253256
modules: validateModulesOption(opts.modules),
254257
shippedProposals: v.validateBooleanOption(
255258
TopLevelOptions.shippedProposals,
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
export const TopLevelOptions = {
42
bugfixes: "bugfixes",
53
configPath: "configPath",
@@ -16,7 +14,7 @@ export const TopLevelOptions = {
1614
targets: "targets",
1715
useBuiltIns: "useBuiltIns",
1816
browserslistEnv: "browserslistEnv",
19-
};
17+
} as const;
2018

2119
export const ModulesOption = {
2220
false: false,
@@ -26,10 +24,10 @@ export const ModulesOption = {
2624
cjs: "cjs",
2725
systemjs: "systemjs",
2826
umd: "umd",
29-
};
27+
} as const;
3028

3129
export const UseBuiltInsOption = {
3230
false: false,
3331
entry: "entry",
3432
usage: "usage",
35-
};
33+
} as const;

packages/babel-preset-env/src/plugins-compat-data.js renamed to packages/babel-preset-env/src/plugins-compat-data.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
import plugins from "@babel/compat-data/plugins";
42
import bugfixPlugins from "@babel/compat-data/plugin-bugfixes";
53
import availablePlugins from "./available-plugins";

packages/babel-preset-env/src/polyfills/babel-polyfill.js renamed to packages/babel-preset-env/src/polyfills/babel-polyfill.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// @flow
2-
31
import { getImportSource, getRequireSource, isPolyfillSource } from "./utils";
42

53
import type { NodePath } from "@babel/traverse";
4+
import * as t from "@babel/types";
65

76
const BABEL_POLYFILL_DEPRECATION = `
87
\`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
@@ -19,7 +18,7 @@ export default function (
1918
return {
2019
name: "preset-env/replace-babel-polyfill",
2120
visitor: {
22-
ImportDeclaration(path: NodePath) {
21+
ImportDeclaration(path: NodePath<t.ImportDeclaration>) {
2322
const src = getImportSource(path);
2423
if (usage && isPolyfillSource(src)) {
2524
// $FlowIgnore
@@ -40,7 +39,7 @@ export default function (
4039
}
4140
}
4241
},
43-
Program(path: NodePath) {
42+
Program(path: NodePath<t.Program>) {
4443
path.get("body").forEach(bodyPath => {
4544
const src = getRequireSource(bodyPath);
4645
if (usage && isPolyfillSource(src)) {

0 commit comments

Comments
 (0)