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'
|
---|
93 | && propTypeExpression.property.name === 'isRequired';
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns the type arguments of a node or type parameters if type arguments are not available.
|
---|
98 | * @param {ASTNode} node The node to get the type arguments from.
|
---|
99 | * @returns {ASTNode} The type arguments or type parameters of the node.
|
---|
100 | */
|
---|
101 | function getTypeArguments(node) {
|
---|
102 | if ('typeArguments' in node) {
|
---|
103 | return node.typeArguments;
|
---|
104 | }
|
---|
105 | return node.typeParameters;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns the super type arguments of a node or super type parameters if type arguments are not available.
|
---|
110 | * @param {ASTNode} node The node to get the super type arguments from.
|
---|
111 | * @returns {ASTNode} The super type arguments or parameters of the node.
|
---|
112 | */
|
---|
113 | function getSuperTypeArguments(node) {
|
---|
114 | if ('superTypeArguments' in node) {
|
---|
115 | return node.superTypeArguments;
|
---|
116 | }
|
---|
117 | return node.superTypeParameters;
|
---|
118 | }
|
---|
119 |
|
---|
120 | module.exports = {
|
---|
121 | isPropTypesDeclaration,
|
---|
122 | isContextTypesDeclaration,
|
---|
123 | isContextTypeDeclaration,
|
---|
124 | isChildContextTypesDeclaration,
|
---|
125 | isDefaultPropsDeclaration,
|
---|
126 | isDisplayNameDeclaration,
|
---|
127 | isRequiredPropType,
|
---|
128 | getTypeArguments,
|
---|
129 | getSuperTypeArguments,
|
---|
130 | };
|
---|