Skip to content

Commit 9ab07e0

Browse files
committed
Add checkContextTypes option to forbid-prop-types
1 parent 4f3fc51 commit 9ab07e0

File tree

4 files changed

+431
-6
lines changed

4 files changed

+431
-6
lines changed

docs/rules/forbid-prop-types.md

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

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

5151
### `forbid`
5252

5353
An array of strings, with the names of `PropTypes` keys that are forbidden. The default value for this option is `['any', 'array', 'object']`.
5454

55+
### `checkContextTypes`
56+
57+
Whether or not to check `contextTypes` for forbidden prop types. The default value is false.
58+
5559
## When not to use
5660

5761
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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module.exports = {
3232
items: {
3333
type: 'string'
3434
}
35+
},
36+
checkContextTypes: {
37+
type: 'boolean'
3538
}
3639
},
3740
additionalProperties: true
@@ -40,14 +43,21 @@ module.exports = {
4043

4144
create: function(context) {
4245
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
46+
const configuration = context.options[0] || {};
47+
const checkContextTypes = configuration.checkContextTypes || false;
4348

4449
function isForbidden(type) {
45-
const configuration = context.options[0] || {};
46-
4750
const forbid = configuration.forbid || DEFAULTS;
4851
return forbid.indexOf(type) >= 0;
4952
}
5053

54+
function shouldCheckContextTypes(node) {
55+
if (checkContextTypes && propsUtil.isContextTypesDeclaration(node)) {
56+
return true;
57+
}
58+
return false;
59+
}
60+
5161
/**
5262
* Find a variable by name in the current scope.
5363
* @param {string} name Name of the variable to look for.
@@ -132,14 +142,14 @@ module.exports = {
132142

133143
return {
134144
ClassProperty: function(node) {
135-
if (!propsUtil.isPropTypesDeclaration(node)) {
145+
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
136146
return;
137147
}
138148
checkNode(node.value);
139149
},
140150

141151
MemberExpression: function(node) {
142-
if (!propsUtil.isPropTypesDeclaration(node)) {
152+
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
143153
return;
144154
}
145155

@@ -152,7 +162,7 @@ module.exports = {
152162
return;
153163
}
154164

155-
if (!propsUtil.isPropTypesDeclaration(property)) {
165+
if (!propsUtil.isPropTypesDeclaration(property) && !shouldCheckContextTypes(property)) {
156166
return;
157167
}
158168
if (property.value.type === 'ObjectExpression') {

lib/util/props.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ function isPropTypesDeclaration(node) {
2020
return astUtil.getPropertyName(node) === 'propTypes';
2121
}
2222

23+
/**
24+
* Checks if the node passed in looks like a contextTypes declaration.
25+
* @param {ASTNode} node The node to check.
26+
* @returns {Boolean} `true` if the node is a contextTypes declaration, `false` if not
27+
*/
28+
function isContextTypesDeclaration(node) {
29+
if (node && node.type === 'ClassProperty') {
30+
// Flow support
31+
if (node.typeAnnotation && node.key.name === 'context') {
32+
return true;
33+
}
34+
}
35+
return astUtil.getPropertyName(node) === 'contextTypes';
36+
}
37+
2338
/**
2439
* Checks if the Identifier node passed in looks like a defaultProps declaration.
2540
* @param {ASTNode} node The node to check. Must be an Identifier node.
@@ -41,6 +56,7 @@ function isRequiredPropType(propTypeExpression) {
4156

4257
module.exports = {
4358
isPropTypesDeclaration: isPropTypesDeclaration,
59+
isContextTypesDeclaration: isContextTypesDeclaration,
4460
isDefaultPropsDeclaration: isDefaultPropsDeclaration,
4561
isRequiredPropType: isRequiredPropType
4662
};

0 commit comments

Comments
 (0)