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

Conversation

wyze
Copy link
Contributor

@wyze wyze commented Jan 11, 2017

This fixes the issue I reported in #1017.

The condition I added is hit when code like the following is used:

type Props = Message

This just takes the right side of the annotation (Message) and tries to find that variable in scope and then continues on.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems legit

@ljharb ljharb added the bug label Jan 11, 2017
Copy link
Collaborator

@EvHaus EvHaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a failing test, such as in the event that Flow type is used but the variable name can't be found?

@wyze wyze force-pushed the fix-require-default-props-with-flow-assignment branch from 887c3b0 to 1b33d49 Compare January 11, 2017 16:39
@wyze
Copy link
Contributor Author

wyze commented Jan 11, 2017

Hey @EvNaverniouk, can you check the failing case I added? Seems a bit off as I needed to provide errors: [] because it is under invalid key in the test suite, but it doesn't actually output any errors.

'}'
].join('\n'),
parser: 'babel-eslint',
errors: []
Copy link
Collaborator

@EvHaus EvHaus Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this to return errors so we can test for it, perhaps in the event that the type isn't defined. Maybe something like this:

code: [
   'function Hello (props: UndefinedVariable) {',
   '    return <div>Hello {props.name.firstname}</div>;'
   '}'
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say "update this", are you referring to the rule, to throw an error if it can't find a variable? If so, I think that should be a separate PR.

If you are referring to the test case when you say "update this", then when making the requested changes, the invalid test still passes with errors: [] so I am not sure there is much difference between what you are suggesting and what I added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I'm misunderstanding what the scope of the changes in this PR. I was under the impression that it adds support for Flow types as valid prop assignments via variables. Your original test case covers the fact the the rule properly catches such valid Flow type assignments without error.

What I'm asking for is to add a test case that when a Flow type is defined, but the Flow type is variable is unassigned. I believe this ESLint rule should still throw an error in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's covered by the no-undef rules, is it not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be covered by no-undef. The require-default-props rule should just try its bests to resolve the props and the defaultProps, and give up if it cannot do it.

If this test case do not return any error then it should be in the "valid" array.

'}'
].join('\n'),
parser: 'babel-eslint',
errors: []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be covered by no-undef. The require-default-props rule should just try its bests to resolve the props and the defaultProps, and give up if it cannot do it.

If this test case do not return any error then it should be in the "valid" array.

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

properties = annotation ? annotation.properties : [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also update the next line to check for annotation.properties existence, or the rule will keep crashing if there is multiple reassignments

properties = annotation && annotation.properties ? annotation.properties : [];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be a test case that would trigger that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, technically, my fix prevented this, but if for some reason you do multiple assignments, it would trigger the original error again:

import type ImportedProps from "fake";
type NestedProps = ImportedProps;
type Props = NestedProps;
function Hello(props: Props) {
  return <div>Hello {props.name.firstname}</div>;
}

@wyze wyze force-pushed the fix-require-default-props-with-flow-assignment branch from 1b33d49 to 0e8a1be Compare January 11, 2017 22:15
@cr0cK
Copy link

cr0cK commented Jan 27, 2017

up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

5 participants