From cea8591e651cb646749ffb952e478084069cee7e Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Fri, 19 May 2017 22:01:44 +0300 Subject: [PATCH 1/3] Add failing and working tests that were raised in the issue --- tests/lib/rules/no-unused-prop-types.js | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index cc4f3c248d..783ff42e1e 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1439,6 +1439,51 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // The next two test cases are related to: https://github.com/yannickcr/eslint-plugin-react/issues/1183 + code: [ + 'export default function SomeComponent(props) {', + ' const callback = () => {', + ' props.a(props.b);', + ' };', + '', + ' const anotherCallback = () => {};', + '', + ' return (', + ' ', + ' );', + '}', + '', + 'SomeComponent.propTypes = {', + ' a: React.PropTypes.func.isRequired,', + ' b: React.PropTypes.string.isRequired,', + ' c: React.PropTypes.string.isRequired,', + '};' + ].join('\n') + }, { + code: [ + 'export default function SomeComponent(props) {', + ' const callback = () => {', + ' props.a(props.b);', + ' };', + '', + ' return (', + ' ', + ' );', + '}', + '', + 'SomeComponent.propTypes = {', + ' a: React.PropTypes.func.isRequired,', + ' b: React.PropTypes.string.isRequired,', + ' c: React.PropTypes.string.isRequired,', + '};' + ].join('\n') } ], From f85ff448b8f1186095dc9e6bfa0e3b5d4bc3ccc7 Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Mon, 22 May 2017 07:48:56 +0300 Subject: [PATCH 2/3] Fix issue with overriding component usedPropTypes if the component already existed in the map --- lib/util/Components.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index d6d844be51..a21bb66e52 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -99,9 +99,12 @@ Components.prototype.list = function() { component = this.get(node); } if (component) { - usedPropTypes[this._getId(component.node)] = (this._list[i].usedPropTypes || []).filter(function(propType) { + var newUsedProps = (this._list[i].usedPropTypes || []).filter(function(propType) { return !propType.node || propType.node.kind !== 'init'; }); + + var componentId = this._getId(component.node); + usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps); } } // Assign used props in not confident components to the parent component From bf69820675bcc3aa5408cbb37259fdf17c24be4b Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Tue, 23 May 2017 23:26:43 +0300 Subject: [PATCH 3/3] Replace var with const for the new code that was added --- lib/util/Components.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index a21bb66e52..56db135ee4 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -99,11 +99,11 @@ Components.prototype.list = function() { component = this.get(node); } if (component) { - var newUsedProps = (this._list[i].usedPropTypes || []).filter(function(propType) { + const newUsedProps = (this._list[i].usedPropTypes || []).filter(function(propType) { return !propType.node || propType.node.kind !== 'init'; }); - var componentId = this._getId(component.node); + const componentId = this._getId(component.node); usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps); } }