Skip to content

Commit 0e8a1be

Browse files
committed
Fix require-default-props rule when using Flow type from assignment
1 parent a33db3b commit 0e8a1be

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/rules/require-default-props.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ module.exports = {
141141
switch (node.typeAnnotation.type) {
142142
case 'GenericTypeAnnotation':
143143
var annotation = resolveGenericTypeAnnotation(node.typeAnnotation);
144-
properties = annotation ? annotation.properties : [];
144+
145+
if (annotation && annotation.id) {
146+
annotation = findVariableByName(annotation.id.name);
147+
}
148+
149+
properties = annotation ? (annotation.properties || []) : [];
145150
break;
146151

147152
case 'UnionTypeAnnotation':

tests/lib/rules/require-default-props.js

+33
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,39 @@ ruleTester.run('require-default-props', rule, {
676676
'}'
677677
].join('\n'),
678678
parser: 'babel-eslint'
679+
},
680+
{
681+
code: [
682+
'import type ImportedProps from "fake";',
683+
'type Props = ImportedProps;',
684+
'function Hello(props: Props) {',
685+
' return <div>Hello {props.name.firstname}</div>;',
686+
'}'
687+
].join('\n'),
688+
parser: 'babel-eslint'
689+
},
690+
// don't error when variable is not in scope
691+
{
692+
code: [
693+
'import type { ImportedType } from "fake";',
694+
'type Props = ImportedType;',
695+
'function Hello(props: Props) {',
696+
' return <div>Hello {props.name.firstname}</div>;',
697+
'}'
698+
].join('\n'),
699+
parser: 'babel-eslint'
700+
},
701+
// make sure error is not thrown with multiple assignments
702+
{
703+
code: [
704+
'import type ImportedProps from "fake";',
705+
'type NestedProps = ImportedProps;',
706+
'type Props = NestedProps;',
707+
'function Hello(props: Props) {',
708+
' return <div>Hello {props.name.firstname}</div>;',
709+
'}'
710+
].join('\n'),
711+
parser: 'babel-eslint'
679712
}
680713
],
681714

0 commit comments

Comments
 (0)