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 06652c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 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 = new Set(),
excludes = new Set(),
}: {
compatData?: { [feature: string]: Targets },
includes?: Set<string>,
excludes?: Set<string>,
} = {},
) {
if (excludes.has(name)) return false;
if (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
6 changes: 5 additions & 1 deletion packages/babel-helper-compilation-targets/src/index.js
Expand Up @@ -19,7 +19,11 @@ export type { Targets };

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

const browserslistDefaults = browserslist.defaults;
Expand Down

0 comments on commit 06652c9

Please sign in to comment.