Skip to content

Commit

Permalink
prop-types doesn't check nextProps of componentWillReceiveProps
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamas Sule committed Jan 20, 2018
1 parent 36beb6d commit f9cc10d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rules/prop-types.js
Expand Up @@ -606,6 +606,10 @@ module.exports = {
type = 'destructuring';
properties = node.params[0].properties;
break;
case 'MethodDefinition':
type = 'destructuring';
properties = node.value.params[0].properties;
break;
case 'VariableDeclarator':
for (let i = 0, j = node.id.properties.length; i < j; i++) {
// let {props: {firstname}} = this
Expand Down Expand Up @@ -1018,6 +1022,11 @@ module.exports = {
},

MethodDefinition: function(node) {
const destructuring = node.value && node.value.params && node.value.params[0] && node.value.params[0].type === 'ObjectPattern';
if (node.key.name === 'componentWillReceiveProps' && destructuring) {
markPropTypesAsUsed(node);
}

if (!node.static || node.kind !== 'get' || !propsUtil.isPropTypesDeclaration(node)) {
return;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -3095,6 +3095,26 @@ ruleTester.run('prop-types', rule, {
errors: [
{message: '\'foo\' is missing in props validation'}
]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' bar: PropTypes.func',
' }',
' componentWillReceiveProps({foo}) {',
' if (foo) {',
' return;',
' }',
' }',
' render() {',
' return <div bar={this.props.bar} />;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'foo\' is missing in props validation'}
]
}, {
code: [
'class Hello extends React.Component {',
Expand Down

0 comments on commit f9cc10d

Please sign in to comment.