Skip to content

Commit

Permalink
Export isRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jan 9, 2020
1 parent 4cd8190 commit 58d6106
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
26 changes: 22 additions & 4 deletions packages/babel-helper-compilation-targets/src/filter-items.js
Expand Up @@ -2,6 +2,8 @@

import semver from "semver";

import pluginsCompatData from "@babel/compat-data/plugins";

import type { Targets } from "./types";
import {
getLowestImplementedVersion,
Expand Down Expand Up @@ -55,6 +57,24 @@ export function targetsSupported(target: Targets, support: Targets) {
return unsupportedEnvironments.length === 0;
}

export function isRequired(
name: string,
targets: Targets,
{
compatData = pluginsCompatData,
includes,
excludes,
}: {
compatData?: { [feature: string]: Targets },
includes?: Set<string>,
excludes?: Set<string>,
} = {},
) {
if (excludes && excludes.has(name)) return false;
if (includes && includes.has(name)) return true;
return !targetsSupported(targets, compatData[name]);
}

export default function filterItems(
list: { [feature: string]: Targets },
includes: Set<string>,
Expand All @@ -65,12 +85,10 @@ export default function filterItems(
pluginSyntaxMap?: Map<string, string | null>,
) {
const result = new Set<string>();
const options = { compatData: list, includes, excludes };

for (const item in list) {
if (
!excludes.has(item) &&
(!targetsSupported(targets, list[item]) || includes.has(item))
) {
if (isRequired(item, targets, options)) {
result.add(item);
} else if (pluginSyntaxMap) {
const shippedProposalsSyntax = pluginSyntaxMap.get(item);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/index.js
Expand Up @@ -19,7 +19,7 @@ export type { Targets };

export { prettifyTargets } from "./pretty";
export { getInclusionReasons } from "./debug";
export { default as filterItems, targetsSupported } from "./filter-items";
export { default as filterItems, isRequired } from "./filter-items";
export { unreleasedLabels } from "./targets";

const browserslistDefaults = browserslist.defaults;
Expand Down
@@ -1,6 +1,6 @@
"use strict";

const { targetsSupported } = require("../lib");
const { targetsSupported } = require("../lib/filter-items");

describe("targetsSupported", () => {
const MAX_VERSION = `${Number.MAX_SAFE_INTEGER}.0.0`;
Expand Down
8 changes: 5 additions & 3 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -20,7 +20,7 @@ import removeRegeneratorEntryPlugin from "./polyfills/regenerator/entry-plugin";
import getTargets, {
prettifyTargets,
filterItems,
targetsSupported,
isRequired,
type Targets,
} from "@babel/helper-compilation-targets";
import availablePlugins from "./available-plugins";
Expand All @@ -31,8 +31,10 @@ import typeof ModuleTransformationsType from "./module-transformations";
import type { BuiltInsOption, ModuleOption } from "./types";

// TODO: Remove in Babel 8
export function isPluginRequired(target: Targets, support: Targets) {
return !targetsSupported(target, support);
export function isPluginRequired(targets: Targets, support: Targets) {
return !isRequired("fake-name", targets, {
compatData: { "fake-name": support },
});
}

const pluginListWithoutProposals = filterStageFromList(
Expand Down

0 comments on commit 58d6106

Please sign in to comment.