From f79c63e6da2f7326c7a3d02bda1c501749f7f16d Mon Sep 17 00:00:00 2001 From: Evgueni Naverniouk Date: Tue, 13 Sep 2016 12:06:13 -0700 Subject: [PATCH] Fixes Issue #825 --- lib/rules/no-unused-prop-types.js | 2 +- tests/lib/rules/no-unused-prop-types.js | 32 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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..291323c141 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1382,6 +1382,20 @@ 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 } ], @@ -2337,6 +2351,24 @@ 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 + }] }/* , { // Enable this when the following issue is fixed // https://github.com/yannickcr/eslint-plugin-react/issues/296