1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 | var _utils = require("../utils");
|
---|
6 | function isRemoved(path) {
|
---|
7 | if (path.removed) return true;
|
---|
8 | if (!path.parentPath) return false;
|
---|
9 | if (path.listKey) {
|
---|
10 | var _path$parentPath$node;
|
---|
11 | if (!((_path$parentPath$node = path.parentPath.node) != null && (_path$parentPath$node = _path$parentPath$node[path.listKey]) != null && _path$parentPath$node.includes(path.node))) return true;
|
---|
12 | } else {
|
---|
13 | if (path.parentPath.node[path.key] !== path.node) return true;
|
---|
14 | }
|
---|
15 | return isRemoved(path.parentPath);
|
---|
16 | }
|
---|
17 | var _default = callProvider => {
|
---|
18 | function property(object, key, placement, path) {
|
---|
19 | return callProvider({
|
---|
20 | kind: "property",
|
---|
21 | object,
|
---|
22 | key,
|
---|
23 | placement
|
---|
24 | }, path);
|
---|
25 | }
|
---|
26 | function handleReferencedIdentifier(path) {
|
---|
27 | const {
|
---|
28 | node: {
|
---|
29 | name
|
---|
30 | },
|
---|
31 | scope
|
---|
32 | } = path;
|
---|
33 | if (scope.getBindingIdentifier(name)) return;
|
---|
34 | callProvider({
|
---|
35 | kind: "global",
|
---|
36 | name
|
---|
37 | }, path);
|
---|
38 | }
|
---|
39 | function analyzeMemberExpression(path) {
|
---|
40 | const key = (0, _utils.resolveKey)(path.get("property"), path.node.computed);
|
---|
41 | return {
|
---|
42 | key,
|
---|
43 | handleAsMemberExpression: !!key && key !== "prototype"
|
---|
44 | };
|
---|
45 | }
|
---|
46 | return {
|
---|
47 | // Symbol(), new Promise
|
---|
48 | ReferencedIdentifier(path) {
|
---|
49 | const {
|
---|
50 | parentPath
|
---|
51 | } = path;
|
---|
52 | if (parentPath.isMemberExpression({
|
---|
53 | object: path.node
|
---|
54 | }) && analyzeMemberExpression(parentPath).handleAsMemberExpression) {
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | handleReferencedIdentifier(path);
|
---|
58 | },
|
---|
59 | "MemberExpression|OptionalMemberExpression"(path) {
|
---|
60 | const {
|
---|
61 | key,
|
---|
62 | handleAsMemberExpression
|
---|
63 | } = analyzeMemberExpression(path);
|
---|
64 | if (!handleAsMemberExpression) return;
|
---|
65 | const object = path.get("object");
|
---|
66 | let objectIsGlobalIdentifier = object.isIdentifier();
|
---|
67 | if (objectIsGlobalIdentifier) {
|
---|
68 | const binding = object.scope.getBinding(object.node.name);
|
---|
69 | if (binding) {
|
---|
70 | if (binding.path.isImportNamespaceSpecifier()) return;
|
---|
71 | objectIsGlobalIdentifier = false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | const source = (0, _utils.resolveSource)(object);
|
---|
75 | let skipObject = property(source.id, key, source.placement, path);
|
---|
76 | skipObject || (skipObject = !objectIsGlobalIdentifier || path.shouldSkip || object.shouldSkip || isRemoved(object));
|
---|
77 | if (!skipObject) handleReferencedIdentifier(object);
|
---|
78 | },
|
---|
79 | ObjectPattern(path) {
|
---|
80 | const {
|
---|
81 | parentPath,
|
---|
82 | parent
|
---|
83 | } = path;
|
---|
84 | let obj;
|
---|
85 |
|
---|
86 | // const { keys, values } = Object
|
---|
87 | if (parentPath.isVariableDeclarator()) {
|
---|
88 | obj = parentPath.get("init");
|
---|
89 | // ({ keys, values } = Object)
|
---|
90 | } else if (parentPath.isAssignmentExpression()) {
|
---|
91 | obj = parentPath.get("right");
|
---|
92 | // !function ({ keys, values }) {...} (Object)
|
---|
93 | // resolution does not work after properties transform :-(
|
---|
94 | } else if (parentPath.isFunction()) {
|
---|
95 | const grand = parentPath.parentPath;
|
---|
96 | if (grand.isCallExpression() || grand.isNewExpression()) {
|
---|
97 | if (grand.node.callee === parent) {
|
---|
98 | obj = grand.get("arguments")[path.key];
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | let id = null;
|
---|
103 | let placement = null;
|
---|
104 | if (obj) ({
|
---|
105 | id,
|
---|
106 | placement
|
---|
107 | } = (0, _utils.resolveSource)(obj));
|
---|
108 | for (const prop of path.get("properties")) {
|
---|
109 | if (prop.isObjectProperty()) {
|
---|
110 | const key = (0, _utils.resolveKey)(prop.get("key"));
|
---|
111 | if (key) property(id, key, placement, prop);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | },
|
---|
115 | BinaryExpression(path) {
|
---|
116 | if (path.node.operator !== "in") return;
|
---|
117 | const source = (0, _utils.resolveSource)(path.get("right"));
|
---|
118 | const key = (0, _utils.resolveKey)(path.get("left"), true);
|
---|
119 | if (!key) return;
|
---|
120 | callProvider({
|
---|
121 | kind: "in",
|
---|
122 | object: source.id,
|
---|
123 | key,
|
---|
124 | placement: source.placement
|
---|
125 | }, path);
|
---|
126 | }
|
---|
127 | };
|
---|
128 | };
|
---|
129 | exports.default = _default; |
---|