-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add forbid-prop-types rule that by default forbids any, array, and ob…
…ject
- Loading branch information
Showing
4 changed files
with
602 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Forbid certain propTypes (forbid-prop-types) | ||
|
||
By default this rule prevents vague prop types with more specific alternatives available (`any`, `array`, `object`), but any prop type can be disabled if desired. The defaults are chosen because they have obvious replacements. `any` should be replaced with, well, anything. `array` and `object` can be replaced with `arrayOf` and `shape`, respectively. | ||
|
||
## Rule Details | ||
|
||
This rule checks all JSX components and verifies that no forbidden propsTypes are used. | ||
This rule is off by default. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var Component = React.createClass({ | ||
propTypes: { | ||
a: React.PropTypes.any, | ||
r: React.PropTypes.array, | ||
o: React.PropTypes.object | ||
}, | ||
... | ||
}); | ||
|
||
class Component extends React.Component { | ||
... | ||
} | ||
Component.propTypes = { | ||
a: React.PropTypes.any, | ||
r: React.PropTypes.array, | ||
o: React.PropTypes.object | ||
}; | ||
|
||
class Component extends React.Component { | ||
static propTypes = { | ||
a: React.PropTypes.any, | ||
r: React.PropTypes.array, | ||
o: React.PropTypes.object | ||
} | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
``` | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"forbid-prop-types": [<enabled>, { "forbid": [<string>] }] | ||
... | ||
``` | ||
|
||
### `forbid` | ||
|
||
An array of strings, with the names of React.PropType keys that are forbidden. | ||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* @fileoverview Forbid certain propTypes | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEFAULTS = ['any', 'array', 'object']; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
function isForbidden(type) { | ||
var configuration = context.options[0] || {}; | ||
|
||
var forbid = configuration.forbid || DEFAULTS; | ||
return forbid.indexOf(type) >= 0; | ||
} | ||
|
||
/** | ||
* Checks if node is `propTypes` declaration | ||
* @param {ASTNode} node The AST node being checked. | ||
* @returns {Boolean} True if node is `propTypes` declaration, false if not. | ||
*/ | ||
function isPropTypesDeclaration(node) { | ||
|
||
// Special case for class properties | ||
// (babel-eslint does not expose property name so we have to rely on tokens) | ||
if (node.type === 'ClassProperty') { | ||
var tokens = context.getFirstTokens(node, 2); | ||
if (tokens[0].value === 'propTypes' || tokens[1].value === 'propTypes') { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
return Boolean( | ||
node && | ||
node.name === 'propTypes' | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Checks if propTypes declarations are forbidden | ||
* @param {Array} declarations The array of AST nodes being checked. | ||
* @returns {void} | ||
*/ | ||
function checkForbidden(declarations) { | ||
declarations.forEach(function(declaration) { | ||
if ( | ||
declaration.value.type === 'MemberExpression' && | ||
declaration.value.property && | ||
declaration.value.property.name && | ||
declaration.value.property.name === 'isRequired' | ||
) { | ||
declaration.value = declaration.value.object; | ||
} | ||
|
||
if (isForbidden(declaration.value.property.name)) { | ||
context.report(declaration, 'Prop type `' + declaration.value.property.name + '` is forbidden'); | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
ClassProperty: function(node) { | ||
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') { | ||
checkForbidden(node.value.properties); | ||
} | ||
}, | ||
|
||
MemberExpression: function(node) { | ||
if (isPropTypesDeclaration(node.property)) { | ||
var right = node.parent.right; | ||
if (right && right.type === 'ObjectExpression') { | ||
checkForbidden(right.properties); | ||
} | ||
} | ||
}, | ||
|
||
ObjectExpression: function(node) { | ||
node.properties.forEach(function(property) { | ||
if (!property.key) { | ||
return; | ||
} | ||
|
||
if (!isPropTypesDeclaration(property.key)) { | ||
return; | ||
} | ||
if (property.value.type === 'ObjectExpression') { | ||
checkForbidden(property.value.properties); | ||
} | ||
}); | ||
} | ||
|
||
}; | ||
}; | ||
|
||
module.exports.schema = [{ | ||
type: 'object', | ||
properties: { | ||
forbid: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
additionalProperties: true | ||
}]; |
Oops, something went wrong.