source: imaps-frontend/node_modules/eslint-plugin-react/lib/util/props.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[d565449]1/**
2 * @fileoverview Utility functions for props
3 */
4
5'use strict';
6
7const 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.
[0c6b92a]12 * @returns {boolean} `true` if the node is a propTypes declaration, `false` if not
[d565449]13 */
14function 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.
[0c6b92a]27 * @returns {boolean} `true` if the node is a contextTypes declaration, `false` if not
[d565449]28 */
29function 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.
[0c6b92a]42 * @returns {boolean} `true` if the node is a contextType declaration, `false` if not
[d565449]43 */
44function 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.
[0c6b92a]51 * @returns {boolean} `true` if the node is a childContextTypes declaration, `false` if not
[d565449]52 */
53function 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.
[0c6b92a]60 * @returns {boolean} `true` if the node is a defaultProps declaration, `false` if not
[d565449]61 */
62function 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.
[0c6b92a]70 * @returns {boolean} True if we are declaring a display name, false if not.
[d565449]71 */
72function 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.
[0c6b92a]89 * @returns {boolean} `true` if this PropType is required, `false` if not.
[d565449]90 */
91function isRequiredPropType(propTypeExpression) {
[0c6b92a]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 */
101function 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 */
113function getSuperTypeArguments(node) {
114 if ('superTypeArguments' in node) {
115 return node.superTypeArguments;
116 }
117 return node.superTypeParameters;
[d565449]118}
119
120module.exports = {
121 isPropTypesDeclaration,
122 isContextTypesDeclaration,
123 isContextTypeDeclaration,
124 isChildContextTypesDeclaration,
125 isDefaultPropsDeclaration,
126 isDisplayNameDeclaration,
127 isRequiredPropType,
[0c6b92a]128 getTypeArguments,
129 getSuperTypeArguments,
[d565449]130};
Note: See TracBrowser for help on using the repository browser.