usage.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _utils = require("../utils");
  5. var _default = callProvider => {
  6. function property(object, key, placement, path) {
  7. return callProvider({
  8. kind: "property",
  9. object,
  10. key,
  11. placement
  12. }, path);
  13. }
  14. return {
  15. // Symbol(), new Promise
  16. ReferencedIdentifier(path) {
  17. const {
  18. node: {
  19. name
  20. },
  21. scope
  22. } = path;
  23. if (scope.getBindingIdentifier(name)) return;
  24. callProvider({
  25. kind: "global",
  26. name
  27. }, path);
  28. },
  29. MemberExpression(path) {
  30. const key = (0, _utils.resolveKey)(path.get("property"), path.node.computed);
  31. if (!key || key === "prototype") return;
  32. const object = path.get("object");
  33. if (object.isIdentifier()) {
  34. const binding = object.scope.getBinding(object.node.name);
  35. if (binding && binding.path.isImportNamespaceSpecifier()) return;
  36. }
  37. const source = (0, _utils.resolveSource)(object);
  38. return property(source.id, key, source.placement, path);
  39. },
  40. ObjectPattern(path) {
  41. const {
  42. parentPath,
  43. parent
  44. } = path;
  45. let obj; // const { keys, values } = Object
  46. if (parentPath.isVariableDeclarator()) {
  47. obj = parentPath.get("init"); // ({ keys, values } = Object)
  48. } else if (parentPath.isAssignmentExpression()) {
  49. obj = parentPath.get("right"); // !function ({ keys, values }) {...} (Object)
  50. // resolution does not work after properties transform :-(
  51. } else if (parentPath.isFunction()) {
  52. const grand = parentPath.parentPath;
  53. if (grand.isCallExpression() || grand.isNewExpression()) {
  54. if (grand.node.callee === parent) {
  55. obj = grand.get("arguments")[path.key];
  56. }
  57. }
  58. }
  59. let id = null;
  60. let placement = null;
  61. if (obj) ({
  62. id,
  63. placement
  64. } = (0, _utils.resolveSource)(obj));
  65. for (const prop of path.get("properties")) {
  66. if (prop.isObjectProperty()) {
  67. const key = (0, _utils.resolveKey)(prop.get("key"));
  68. if (key) property(id, key, placement, prop);
  69. }
  70. }
  71. },
  72. BinaryExpression(path) {
  73. if (path.node.operator !== "in") return;
  74. const source = (0, _utils.resolveSource)(path.get("right"));
  75. const key = (0, _utils.resolveKey)(path.get("left"), true);
  76. if (!key) return;
  77. callProvider({
  78. kind: "in",
  79. object: source.id,
  80. key,
  81. placement: source.placement
  82. }, path);
  83. }
  84. };
  85. };
  86. exports.default = _default;