usage.js 3.9 KB

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