usage.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const binding = object.scope.getBinding(object.node.name);
  34. if (binding && binding.path.isImportNamespaceSpecifier()) return;
  35. const source = (0, _utils.resolveSource)(object);
  36. return property(source.id, key, source.placement, path);
  37. },
  38. ObjectPattern(path) {
  39. const {
  40. parentPath,
  41. parent
  42. } = path;
  43. let obj; // const { keys, values } = Object
  44. if (parentPath.isVariableDeclarator()) {
  45. obj = parentPath.get("init"); // ({ keys, values } = Object)
  46. } else if (parentPath.isAssignmentExpression()) {
  47. obj = parentPath.get("right"); // !function ({ keys, values }) {...} (Object)
  48. // resolution does not work after properties transform :-(
  49. } else if (parentPath.isFunction()) {
  50. const grand = parentPath.parentPath;
  51. if (grand.isCallExpression() || grand.isNewExpression()) {
  52. if (grand.node.callee === parent) {
  53. obj = grand.get("arguments")[path.key];
  54. }
  55. }
  56. }
  57. let id = null;
  58. let placement = null;
  59. if (obj) ({
  60. id,
  61. placement
  62. } = (0, _utils.resolveSource)(obj));
  63. for (const prop of path.get("properties")) {
  64. if (prop.isObjectProperty()) {
  65. const key = (0, _utils.resolveKey)(prop.get("key"));
  66. if (key) property(id, key, placement, prop);
  67. }
  68. }
  69. },
  70. BinaryExpression(path) {
  71. if (path.node.operator !== "in") return;
  72. const source = (0, _utils.resolveSource)(path.get("right"));
  73. const key = (0, _utils.resolveKey)(path.get("left"), true);
  74. if (!key) return;
  75. callProvider({
  76. kind: "in",
  77. object: source.id,
  78. key,
  79. placement: source.placement
  80. }, path);
  81. }
  82. };
  83. };
  84. exports.default = _default;