Skip to content

forbid-prop-types: forbid contextTypes, childContextTypes #1533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/rules/forbid-prop-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ class Component extends React.Component {

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

### `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.
60 changes: 55 additions & 5 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const variableUtil = require('../util/variable');
const propsUtil = require('../util/props');
const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -32,6 +33,12 @@ module.exports = {
items: {
type: 'string'
}
},
checkContextTypes: {
type: 'boolean'
},
checkChildContextTypes: {
type: 'boolean'
}
},
additionalProperties: true
Expand All @@ -40,14 +47,29 @@ module.exports = {

create: function(context) {
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 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;
}

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 @@ -132,27 +154,55 @@ module.exports = {

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

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

checkNode(node.parent.right);
},

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

const returnStatement = astUtil.findReturnStatement(node);

if (returnStatement && returnStatement.argument) {
checkNode(returnStatement.argument);
}
},

ObjectExpression: function(node) {
node.properties.forEach(property => {
if (!property.key) {
return;
}

if (!propsUtil.isPropTypesDeclaration(property)) {
if (
!propsUtil.isPropTypesDeclaration(property) &&
!shouldCheckContextTypes(property) &&
!shouldCheckChildContextTypes(property)
) {
return;
}
if (property.value.type === 'ObjectExpression') {
Expand Down
20 changes: 2 additions & 18 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const util = require('util');
const doctrine = require('doctrine');
const variableUtil = require('./variable');
const pragmaUtil = require('./pragma');
const astUtil = require('./ast');

const usedPropTypesAreEquivalent = (propA, propB) => {
if (propA.name === propB.name) {
Expand Down Expand Up @@ -356,24 +357,7 @@ function componentRule(rule, context) {
*
* @param {ASTNode} ASTnode The AST node being checked
*/
findReturnStatement: function(node) {
if (
(!node.value || !node.value.body || !node.value.body.body) &&
(!node.body || !node.body.body)
) {
return false;
}

const bodyNodes = (node.value ? node.value.body.body : node.body.body);

let i = bodyNodes.length - 1;
for (; i >= 0; i--) {
if (bodyNodes[i].type === 'ReturnStatement') {
return bodyNodes[i];
}
}
return false;
},
findReturnStatement: astUtil.findReturnStatement,

/**
* Get the parent component node from the current scope
Expand Down
27 changes: 26 additions & 1 deletion lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,32 @@ function getComponentProperties(node) {
}
}

/**
* Find a return statment in the current node
*
* @param {ASTNode} ASTnode The AST node being checked
*/
function findReturnStatement(node) {
if (
(!node.value || !node.value.body || !node.value.body.body) &&
(!node.body || !node.body.body)
) {
return false;
}

const bodyNodes = (node.value ? node.value.body.body : node.body.body);

let i = bodyNodes.length - 1;
for (; i >= 0; i--) {
if (bodyNodes[i].type === 'ReturnStatement') {
return bodyNodes[i];
}
}
return false;
}

module.exports = {
getPropertyName: getPropertyName,
getComponentProperties: getComponentProperties
getComponentProperties: getComponentProperties,
findReturnStatement: findReturnStatement
};
26 changes: 26 additions & 0 deletions lib/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ 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 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 @@ -41,6 +65,8 @@ function isRequiredPropType(propTypeExpression) {

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