Skip to content

Commit f9cc10d

Browse files
author
Tamas Sule
committed
prop-types doesn't check nextProps of componentWillReceiveProps
1 parent 36beb6d commit f9cc10d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/rules/prop-types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ module.exports = {
606606
type = 'destructuring';
607607
properties = node.params[0].properties;
608608
break;
609+
case 'MethodDefinition':
610+
type = 'destructuring';
611+
properties = node.value.params[0].properties;
612+
break;
609613
case 'VariableDeclarator':
610614
for (let i = 0, j = node.id.properties.length; i < j; i++) {
611615
// let {props: {firstname}} = this
@@ -1018,6 +1022,11 @@ module.exports = {
10181022
},
10191023

10201024
MethodDefinition: function(node) {
1025+
const destructuring = node.value && node.value.params && node.value.params[0] && node.value.params[0].type === 'ObjectPattern';
1026+
if (node.key.name === 'componentWillReceiveProps' && destructuring) {
1027+
markPropTypesAsUsed(node);
1028+
}
1029+
10211030
if (!node.static || node.kind !== 'get' || !propsUtil.isPropTypesDeclaration(node)) {
10221031
return;
10231032
}

tests/lib/rules/prop-types.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,26 @@ ruleTester.run('prop-types', rule, {
30953095
errors: [
30963096
{message: '\'foo\' is missing in props validation'}
30973097
]
3098+
}, {
3099+
code: [
3100+
'class Hello extends Component {',
3101+
' static propTypes = {',
3102+
' bar: PropTypes.func',
3103+
' }',
3104+
' componentWillReceiveProps({foo}) {',
3105+
' if (foo) {',
3106+
' return;',
3107+
' }',
3108+
' }',
3109+
' render() {',
3110+
' return <div bar={this.props.bar} />;',
3111+
' }',
3112+
'}'
3113+
].join('\n'),
3114+
parser: 'babel-eslint',
3115+
errors: [
3116+
{message: '\'foo\' is missing in props validation'}
3117+
]
30983118
}, {
30993119
code: [
31003120
'class Hello extends React.Component {',

0 commit comments

Comments
 (0)