Skip to content

Commit 1bbbbba

Browse files
authored
Merge pull request #1018 from wyze/fix-require-default-props-with-flow-assignment
Fix `require-default-props` rule when using Flow type from assignment
2 parents 5827897 + 0e8a1be commit 1bbbbba

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
@@ -161,7 +161,12 @@ module.exports = {
161161
switch (node.typeAnnotation.type) {
162162
case 'GenericTypeAnnotation':
163163
var annotation = resolveGenericTypeAnnotation(node.typeAnnotation);
164-
properties = annotation ? annotation.properties : [];
164+
165+
if (annotation && annotation.id) {
166+
annotation = findVariableByName(annotation.id.name);
167+
}
168+
169+
properties = annotation ? (annotation.properties || []) : [];
165170
break;
166171

167172
case 'UnionTypeAnnotation':

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

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

0 commit comments

Comments
 (0)