Skip to content

Commit 1b0caca

Browse files
committed
Fix propTypes detection when declared before the component (fixes #472)
1 parent 45b3c27 commit 1b0caca

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/util/Components.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Components() {
2424
*
2525
* @param {ASTNode} node The AST node being added.
2626
* @param {Number} confidence Confidence in the component detection (0=banned, 1=maybe, 2=yes)
27+
* @returns {Object} Added component object
2728
*/
2829
Components.prototype.add = function(node, confidence) {
2930
var id = this._getId(node);
@@ -33,12 +34,13 @@ Components.prototype.add = function(node, confidence) {
3334
} else {
3435
this._list[id].confidence = Math.max(this._list[id].confidence, confidence);
3536
}
36-
return;
37+
return this._list[id];
3738
}
3839
this._list[id] = {
3940
node: node,
4041
confidence: confidence
4142
};
43+
return this._list[id];
4244
};
4345

4446
/**
@@ -333,7 +335,7 @@ function componentRule(rule, context) {
333335
}
334336

335337
// Return the component
336-
return components.get(node);
338+
return components.add(node, 1);
337339
}
338340
};
339341

tests/lib/rules/prop-types.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,20 @@ ruleTester.run('prop-types', rule, {
11451145
'}'
11461146
].join('\n'),
11471147
parser: 'babel-eslint'
1148+
}, {
1149+
code: [
1150+
'Card.propTypes = {',
1151+
' title: PropTypes.string.isRequired,',
1152+
' children: PropTypes.element.isRequired,',
1153+
' footer: PropTypes.node',
1154+
'}',
1155+
'function Card ({ title, children, footer }) {',
1156+
' return (',
1157+
' <div/>',
1158+
' )',
1159+
'}'
1160+
].join('\n'),
1161+
parserOptions: parserOptions
11481162
}
11491163
],
11501164

0 commit comments

Comments
 (0)