Skip to content

Commit

Permalink
Add checkChildContextTypes for forbid-prop-types
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Nov 11, 2017
1 parent 9ab07e0 commit da82f2b
Show file tree
Hide file tree
Showing 4 changed files with 450 additions and 18 deletions.
6 changes: 5 additions & 1 deletion docs/rules/forbid-prop-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Component extends React.Component {

```js
...
"react/forbid-prop-types": [<enabled>, { "forbid": [<string>], checkContextTypes: <boolean> }]
"react/forbid-prop-types": [<enabled>, { "forbid": [<string>], checkContextTypes: <boolean>, checkChildContextTypes: <boolean> }]
...
```

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

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

### `checkChildContextTypes`

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

## When not to use

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.
29 changes: 26 additions & 3 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = {
},
checkContextTypes: {
type: 'boolean'
},
checkChildContextTypes: {
type: 'boolean'
}
},
additionalProperties: true
Expand All @@ -45,6 +48,7 @@ module.exports = {
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
const configuration = context.options[0] || {};
const checkContextTypes = configuration.checkContextTypes || false;
const checkChildContextTypes = configuration.checkChildContextTypes || false;

function isForbidden(type) {
const forbid = configuration.forbid || DEFAULTS;
Expand All @@ -58,6 +62,13 @@ module.exports = {
return false;
}

function shouldCheckChildContextTypes(node) {
if (checkChildContextTypes && propsUtil.isChildContextTypesDeclaration(node)) {
return true;
}
return false;
}

/**
* Find a variable by name in the current scope.
* @param {string} name Name of the variable to look for.
Expand Down Expand Up @@ -142,14 +153,22 @@ module.exports = {

return {
ClassProperty: function(node) {
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
if (
!propsUtil.isPropTypesDeclaration(node) &&
!shouldCheckContextTypes(node) &&
!shouldCheckChildContextTypes(node)
) {
return;
}
checkNode(node.value);
},

MemberExpression: function(node) {
if (!propsUtil.isPropTypesDeclaration(node) && !shouldCheckContextTypes(node)) {
if (
!propsUtil.isPropTypesDeclaration(node) &&
!shouldCheckContextTypes(node) &&
!shouldCheckChildContextTypes(node)
) {
return;
}

Expand All @@ -162,7 +181,11 @@ module.exports = {
return;
}

if (!propsUtil.isPropTypesDeclaration(property) && !shouldCheckContextTypes(property)) {
if (
!propsUtil.isPropTypesDeclaration(property) &&
!shouldCheckContextTypes(property) &&
!shouldCheckChildContextTypes(property)
) {
return;
}
if (property.value.type === 'ObjectExpression') {
Expand Down
10 changes: 10 additions & 0 deletions lib/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ function isContextTypesDeclaration(node) {
return astUtil.getPropertyName(node) === 'contextTypes';
}

/**
* Checks if the node passed in looks like a childContextTypes declaration.
* @param {ASTNode} node The node to check.
* @returns {Boolean} `true` if the node is a childContextTypes declaration, `false` if not
*/
function isChildContextTypesDeclaration(node) {
return astUtil.getPropertyName(node) === 'childContextTypes';
}

/**
* Checks if the Identifier node passed in looks like a defaultProps declaration.
* @param {ASTNode} node The node to check. Must be an Identifier node.
Expand All @@ -57,6 +66,7 @@ function isRequiredPropType(propTypeExpression) {
module.exports = {
isPropTypesDeclaration: isPropTypesDeclaration,
isContextTypesDeclaration: isContextTypesDeclaration,
isChildContextTypesDeclaration: isChildContextTypesDeclaration,
isDefaultPropsDeclaration: isDefaultPropsDeclaration,
isRequiredPropType: isRequiredPropType
};

0 comments on commit da82f2b

Please sign in to comment.