Skip to content

Commit c558451

Browse files
authored
Merge pull request jsx-eslint#1651 from xjmdoo/master
prop-types doesn't check nextProps of componentWillReceiveProps
2 parents f4cab9a + 20dff22 commit c558451

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/rules/prop-types.js

+14
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,15 @@ module.exports = {
606606
type = 'destructuring';
607607
properties = node.params[0].properties;
608608
break;
609+
case 'MethodDefinition':
610+
const destructuring = node.value && node.value.params && node.value.params[0] && node.value.params[0].type === 'ObjectPattern';
611+
if (destructuring) {
612+
type = 'destructuring';
613+
properties = node.value.params[0].properties;
614+
break;
615+
} else {
616+
return;
617+
}
609618
case 'VariableDeclarator':
610619
for (let i = 0, j = node.id.properties.length; i < j; i++) {
611620
// let {props: {firstname}} = this
@@ -1018,6 +1027,11 @@ module.exports = {
10181027
},
10191028

10201029
MethodDefinition: function(node) {
1030+
const destructuring = node.value && node.value.params && node.value.params[0] && node.value.params[0].type === 'ObjectPattern';
1031+
if (node.key.name === 'componentWillReceiveProps' && destructuring) {
1032+
markPropTypesAsUsed(node);
1033+
}
1034+
10211035
if (!node.static || node.kind !== 'get' || !propsUtil.isPropTypesDeclaration(node)) {
10221036
return;
10231037
}

tests/lib/rules/prop-types.js

+39
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,45 @@ 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+
]
3118+
}, {
3119+
code: [
3120+
'class Hello extends Component {',
3121+
' componentWillReceiveProps({foo}) {',
3122+
' if (foo) {',
3123+
' return;',
3124+
' }',
3125+
' }',
3126+
' render() {',
3127+
' return <div bar={this.props.bar} />;',
3128+
' }',
3129+
'}',
3130+
'Hello.propTypes = {',
3131+
' bar: PropTypes.func',
3132+
' }'
3133+
].join('\n'),
3134+
errors: [
3135+
{message: '\'foo\' is missing in props validation'}
3136+
]
30983137
}, {
30993138
code: [
31003139
'class Hello extends React.Component {',

0 commit comments

Comments
 (0)