forked from babel/babel-polyfills
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.js
99 lines (82 loc) · 2.71 KB
/
usage.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
// @flow
import type { NodePath } from "@babel/traverse";
import type { MetaDescriptor } from "../types";
import { resolveKey, resolveSource } from "../utils";
export default (
callProvider: (payload: MetaDescriptor, path: NodePath) => void,
) => {
function property(object, key, placement, path) {
return callProvider({ kind: "property", object, key, placement }, path);
}
return {
// Symbol(), new Promise
ReferencedIdentifier(path: NodePath) {
if (
path.parentPath.isExportSpecifier() &&
path.parentPath.parent.source
) {
// export { Foo } from "module"
return;
}
const {
node: { name },
scope,
} = path;
if (scope.getBindingIdentifier(name)) return;
callProvider({ kind: "global", name }, path);
},
MemberExpression(path: NodePath) {
const key = resolveKey(path.get("property"), path.node.computed);
if (!key || key === "prototype") return;
const object = path.get("object");
const binding = object.scope.getBinding(object.node.name);
if (binding && binding.path.isImportNamespaceSpecifier()) return;
const source = resolveSource(object);
return property(source.id, key, source.placement, path);
},
ObjectPattern(path: NodePath) {
const { parentPath, parent } = path;
let obj;
// const { keys, values } = Object
if (parentPath.isVariableDeclarator()) {
obj = parentPath.get("init");
// ({ keys, values } = Object)
} else if (parentPath.isAssignmentExpression()) {
obj = parentPath.get("right");
// !function ({ keys, values }) {...} (Object)
// resolution does not work after properties transform :-(
} else if (parentPath.isFunction()) {
const grand = parentPath.parentPath;
if (grand.isCallExpression() || grand.isNewExpression()) {
if (grand.node.callee === parent) {
obj = grand.get("arguments")[path.key];
}
}
}
let id = null;
let placement = null;
if (obj) ({ id, placement } = resolveSource(obj));
for (const prop of path.get("properties")) {
if (prop.isObjectProperty()) {
const key = resolveKey(prop.get("key"));
if (key) property(id, key, placement, prop);
}
}
},
BinaryExpression(path: NodePath) {
if (path.node.operator !== "in") return;
const source = resolveSource(path.get("right"));
const key = resolveKey(path.get("left"), true);
if (!key) return;
callProvider(
{
kind: "in",
object: source.id,
key,
placement: source.placement,
},
path,
);
},
};
};