/
utils.js
76 lines (63 loc) 路 2.08 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
// @flow
import * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
import { addSideEffect } from "@babel/helper-module-imports";
import type { Targets } from "@babel/helper-compilation-targets";
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();
}
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 filterStageFromList(
list: { [feature: string]: Targets },
stageList: Set<string>,
) {
return Object.keys(list).reduce((result, item) => {
if (!stageList.has(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.js",
};
export function getModulePath(mod: string): string {
return modulePathMap[mod] || `core-js/modules/${mod}.js`;
}
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();
}