From 4e1bcc0020834f06801a6987043e085658e72bd7 Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Sun, 21 Feb 2016 21:07:42 +0000 Subject: [PATCH] Fix used props detection in components for which we are not confident (fixes #420) --- lib/util/Components.js | 26 ++++++++++++++++++++++++-- tests/lib/rules/prop-types.js | 10 ++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index b0e190a67c..ee2e051824 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -77,11 +77,33 @@ Components.prototype.set = function(node, props) { */ Components.prototype.list = function() { var list = {}; + var usedPropTypes = {}; + // Find props used in components for which we are not confident for (var i in this._list) { - if (!this._list.hasOwnProperty(i) || this._list[i].confidence < 2) { + if (!this._list.hasOwnProperty(i) || this._list[i].confidence >= 2) { continue; } - list[i] = this._list[i]; + var component; + var node; + node = this._list[i].node; + while (!component && node.parent) { + node = node.parent; + component = this.get(node); + } + if (component) { + usedPropTypes[this._getId(component.node)] = this._list[i].usedPropTypes; + } + } + // Assign used props in not confident components to the parent component + for (var j in this._list) { + if (!this._list.hasOwnProperty(j) || this._list[j].confidence < 2) { + continue; + } + var id = this._getId(this._list[j].node); + list[j] = this._list[j]; + if (usedPropTypes[id]) { + list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]); + } } return list; }; diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 8d9c2cfc9c..263948eab9 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -1937,6 +1937,16 @@ ruleTester.run('prop-types', rule, { errors: [{ message: '\'names\' is missing in props validation' }] + }, { + code: [ + 'const MyComponent = props => (', + '
props.toggle()}>
', + ')' + ].join('\n'), + parserOptions: parserOptions, + errors: [{ + message: '\'toggle\' is missing in props validation' + }] } ] });