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