index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  8. var _core = require("@babel/core");
  9. const PURE_CALLS = new Map([["react", ["cloneElement", "createContext", "createElement", "createFactory", "createRef", "forwardRef", "isValidElement", "memo", "lazy"]], ["react-dom", ["createPortal"]]]);
  10. var _default = (0, _helperPluginUtils.declare)(api => {
  11. api.assertVersion(7);
  12. return {
  13. name: "transform-react-pure-annotations",
  14. visitor: {
  15. CallExpression(path) {
  16. if (isReactCall(path)) {
  17. (0, _helperAnnotateAsPure.default)(path);
  18. }
  19. }
  20. }
  21. };
  22. });
  23. exports.default = _default;
  24. function isReactCall(path) {
  25. if (!_core.types.isMemberExpression(path.node.callee)) {
  26. const callee = path.get("callee");
  27. for (const [module, methods] of PURE_CALLS) {
  28. for (const method of methods) {
  29. if (callee.referencesImport(module, method)) {
  30. return true;
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36. for (const [module, methods] of PURE_CALLS) {
  37. const object = path.get("callee.object");
  38. if (object.referencesImport(module, "default") || object.referencesImport(module, "*")) {
  39. for (const method of methods) {
  40. if (_core.types.isIdentifier(path.node.callee.property, {
  41. name: method
  42. })) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. }
  49. return false;
  50. }