convert @babel/helper-compilation-targets to typescript (#13218)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Bogdan Savluk 2021-05-21 00:20:42 +02:00 committed by GitHub
parent 12190042e6
commit 8b1bcd1079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 58 additions and 56 deletions

View File

@ -216,3 +216,7 @@ declare module "@babel/helper-module-transforms" {
declare module "@babel/plugin-transform-classes" {
declare module.exports: any;
}
declare module "@babel/helper-compilation-targets" {
declare module.exports: any;
}

View File

@ -30,6 +30,8 @@
"@babel/core": "^7.0.0"
},
"devDependencies": {
"@babel/core": "workspace:*"
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*",
"@types/semver": "^5.5.0"
}
}

View File

@ -1,5 +1,3 @@
// @flow
import semver from "semver";
import { prettifyVersion } from "./pretty";
import {
@ -7,16 +5,16 @@ import {
isUnreleasedVersion,
getLowestImplementedVersion,
} from "./utils";
import type { Targets } from "./types";
import type { Target, Targets } from "./types";
export function getInclusionReasons(
item: string,
targetVersions: Targets,
list: { [key: string]: Targets },
) {
const minVersions = list[item] || {};
const minVersions = list[item] || ({} as Targets);
return Object.keys(targetVersions).reduce((result, env) => {
return (Object.keys(targetVersions) as Target[]).reduce((result, env) => {
const minVersion = getLowestImplementedVersion(minVersions, env);
const targetVersion = targetVersions[env];

View File

@ -1,5 +1,3 @@
// @flow
import semver from "semver";
import pluginsCompatData from "@babel/compat-data/plugins";
@ -12,7 +10,7 @@ import {
} from "./utils";
export function targetsSupported(target: Targets, support: Targets) {
const targetEnvironments = Object.keys(target);
const targetEnvironments = Object.keys(target) as Array<keyof Targets>;
if (targetEnvironments.length === 0) {
return false;
@ -65,9 +63,9 @@ export function isRequired(
includes,
excludes,
}: {
compatData?: { [feature: string]: Targets },
includes?: Set<string>,
excludes?: Set<string>,
compatData?: { [feature: string]: Targets };
includes?: Set<string>;
excludes?: Set<string>;
} = {},
) {
if (excludes?.has(name)) return false;

View File

@ -1,5 +1,3 @@
// @flow
import browserslist from "browserslist";
import { findSuggestion } from "@babel/helper-validator-option";
import browserModulesData from "@babel/compat-data/native-modules";
@ -26,6 +24,7 @@ export { TargetNames };
const ESM_SUPPORT = browserModulesData["es6.module"];
declare const PACKAGE_JSON: { name: string; version: string };
const v = new OptionValidator(PACKAGE_JSON.name);
function validateTargetNames(targets: Targets): TargetsTuple {
@ -39,17 +38,17 @@ function validateTargetNames(targets: Targets): TargetsTuple {
}
}
return (targets: any);
return targets as any;
}
export function isBrowsersQueryValid(browsers: mixed): boolean %checks {
export function isBrowsersQueryValid(browsers: unknown): boolean {
return (
typeof browsers === "string" ||
(Array.isArray(browsers) && browsers.every(b => typeof b === "string"))
);
}
function validateBrowsers(browsers: Browsers | void) {
function validateBrowsers(browsers: Browsers | undefined) {
v.invariant(
browsers === undefined || isBrowsersQueryValid(browsers),
`'${String(browsers)}' is not a valid browserslist query`,
@ -59,7 +58,7 @@ function validateBrowsers(browsers: Browsers | void) {
}
function getLowestVersions(browsers: Array<string>): Targets {
return browsers.reduce((all: Object, browser: string): Object => {
return browsers.reduce((all: any, browser: string): any => {
const [browserName, browserVersion] = browser.split(" ");
const normalizedBrowserName = browserNameMap[browserName];
@ -102,7 +101,7 @@ function getLowestVersions(browsers: Array<string>): Targets {
}
function outputDecimalWarning(
decimalTargets: Array<{| target: string, value: string |}>,
decimalTargets: Array<{ target: string; value: string }>,
): void {
if (!decimalTargets.length) {
return;
@ -152,7 +151,7 @@ function generateTargets(inputTargets: InputTargets): Targets {
const input = { ...inputTargets };
delete input.esmodules;
delete input.browsers;
return ((input: any): Targets);
return input as any as Targets;
}
function resolveTargets(queries: Browsers): Targets {
@ -162,20 +161,17 @@ function resolveTargets(queries: Browsers): Targets {
type GetTargetsOption = {
// This is not the path of the config file, but the path where start searching it from
configPath?: string,
configPath?: string;
// The path of the config file
configFile?: string,
configFile?: string;
// The env to pass to browserslist
browserslistEnv?: string,
browserslistEnv?: string;
// true to disable config loading
ignoreBrowserslistConfig?: boolean,
ignoreBrowserslistConfig?: boolean;
};
export default function getTargets(
inputTargets: InputTargets = {},
inputTargets: InputTargets = {} as InputTargets,
options: GetTargetsOption = {},
): Targets {
let { browsers, esmodules } = inputTargets;
@ -243,7 +239,7 @@ export default function getTargets(
}
// Parse remaining targets
const result: Targets = {};
const result: Targets = {} as Targets;
const decimalWarnings = [];
for (const target of Object.keys(targets).sort()) {
const value = targets[target];

View File

@ -1,5 +1,3 @@
// @flow
export const TargetNames = {
node: "node",
chrome: "chrome",

View File

@ -1,5 +1,6 @@
import semver from "semver";
import { unreleasedLabels } from "./targets";
import type { Targets } from "./types";
export function prettifyVersion(version: string) {
if (typeof version !== "string") {
@ -32,5 +33,5 @@ export function prettifyTargets(targets: Targets): Targets {
results[target] = value;
return results;
}, {});
}, {} as Targets);
}

View File

@ -1,5 +1,3 @@
// @flow
// Targets
export type Target =
| "node"
@ -15,24 +13,21 @@ export type Target =
| "samsung";
export type Targets = {
[target: Target]: string,
[target in Target]: string;
};
export type TargetsTuple = {|
[target: Target]: string,
|};
export type TargetsTuple = {
[target in Target]: string;
};
export type Browsers = string | $ReadOnlyArray<string>;
export type Browsers = string | ReadonlyArray<string>;
export type InputTargets = {
...Targets,
browsers?: Browsers,
browsers?: Browsers;
// When `true`, this completely replaces the `browsers` option.
// When `intersect`, this is intersected with the `browsers`
// option (giving the higher browsers as the result).
// TODO(Babel 8): Make `true` behave like `intersect` and
// remove `intersect`.
esmodules?: boolean | "intersect",
};
esmodules?: boolean | "intersect";
} & Targets;

View File

@ -1,14 +1,18 @@
// @flow
import semver from "semver";
import { OptionValidator } from "@babel/helper-validator-option";
import { unreleasedLabels } from "./targets";
import type { Target, Targets } from "./types";
declare const PACKAGE_JSON: { name: string; version: string };
const versionRegExp = /^(\d+|\d+.\d+)$/;
const v = new OptionValidator(PACKAGE_JSON.name);
export function semverMin(first: ?string, second: string): string {
export function semverMin(
first: string | undefined | null,
second: string,
): string {
return first && semver.lt(first, second) ? first : second;
}
@ -46,6 +50,7 @@ export function getLowestUnreleased(a: string, b: string, env: string): string {
const unreleasedLabel = unreleasedLabels[env];
const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
if (hasUnreleased) {
// @ts-expect-error todo(flow->ts): probably a bug - types of a hasUnreleased to not overlap
return a === hasUnreleased ? b : a || b;
}
return semverMin(a, b);

View File

@ -27,7 +27,6 @@ export const logPlugin = (
if (!first) formattedTargets += `,`;
first = false;
formattedTargets += ` ${target}`;
// $FlowIgnore
if (support[target]) formattedTargets += ` < ${support[target]}`;
}
formattedTargets += ` }`;

View File

@ -249,11 +249,11 @@ function getLocalTargets(
`);
}
return getTargets(
// $FlowIgnore optionsTargets doesn't have an "uglify" property anymore
(optionsTargets: InputTargets),
{ ignoreBrowserslistConfig, configPath, browserslistEnv },
);
return getTargets((optionsTargets: InputTargets), {
ignoreBrowserslistConfig,
configPath,
browserslistEnv,
});
}
function supportsStaticESM(caller) {

View File

@ -111,7 +111,7 @@ export const checkDuplicateIncludeExcludes = (
);
};
const normalizeTargets = targets => {
const normalizeTargets = (targets): $PropertyType<Options, "targets"> => {
// TODO: Allow to use only query or strings as a targets from next breaking change.
if (typeof targets === "string" || Array.isArray(targets)) {
return { browsers: targets };

View File

@ -32,7 +32,7 @@ export type Options = {
modules: ModuleOption,
shippedProposals: boolean,
spec: boolean,
targets: { ...InputTargets, uglify?: boolean },
targets: { ...InputTargets, uglify?: boolean, esmodules?: boolean },
useBuiltIns: BuiltInsOption,
browserslistEnv: string,
};

View File

@ -6,6 +6,7 @@
"./packages/babel-core/src/**/*.ts",
"./packages/babel-generator/src/**/*.ts",
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
"./packages/babel-helper-compilation-targets/src/**/*.ts",
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
"./packages/babel-helper-fixtures/src/**/*.ts",
@ -49,6 +50,9 @@
"@babel/helper-annotate-as-pure": [
"./packages/babel-helper-annotate-as-pure/src"
],
"@babel/helper-compilation-targets": [
"./packages/babel-helper-compilation-targets/src"
],
"@babel/helper-create-regexp-features-plugin": [
"./packages/babel-helper-create-regexp-features-plugin/src"
],

View File

@ -410,7 +410,9 @@ __metadata:
dependencies:
"@babel/compat-data": "workspace:^7.13.15"
"@babel/core": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-validator-option": "workspace:^7.12.17"
"@types/semver": ^5.5.0
browserslist: ^4.14.5
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
@ -4243,7 +4245,7 @@ __metadata:
languageName: node
linkType: hard
"@types/semver@npm:^5.4.0":
"@types/semver@npm:^5.4.0, @types/semver@npm:^5.5.0":
version: 5.5.0
resolution: "@types/semver@npm:5.5.0"
checksum: df74589466e171c36dd868b760609e518830f212134c238674ddd6eb83653368c59f4510aa6523b7692ec99c5d8ab40b818e30f9d65e0df97c56bdbacef06661