| 1 | import semver from "semver"; |
| 2 | |
| 3 | import pluginsCompatData from "@babel/compat-data/plugins"; |
| 4 | |
| 5 | import type { Targets } from "./types"; |
| 6 | import { |
| 7 | getLowestImplementedVersion, |
| 8 | isUnreleasedVersion, |
| 9 | semverify, |
| 10 | } from "./utils"; |
| 11 | |
| 12 | export function targetsSupported(target: Targets, support: Targets) { |
| 13 | const targetEnvironments = Object.keys(target) as Array<keyof Targets>; |
| 14 | |
| 15 | if (targetEnvironments.length === 0) { |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | const unsupportedEnvironments = targetEnvironments.filter(environment => { |
| 20 | const lowestImplementedVersion = getLowestImplementedVersion( |
| 21 | support, |
| 22 | environment, |
| 23 | ); |
| 24 | |
| 25 | // Feature is not implemented in that environment |
| 26 | if (!lowestImplementedVersion) { |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | const lowestTargetedVersion = target[environment]; |
| 31 | |
| 32 | // If targets has unreleased value as a lowest version, then don't require a plugin. |
| 33 | if (isUnreleasedVersion(lowestTargetedVersion, environment)) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | // Include plugin if it is supported in the unreleased environment, which wasn't specified in targets |
| 38 | if (isUnreleasedVersion(lowestImplementedVersion, environment)) { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | if (!semver.valid(lowestTargetedVersion.toString())) { |
| 43 | throw new Error( |
| 44 | `Invalid version passed for target "${environment}": "${lowestTargetedVersion}". ` + |
| 45 | "Versions must be in semver format (major.minor.patch)", |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | return semver.gt( |
| 50 | semverify(lowestImplementedVersion), |
| 51 | lowestTargetedVersion.toString(), |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | return unsupportedEnvironments.length === 0; |
| 56 | } |
| 57 | |
| 58 | export function isRequired( |
| 59 | name: string, |
| 60 | targets: Targets, |
| 61 | { |
| 62 | compatData = pluginsCompatData, |
| 63 | includes, |
| 64 | excludes, |
| 65 | }: { |
| 66 | compatData?: { [feature: string]: Targets }; |
| 67 | includes?: Set<string>; |
| 68 | excludes?: Set<string>; |
| 69 | } = {}, |
| 70 | ) { |
| 71 | if (excludes?.has(name)) return false; |
| 72 | if (includes?.has(name)) return true; |
| 73 | return !targetsSupported(targets, compatData[name]); |
| 74 | } |
| 75 | |
| 76 | export default function filterItems( |
| 77 | list: { [feature: string]: Targets }, |
| 78 | includes: Set<string>, |
| 79 | excludes: Set<string>, |
| 80 | targets: Targets, |
| 81 | defaultIncludes: Array<string> | null, |
| 82 | defaultExcludes?: Array<string> | null, |
| 83 | pluginSyntaxMap?: Map<string, string | null>, |
| 84 | ) { |
| 85 | const result = new Set<string>(); |
| 86 | const options = { compatData: list, includes, excludes }; |
| 87 | |
| 88 | for (const item in list) { |
| 89 | if (isRequired(item, targets, options)) { |
| 90 | result.add(item); |
| 91 | } else if (pluginSyntaxMap) { |
| 92 | const shippedProposalsSyntax = pluginSyntaxMap.get(item); |
| 93 | |
| 94 | if (shippedProposalsSyntax) { |
| 95 | result.add(shippedProposalsSyntax); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (defaultIncludes) { |
| 101 | defaultIncludes.forEach(item => !excludes.has(item) && result.add(item)); |
| 102 | } |
| 103 | |
| 104 | if (defaultExcludes) { |
| 105 | defaultExcludes.forEach(item => !includes.has(item) && result.delete(item)); |
| 106 | } |
| 107 | |
| 108 | return result; |
| 109 | } |