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