diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index e6226d80dc..a17d7cd0e9 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -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 @@ -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; } diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 08153a711a..3ee4072ac3 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -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
;', + ' }', + '}' + ].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 ;', + ' }', + '}', + 'Hello.propTypes = {', + ' bar: PropTypes.func', + ' }' + ].join('\n'), + errors: [ + {message: '\'foo\' is missing in props validation'} + ] }, { code: [ 'class Hello extends React.Component {',