forked from babel/babel
/
utils.js
170 lines (142 loc) 路 4.33 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// @flow
import * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
import invariant from "invariant";
import semver from "semver";
import { addSideEffect } from "@babel/helper-module-imports";
import unreleasedLabels from "../data/unreleased-labels";
import { semverMin } from "./targets-parser";
import type { Target, Targets } from "./types";
export const has = Object.hasOwnProperty.call.bind(Object.hasOwnProperty);
export function getType(target: any): string {
return Object.prototype.toString
.call(target)
.slice(8, -1)
.toLowerCase();
}
const versionRegExp = /^(\d+|\d+.\d+)$/;
// Convert version to a semver value.
// 2.5 -> 2.5.0; 1 -> 1.0.0;
export function semverify(version: number | string): string {
if (typeof version === "string" && semver.valid(version)) {
return version;
}
invariant(
typeof version === "number" ||
(typeof version === "string" && versionRegExp.test(version)),
`'${version}' is not a valid version`,
);
const split = version.toString().split(".");
while (split.length < 3) {
split.push("0");
}
return split.join(".");
}
export function intersection<T>(
first: Set<T>,
second: Set<T>,
third: Set<T>,
): Set<T> {
const result = new Set();
for (const el of first) {
if (second.has(el) && third.has(el)) result.add(el);
}
return result;
}
export function prettifyVersion(version: string) {
if (typeof version !== "string") {
return version;
}
const parts = [semver.major(version)];
const minor = semver.minor(version);
const patch = semver.patch(version);
if (minor || patch) {
parts.push(minor);
}
if (patch) {
parts.push(patch);
}
return parts.join(".");
}
export function prettifyTargets(targets: Targets): Targets {
return Object.keys(targets).reduce((results, target) => {
let value = targets[target];
const unreleasedLabel = unreleasedLabels[target];
if (typeof value === "string" && unreleasedLabel !== value) {
value = prettifyVersion(value);
}
results[target] = value;
return results;
}, {});
}
export function isUnreleasedVersion(
version: string | number,
env: string,
): boolean {
const unreleasedLabel = unreleasedLabels[env];
return (
!!unreleasedLabel && unreleasedLabel === version.toString().toLowerCase()
);
}
export function getLowestUnreleased(a: string, b: string, env: string): string {
const unreleasedLabel = unreleasedLabels[env];
const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
if (hasUnreleased) {
return a === hasUnreleased ? b : a || b;
}
return semverMin(a, b);
}
export function getLowestImplementedVersion(
plugin: Targets,
environment: Target,
): string {
const result = plugin[environment];
// When Android support data is absent, use Chrome data as fallback
if (!result && environment === "android") {
return plugin.chrome;
}
return result;
}
export function filterStageFromList(
list: { [feature: string]: Targets },
stageList: { [feature: string]: boolean },
) {
return Object.keys(list).reduce((result, item) => {
if (!stageList[item]) {
result[item] = list[item];
}
return result;
}, {});
}
export function getImportSource({ node }: NodePath) {
if (node.specifiers.length === 0) return node.source.value;
}
export function getRequireSource({ node }: NodePath) {
if (!t.isExpressionStatement(node)) return;
const { expression } = node;
const isRequire =
t.isCallExpression(expression) &&
t.isIdentifier(expression.callee) &&
expression.callee.name === "require" &&
expression.arguments.length === 1 &&
t.isStringLiteral(expression.arguments[0]);
if (isRequire) return expression.arguments[0].value;
}
export function isPolyfillSource(source: ?string): boolean {
return source === "@babel/polyfill" || source === "core-js";
}
const modulePathMap = {
"regenerator-runtime": "regenerator-runtime/runtime",
};
export function getModulePath(mod: string): string {
return modulePathMap[mod] || `core-js/modules/${mod}`;
}
export function createImport(path: NodePath, mod: string) {
return addSideEffect(path, getModulePath(mod));
}
export function isNamespaced(path: NodePath) {
if (!path.node) return false;
const binding = path.scope.getBinding(path.node.name);
if (!binding) return false;
return binding.path.isImportNamespaceSpecifier();
}