Skip to content

Commit

Permalink
Fix used props detection in components for which we are not confident (
Browse files Browse the repository at this point in the history
…fixes #420)
  • Loading branch information
yannickcr committed Feb 21, 2016
1 parent b898fcb commit 4e1bcc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,16 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'names\' is missing in props validation'
}]
}, {
code: [
'const MyComponent = props => (',
' <div onClick={() => props.toggle()}></div>',
')'
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: '\'toggle\' is missing in props validation'
}]
}
]
});

0 comments on commit 4e1bcc0

Please sign in to comment.