props.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @fileoverview Utility functions for props
  3. */
  4. 'use strict';
  5. const astUtil = require('./ast');
  6. /**
  7. * Checks if the Identifier node passed in looks like a propTypes declaration.
  8. * @param {ASTNode} node The node to check. Must be an Identifier node.
  9. * @returns {Boolean} `true` if the node is a propTypes declaration, `false` if not
  10. */
  11. function isPropTypesDeclaration(node) {
  12. if (node && node.type === 'ClassProperty') {
  13. // Flow support
  14. if (node.typeAnnotation && node.key.name === 'props') {
  15. return true;
  16. }
  17. }
  18. return astUtil.getPropertyName(node) === 'propTypes';
  19. }
  20. /**
  21. * Checks if the node passed in looks like a contextTypes declaration.
  22. * @param {ASTNode} node The node to check.
  23. * @returns {Boolean} `true` if the node is a contextTypes declaration, `false` if not
  24. */
  25. function isContextTypesDeclaration(node) {
  26. if (node && node.type === 'ClassProperty') {
  27. // Flow support
  28. if (node.typeAnnotation && node.key.name === 'context') {
  29. return true;
  30. }
  31. }
  32. return astUtil.getPropertyName(node) === 'contextTypes';
  33. }
  34. /**
  35. * Checks if the node passed in looks like a contextType declaration.
  36. * @param {ASTNode} node The node to check.
  37. * @returns {Boolean} `true` if the node is a contextType declaration, `false` if not
  38. */
  39. function isContextTypeDeclaration(node) {
  40. return astUtil.getPropertyName(node) === 'contextType';
  41. }
  42. /**
  43. * Checks if the node passed in looks like a childContextTypes declaration.
  44. * @param {ASTNode} node The node to check.
  45. * @returns {Boolean} `true` if the node is a childContextTypes declaration, `false` if not
  46. */
  47. function isChildContextTypesDeclaration(node) {
  48. return astUtil.getPropertyName(node) === 'childContextTypes';
  49. }
  50. /**
  51. * Checks if the Identifier node passed in looks like a defaultProps declaration.
  52. * @param {ASTNode} node The node to check. Must be an Identifier node.
  53. * @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
  54. */
  55. function isDefaultPropsDeclaration(node) {
  56. const propName = astUtil.getPropertyName(node);
  57. return (propName === 'defaultProps' || propName === 'getDefaultProps');
  58. }
  59. /**
  60. * Checks if we are declaring a display name
  61. * @param {ASTNode} node The AST node being checked.
  62. * @returns {Boolean} True if we are declaring a display name, false if not.
  63. */
  64. function isDisplayNameDeclaration(node) {
  65. switch (node.type) {
  66. case 'ClassProperty':
  67. return node.key && node.key.name === 'displayName';
  68. case 'Identifier':
  69. return node.name === 'displayName';
  70. case 'Literal':
  71. return node.value === 'displayName';
  72. default:
  73. return false;
  74. }
  75. }
  76. /**
  77. * Checks if the PropTypes MemberExpression node passed in declares a required propType.
  78. * @param {ASTNode} propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
  79. * @returns {Boolean} `true` if this PropType is required, `false` if not.
  80. */
  81. function isRequiredPropType(propTypeExpression) {
  82. return propTypeExpression.type === 'MemberExpression' && propTypeExpression.property.name === 'isRequired';
  83. }
  84. module.exports = {
  85. isPropTypesDeclaration,
  86. isContextTypesDeclaration,
  87. isContextTypeDeclaration,
  88. isChildContextTypesDeclaration,
  89. isDefaultPropsDeclaration,
  90. isDisplayNameDeclaration,
  91. isRequiredPropType
  92. };