usage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _utils = require("../utils");
  5. function isRemoved(path) {
  6. if (path.removed) return true;
  7. if (!path.parentPath) return false;
  8. if (path.listKey) {
  9. var _path$parentPath$node;
  10. 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;
  11. } else {
  12. var _path$parentPath$node2;
  13. if (((_path$parentPath$node2 = path.parentPath.node) == null ? void 0 : _path$parentPath$node2[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. // const { keys, values } = Object
  86. if (parentPath.isVariableDeclarator()) {
  87. obj = parentPath.get("init");
  88. // ({ keys, values } = Object)
  89. } else if (parentPath.isAssignmentExpression()) {
  90. obj = parentPath.get("right");
  91. // !function ({ keys, values }) {...} (Object)
  92. // resolution does not work after properties transform :-(
  93. } else if (parentPath.isFunction()) {
  94. const grand = parentPath.parentPath;
  95. if (grand.isCallExpression() || grand.isNewExpression()) {
  96. if (grand.node.callee === parent) {
  97. obj = grand.get("arguments")[path.key];
  98. }
  99. }
  100. }
  101. let id = null;
  102. let placement = null;
  103. if (obj) ({
  104. id,
  105. placement
  106. } = (0, _utils.resolveSource)(obj));
  107. for (const prop of path.get("properties")) {
  108. if (prop.isObjectProperty()) {
  109. const key = (0, _utils.resolveKey)(prop.get("key"));
  110. if (key) property(id, key, placement, prop);
  111. }
  112. }
  113. },
  114. BinaryExpression(path) {
  115. if (path.node.operator !== "in") return;
  116. const source = (0, _utils.resolveSource)(path.get("right"));
  117. const key = (0, _utils.resolveKey)(path.get("left"), true);
  118. if (!key) return;
  119. callProvider({
  120. kind: "in",
  121. object: source.id,
  122. key,
  123. placement: source.placement
  124. }, path);
  125. }
  126. };
  127. };
  128. exports.default = _default;