Skip to content

Commit

Permalink
Add checkContextTypes option to forbid-prop-types
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Nov 11, 2017
1 parent 4f3fc51 commit 9ab07e0
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 6 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,14 +44,18 @@ class Component extends React.Component {

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

### `forbid`

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

### `checkContextTypes`

Whether or not to check `contextTypes` 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.
20 changes: 15 additions & 5 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = {
items: {
type: 'string'
}
},
checkContextTypes: {
type: 'boolean'
}
},
additionalProperties: true
Expand All @@ -40,14 +43,21 @@ module.exports = {

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

function isForbidden(type) {
const configuration = context.options[0] || {};

const forbid = configuration.forbid || DEFAULTS;
return forbid.indexOf(type) >= 0;
}

function shouldCheckContextTypes(node) {
if (checkContextTypes && propsUtil.isContextTypesDeclaration(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 @@ -132,14 +142,14 @@ module.exports = {

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

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

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

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

/**
* Checks if the node passed in looks like a contextTypes declaration.
* @param {ASTNode} node The node to check.
* @returns {Boolean} `true` if the node is a contextTypes declaration, `false` if not
*/
function isContextTypesDeclaration(node) {
if (node && node.type === 'ClassProperty') {
// Flow support
if (node.typeAnnotation && node.key.name === 'context') {
return true;
}
}
return astUtil.getPropertyName(node) === 'contextTypes';
}

/**
* 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 @@ -41,6 +56,7 @@ function isRequiredPropType(propTypeExpression) {

module.exports = {
isPropTypesDeclaration: isPropTypesDeclaration,
isContextTypesDeclaration: isContextTypesDeclaration,
isDefaultPropsDeclaration: isDefaultPropsDeclaration,
isRequiredPropType: isRequiredPropType
};

0 comments on commit 9ab07e0

Please sign in to comment.