props.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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' || node.type === 'PropertyDefinition')) {
  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' || node.type === 'PropertyDefinition')) {
  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. case 'PropertyDefinition':
  68. return node.key && node.key.name === 'displayName';
  69. case 'Identifier':
  70. return node.name === 'displayName';
  71. case 'Literal':
  72. return node.value === 'displayName';
  73. default:
  74. return false;
  75. }
  76. }
  77. /**
  78. * Checks if the PropTypes MemberExpression node passed in declares a required propType.
  79. * @param {ASTNode} propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
  80. * @returns {boolean} `true` if this PropType is required, `false` if not.
  81. */
  82. function isRequiredPropType(propTypeExpression) {
  83. return propTypeExpression.type === 'MemberExpression'
  84. && propTypeExpression.property.name === 'isRequired';
  85. }
  86. /**
  87. * Returns the type arguments of a node or type parameters if type arguments are not available.
  88. * @param {ASTNode} node The node to get the type arguments from.
  89. * @returns {ASTNode} The type arguments or type parameters of the node.
  90. */
  91. function getTypeArguments(node) {
  92. if ('typeArguments' in node) {
  93. return node.typeArguments;
  94. }
  95. return node.typeParameters;
  96. }
  97. /**
  98. * Returns the super type arguments of a node or super type parameters if type arguments are not available.
  99. * @param {ASTNode} node The node to get the super type arguments from.
  100. * @returns {ASTNode} The super type arguments or parameters of the node.
  101. */
  102. function getSuperTypeArguments(node) {
  103. if ('superTypeArguments' in node) {
  104. return node.superTypeArguments;
  105. }
  106. return node.superTypeParameters;
  107. }
  108. module.exports = {
  109. isPropTypesDeclaration,
  110. isContextTypesDeclaration,
  111. isContextTypeDeclaration,
  112. isChildContextTypesDeclaration,
  113. isDefaultPropsDeclaration,
  114. isDisplayNameDeclaration,
  115. isRequiredPropType,
  116. getTypeArguments,
  117. getSuperTypeArguments,
  118. };