isCreateContext.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const astUtil = require('./ast');
  3. /**
  4. * Checks if the node is a React.createContext call
  5. * @param {ASTNode} node - The AST node being checked.
  6. * @returns {boolean} - True if node is a React.createContext call, false if not.
  7. */
  8. module.exports = function isCreateContext(node) {
  9. if (
  10. node.init
  11. && node.init.callee
  12. ) {
  13. if (
  14. astUtil.isCallExpression(node.init)
  15. && node.init.callee.name === 'createContext'
  16. ) {
  17. return true;
  18. }
  19. if (
  20. node.init.callee.type === 'MemberExpression'
  21. && node.init.callee.property
  22. && node.init.callee.property.name === 'createContext'
  23. ) {
  24. return true;
  25. }
  26. }
  27. if (
  28. node.expression
  29. && node.expression.type === 'AssignmentExpression'
  30. && node.expression.operator === '='
  31. && astUtil.isCallExpression(node.expression.right)
  32. && node.expression.right.callee
  33. ) {
  34. const right = node.expression.right;
  35. if (right.callee.name === 'createContext') {
  36. return true;
  37. }
  38. if (
  39. right.callee.type === 'MemberExpression'
  40. && right.callee.property
  41. && right.callee.property.name === 'createContext'
  42. ) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. };