Skip to content

Commit

Permalink
Merge pull request #1651 from xjmdoo/master
Browse files Browse the repository at this point in the history
prop-types doesn't check nextProps of componentWillReceiveProps
  • Loading branch information
ljharb authored Jan 21, 2018
2 parents f4cab9a + 20dff22 commit c558451
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ module.exports = {
type = 'destructuring';
properties = node.params[0].properties;
break;
case 'MethodDefinition':
const destructuring = node.value && node.value.params && node.value.params[0] && node.value.params[0].type === 'ObjectPattern';
if (destructuring) {
type = 'destructuring';
properties = node.value.params[0].properties;
break;
} else {
return;
}
case 'VariableDeclarator':
for (let i = 0, j = node.id.properties.length; i < j; i++) {
// let {props: {firstname}} = this
Expand Down Expand Up @@ -1018,6 +1027,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
39 changes: 39 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3095,6 +3095,45 @@ 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 Component {',
' componentWillReceiveProps({foo}) {',
' if (foo) {',
' return;',
' }',
' }',
' render() {',
' return <div bar={this.props.bar} />;',
' }',
'}',
'Hello.propTypes = {',
' bar: PropTypes.func',
' }'
].join('\n'),
errors: [
{message: '\'foo\' is missing in props validation'}
]
}, {
code: [
'class Hello extends React.Component {',
Expand Down

0 comments on commit c558451

Please sign in to comment.