Skip to content

Commit 827db0a

Browse files
committed
Fix prop-types on annotated components (fixes jsx-eslint#766)
1 parent 99bdb42 commit 827db0a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/rules/prop-types.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ module.exports = {
118118
function isAnnotatedFunctionPropsDeclaration(node) {
119119
if (node && node.params && node.params.length) {
120120
var tokens = context.getFirstTokens(node.params[0], 2);
121-
var isAnnotated = (
122-
node.params[0].typeAnnotation &&
123-
node.params[0].typeAnnotation.typeAnnotation &&
124-
node.params[0].typeAnnotation.typeAnnotation.id &&
125-
node.params[0].typeAnnotation.typeAnnotation.id.name === 'Props'
126-
);
121+
var isAnnotated = node.params[0].typeAnnotation;
127122
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
128123
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
129124
if (isAnnotated && (isDestructuredProps || isProps)) {
@@ -656,7 +651,11 @@ module.exports = {
656651
* @param {propTypes} node The AST node containing the proptypes
657652
*/
658653
function markPropTypesAsDeclared(node, propTypes) {
659-
var component = components.get(node);
654+
var componentNode = node;
655+
while (componentNode && !components.get(componentNode)) {
656+
componentNode = componentNode.parent;
657+
}
658+
var component = components.get(componentNode);
660659
var declaredPropTypes = component && component.declaredPropTypes || {};
661660
var ignorePropsValidation = false;
662661

tests/lib/rules/prop-types.js

+16
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,22 @@ ruleTester.run('prop-types', rule, {
22852285
column: 35,
22862286
type: 'Identifier'
22872287
}]
2288+
}, {
2289+
code: [
2290+
'type MyComponentProps = {',
2291+
' a: number,',
2292+
'};',
2293+
'function MyComponent({ a, b }: MyComponentProps) {',
2294+
' return <div />;',
2295+
'}'
2296+
].join('\n'),
2297+
parser: 'babel-eslint',
2298+
errors: [{
2299+
message: '\'b\' is missing in props validation',
2300+
line: 4,
2301+
column: 27,
2302+
type: 'Property'
2303+
}]
22882304
}
22892305
]
22902306
});

0 commit comments

Comments
 (0)