isCreateContext.js 1.4 KB

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