Skip to content

Commit

Permalink
Merge pull request #1018 from wyze/fix-require-default-props-with-flo…
Browse files Browse the repository at this point in the history
…w-assignment

Fix `require-default-props` rule when using Flow type from assignment
  • Loading branch information
ljharb committed Jan 27, 2017
2 parents 5827897 + 0e8a1be commit 1bbbbba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rules/require-default-props.js
Expand Up @@ -161,7 +161,12 @@ module.exports = {
switch (node.typeAnnotation.type) {
case 'GenericTypeAnnotation':
var annotation = resolveGenericTypeAnnotation(node.typeAnnotation);
properties = annotation ? annotation.properties : [];

if (annotation && annotation.id) {
annotation = findVariableByName(annotation.id.name);
}

properties = annotation ? (annotation.properties || []) : [];
break;

case 'UnionTypeAnnotation':
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/require-default-props.js
Expand Up @@ -677,6 +677,39 @@ ruleTester.run('require-default-props', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
},
{
code: [
'import type ImportedProps from "fake";',
'type Props = ImportedProps;',
'function Hello(props: Props) {',
' return <div>Hello {props.name.firstname}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
},
// don't error when variable is not in scope
{
code: [
'import type { ImportedType } from "fake";',
'type Props = ImportedType;',
'function Hello(props: Props) {',
' return <div>Hello {props.name.firstname}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
},
// make sure error is not thrown with multiple assignments
{
code: [
'import type ImportedProps from "fake";',
'type NestedProps = ImportedProps;',
'type Props = NestedProps;',
'function Hello(props: Props) {',
' return <div>Hello {props.name.firstname}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 1bbbbba

Please sign in to comment.