Skip to content

Commit 4e1bcc0

Browse files
committed
Fix used props detection in components for which we are not confident (fixes #420)
1 parent b898fcb commit 4e1bcc0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/util/Components.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,33 @@ Components.prototype.set = function(node, props) {
7777
*/
7878
Components.prototype.list = function() {
7979
var list = {};
80+
var usedPropTypes = {};
81+
// Find props used in components for which we are not confident
8082
for (var i in this._list) {
81-
if (!this._list.hasOwnProperty(i) || this._list[i].confidence < 2) {
83+
if (!this._list.hasOwnProperty(i) || this._list[i].confidence >= 2) {
8284
continue;
8385
}
84-
list[i] = this._list[i];
86+
var component;
87+
var node;
88+
node = this._list[i].node;
89+
while (!component && node.parent) {
90+
node = node.parent;
91+
component = this.get(node);
92+
}
93+
if (component) {
94+
usedPropTypes[this._getId(component.node)] = this._list[i].usedPropTypes;
95+
}
96+
}
97+
// Assign used props in not confident components to the parent component
98+
for (var j in this._list) {
99+
if (!this._list.hasOwnProperty(j) || this._list[j].confidence < 2) {
100+
continue;
101+
}
102+
var id = this._getId(this._list[j].node);
103+
list[j] = this._list[j];
104+
if (usedPropTypes[id]) {
105+
list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]);
106+
}
85107
}
86108
return list;
87109
};

tests/lib/rules/prop-types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,16 @@ ruleTester.run('prop-types', rule, {
19371937
errors: [{
19381938
message: '\'names\' is missing in props validation'
19391939
}]
1940+
}, {
1941+
code: [
1942+
'const MyComponent = props => (',
1943+
' <div onClick={() => props.toggle()}></div>',
1944+
')'
1945+
].join('\n'),
1946+
parserOptions: parserOptions,
1947+
errors: [{
1948+
message: '\'toggle\' is missing in props validation'
1949+
}]
19401950
}
19411951
]
19421952
});

0 commit comments

Comments
 (0)