Skip to content

Commit

Permalink
Better flow coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 20, 2020
1 parent 86ed830 commit c207323
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .flowconfig
Expand Up @@ -10,4 +10,4 @@ include_warnings=true
suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore
suppress_type=$FlowIgnore
esproposal.optional_chaining=enable
module.name_mapper='^@babel\/plugin-inject-polyfills$' -> '<PROJECT_ROOT>/packages/babel-plugin-inject-polyfills/src/index'
module.name_mapper='^@babel/plugin-inject-polyfills$' -> '<PROJECT_ROOT>/packages/babel-plugin-inject-polyfills/src/index'
11 changes: 11 additions & 0 deletions packages/babel-plugin-inject-polyfills/src/define-provider.js
@@ -0,0 +1,11 @@
// @flow

import type { PolyfillProviderInternal, PolyfillProvider } from "./types";

export function defineProvider<Options>(
factory: PolyfillProvider<Options>,
): PolyfillProviderInternal<Options> {
// This will allow us to do some things

return (factory: any);
}
12 changes: 3 additions & 9 deletions packages/babel-plugin-inject-polyfills/src/index.js
Expand Up @@ -23,19 +23,13 @@ import {
} from "./debug-utils";
import { validateIncludeExclude } from "./normalize-options";

import type {
ProviderApi,
Options,
Targets,
MetaDescriptor,
PolyfillProvider,
Utils,
} from "./types";
import type { ProviderApi, Options, Targets, MetaDescriptor } from "./types";

import createMetaResolver from "./meta-resolver";

export { defineProvider } from "./define-provider";
export { resolveProvider } from "./config";
export type { PolyfillProvider, MetaDescriptor, Utils };
export type { PolyfillProvider, MetaDescriptor, Utils, Targets } from "./types";

export default declare((api, options: Options, dirname: string) => {
api.assertVersion(7);
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-inject-polyfills/src/types.js
Expand Up @@ -29,6 +29,8 @@ export type PolyfillProvider<Opts = {||}> = (
dirname: string,
) => ProviderResult;

export opaque type PolyfillProviderInternal<Opts> = PolyfillProvider<Opts>;

export type MethodString = "entry-global" | "usage-global" | "usage-pure";

export type Targets = {
Expand Down
@@ -1,3 +1,7 @@
// @flow

import type { Targets } from "@babel/plugin-inject-polyfills";

const webPolyfills = {
"web.timers": {},
"web.immediate": {},
Expand All @@ -10,7 +14,7 @@ const purePolyfills = {
"es7.string.at": {},
};

export default function(targets, method, polyfills) {
export default function(targets: Targets, method: string, polyfills: Object) {
const targetNames = Object.keys(targets);
const isAnyTarget = !targetNames.length;
const isWebTarget = targetNames.some(name => name !== "node");
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-polyfill-provider-corejs2/src/index.js
Expand Up @@ -10,7 +10,7 @@ import {
import addPlatformSpecificPolyfills from "./add-platform-specific-polyfills";
import { hasMinVersion } from "./helpers";

import type { PolyfillProvider } from "@babel/plugin-inject-polyfills";
import { defineProvider } from "@babel/plugin-inject-polyfills";
import { types as t } from "@babel/core";

const presetEnvCompat: "#__secret_key__@babel/preset-env__compatibility" =
Expand All @@ -26,13 +26,13 @@ type Options = {|
},
|};

export default ((
export default defineProvider<Options>(function(
api,
{
version: runtimeVersion = "7.0.0-beta.0",
[presetEnvCompat]: { entryInjectRegenerator } = {},
},
) => {
) {
const resolve = api.createMetaResolver({
global: BuiltIns,
static: StaticProperties,
Expand All @@ -50,7 +50,7 @@ export default ((
const coreJSBase =
method === "usage-pure" ? "core-js/library/fn" : "core-js/modules";

function inject(name, utils) {
function inject(name: string | string[], utils) {
if (typeof name === "string") {
if (shouldInjectPolyfill(name)) {
debug(name);
Expand Down Expand Up @@ -185,4 +185,4 @@ export default ((
},
},
};
}: PolyfillProvider<Options>);
});
8 changes: 4 additions & 4 deletions packages/babel-polyfill-provider-corejs3/src/index.js
Expand Up @@ -16,18 +16,18 @@ import {
import { types as t } from "@babel/core";
import { callMethod, coreJSModule, isCoreJSSource } from "./utils";

import { type PolyfillProvider } from "@babel/plugin-inject-polyfills";
import { defineProvider } from "@babel/plugin-inject-polyfills";

type Options = {|
version?: number | string,
proposals?: boolean,
shippedProposals?: boolean,
|};

export default ((
export default defineProvider<Options>(function(
{ getUtils, method, shouldInjectPolyfill, createMetaResolver, debug },
{ version = 3, proposals, shippedProposals },
) => {
) {
const resolve = createMetaResolver({
global: BuiltIns,
static: StaticProperties,
Expand Down Expand Up @@ -254,4 +254,4 @@ export default ((
},
},
};
}: PolyfillProvider<Options>);
});
17 changes: 13 additions & 4 deletions packages/babel-polyfill-provider-es-shims/src/index.js
@@ -1,6 +1,7 @@
// @flow

import type { PolyfillProvider, Utils } from "@babel/plugin-inject-polyfills";
import { defineProvider, type Utils } from "@babel/plugin-inject-polyfills";

import resolve from "resolve";
import debounce from "lodash.debounce";

Expand All @@ -13,11 +14,19 @@ import {
InstanceProperties,
} from "./mappings";

export default ((
type Options = {|
missingDependencies?: {
log?: "per-file" | "deferred",
// When true, log all the polyfills without checking if they are installed
all?: boolean,
},
|};

export default defineProvider<Options>(function(
{ shouldInjectPolyfill, createMetaResolver, debug },
options,
dirname,
) => {
) {
const resolvePolyfill = createMetaResolver<Descriptor[]>({
global: Globals,
static: StaticProperties,
Expand Down Expand Up @@ -86,7 +95,7 @@ export default ((
}
},
};
}: PolyfillProvider<*>);
});

function hasDependency(basedir, name) {
try {
Expand Down

0 comments on commit c207323

Please sign in to comment.