Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prop-types doesn't check nextProps of componentWillReceiveProps #1651

Merged
merged 2 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a duplicate test that uses the default parser

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