Skip to content

Fix require-default-props rule when using Flow type from assignment #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,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