diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index 1d0bb94dd9..4c2c146c95 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -619,7 +619,7 @@ module.exports = { }); break; case 'destructuring': - for (var k = 0, l = properties.length; k < l; k++) { + for (var k = 0, l = (properties || []).length; k < l; k++) { if (hasSpreadOperator(properties[k]) || properties[k].computed) { continue; } diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 986a42dd84..c703f42536 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1382,6 +1382,33 @@ ruleTester.run('no-unused-prop-types', rule, { ].join('\n'), parser: 'babel-eslint', parserOptions: parserOptions + }, { + // Destructured state props in `componentDidUpdate` [Issue #825] + code: [ + 'class Hello extends Component {', + ' static propTypes = {', + ' something: PropTypes.bool', + ' }', + ' componentDidUpdate ({something}, {state1, state2}) {', + ' return something;', + ' }', + '}' + ].join('\n'), + parser: 'babel-eslint', + parserOptions: parserOptions + }, { + // Destructured state props in `componentDidUpdate` without custom parser [Issue #825] + code: [ + 'var Hello = React.Component({', + ' propTypes: {', + ' something: PropTypes.bool', + ' },', + ' componentDidUpdate: function ({something}, {state1, state2}) {', + ' return something;', + ' }', + '});' + ].join('\n'), + parserOptions: parserOptions } ], @@ -2337,6 +2364,41 @@ ruleTester.run('no-unused-prop-types', rule, { line: 3, column: 13 }] + }, { + code: [ + 'class Hello extends Component {', + ' static propTypes = {', + ' something: PropTypes.bool', + ' }', + ' componentDidUpdate (prevProps, {state1, state2}) {', + ' return something;', + ' }', + '}' + ].join('\n'), + parser: 'babel-eslint', + parserOptions: parserOptions, + errors: [{ + message: '\'something\' PropType is defined but prop is never used', + line: 3, + column: 16 + }] + }, { + code: [ + 'var Hello = React.createClass({', + ' propTypes: {', + ' something: PropTypes.bool', + ' },', + ' componentDidUpdate: function (prevProps, {state1, state2}) {', + ' return something;', + ' }', + '})' + ].join('\n'), + parserOptions: parserOptions, + errors: [{ + message: '\'something\' PropType is defined but prop is never used', + line: 3, + column: 16 + }] }/* , { // Enable this when the following issue is fixed // https://github.com/yannickcr/eslint-plugin-react/issues/296