Skip to content

Commit da82f2b

Browse files
committed
Add checkChildContextTypes for forbid-prop-types
1 parent 9ab07e0 commit da82f2b

File tree

4 files changed

+450
-18
lines changed

4 files changed

+450
-18
lines changed

docs/rules/forbid-prop-types.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Component extends React.Component {
4444

4545
```js
4646
...
47-
"react/forbid-prop-types": [<enabled>, { "forbid": [<string>], checkContextTypes: <boolean> }]
47+
"react/forbid-prop-types": [<enabled>, { "forbid": [<string>], checkContextTypes: <boolean>, checkChildContextTypes: <boolean> }]
4848
...
4949
```
5050

@@ -56,6 +56,10 @@ An array of strings, with the names of `PropTypes` keys that are forbidden. The
5656

5757
Whether or not to check `contextTypes` for forbidden prop types. The default value is false.
5858

59+
### `checkChildContextTypes`
60+
61+
Whether or not to check `childContextTypes` for forbidden prop types. The default value is false.
62+
5963
## When not to use
6064

6165
This rule is a formatting/documenting preference and not following it won't negatively affect the quality of your code. This rule encourages prop types that more specifically document their usage.

lib/rules/forbid-prop-types.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module.exports = {
3535
},
3636
checkContextTypes: {
3737
type: 'boolean'
38+
},
39+
checkChildContextTypes: {
40+
type: 'boolean'
3841
}
3942
},
4043
additionalProperties: true
@@ -45,6 +48,7 @@ module.exports = {
4548
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4649
const configuration = context.options[0] || {};
4750
const checkContextTypes = configuration.checkContextTypes || false;
51+
const checkChildContextTypes = configuration.checkChildContextTypes || false;
4852

4953
function isForbidden(type) {
5054
const forbid = configuration.forbid || DEFAULTS;
@@ -58,6 +62,13 @@ module.exports = {
5862
return false;
5963
}
6064

65+
function shouldCheckChildContextTypes(node) {
66+
if (checkChildContextTypes && propsUtil.isChildContextTypesDeclaration(node)) {
67+
return true;
68+
}
69+
return false;
70+
}
71+
6172
/**
6273
* Find a variable by name in the current scope.
6374
* @param {string} name Name of the variable to look for.
@@ -142,14 +153,22 @@ module.exports = {
142153

143154
return {
144155
ClassProperty: function(node) {
145-
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
156+
if (
157+
!propsUtil.isPropTypesDeclaration(node) &&
158+
!shouldCheckContextTypes(node) &&
159+
!shouldCheckChildContextTypes(node)
160+
) {
146161
return;
147162
}
148163
checkNode(node.value);
149164
},
150165

151166
MemberExpression: function(node) {
152-
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
167+
if (
168+
!propsUtil.isPropTypesDeclaration(node) &&
169+
!shouldCheckContextTypes(node) &&
170+
!shouldCheckChildContextTypes(node)
171+
) {
153172
return;
154173
}
155174

@@ -162,7 +181,11 @@ module.exports = {
162181
return;
163182
}
164183

165-
if (!propsUtil.isPropTypesDeclaration(property) && !shouldCheckContextTypes(property)) {
184+
if (
185+
!propsUtil.isPropTypesDeclaration(property) &&
186+
!shouldCheckContextTypes(property) &&
187+
!shouldCheckChildContextTypes(property)
188+
) {
166189
return;
167190
}
168191
if (property.value.type === 'ObjectExpression') {

lib/util/props.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ function isContextTypesDeclaration(node) {
3535
return astUtil.getPropertyName(node) === 'contextTypes';
3636
}
3737

38+
/**
39+
* Checks if the node passed in looks like a childContextTypes declaration.
40+
* @param {ASTNode} node The node to check.
41+
* @returns {Boolean} `true` if the node is a childContextTypes declaration, `false` if not
42+
*/
43+
function isChildContextTypesDeclaration(node) {
44+
return astUtil.getPropertyName(node) === 'childContextTypes';
45+
}
46+
3847
/**
3948
* Checks if the Identifier node passed in looks like a defaultProps declaration.
4049
* @param {ASTNode} node The node to check. Must be an Identifier node.
@@ -57,6 +66,7 @@ function isRequiredPropType(propTypeExpression) {
5766
module.exports = {
5867
isPropTypesDeclaration: isPropTypesDeclaration,
5968
isContextTypesDeclaration: isContextTypesDeclaration,
69+
isChildContextTypesDeclaration: isChildContextTypesDeclaration,
6070
isDefaultPropsDeclaration: isDefaultPropsDeclaration,
6171
isRequiredPropType: isRequiredPropType
6272
};

0 commit comments

Comments
 (0)