Skip to content

Commit

Permalink
Fix prop-types on annotated components (fixes #766)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Aug 15, 2016
1 parent 99bdb42 commit 827db0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ module.exports = {
function isAnnotatedFunctionPropsDeclaration(node) {
if (node && node.params && node.params.length) {
var tokens = context.getFirstTokens(node.params[0], 2);
var isAnnotated = (
node.params[0].typeAnnotation &&
node.params[0].typeAnnotation.typeAnnotation &&
node.params[0].typeAnnotation.typeAnnotation.id &&
node.params[0].typeAnnotation.typeAnnotation.id.name === 'Props'
);
var isAnnotated = node.params[0].typeAnnotation;
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
if (isAnnotated && (isDestructuredProps || isProps)) {
Expand Down Expand Up @@ -656,7 +651,11 @@ module.exports = {
* @param {propTypes} node The AST node containing the proptypes
*/
function markPropTypesAsDeclared(node, propTypes) {
var component = components.get(node);
var componentNode = node;
while (componentNode && !components.get(componentNode)) {
componentNode = componentNode.parent;
}
var component = components.get(componentNode);
var declaredPropTypes = component && component.declaredPropTypes || {};
var ignorePropsValidation = false;

Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,22 @@ ruleTester.run('prop-types', rule, {
column: 35,
type: 'Identifier'
}]
}, {
code: [
'type MyComponentProps = {',
' a: number,',
'};',
'function MyComponent({ a, b }: MyComponentProps) {',
' return <div />;',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: '\'b\' is missing in props validation',
line: 4,
column: 27,
type: 'Property'
}]
}
]
});

0 comments on commit 827db0a

Please sign in to comment.