-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix require-default-props
rule when using Flow type from assignment
#1018
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems legit
There was a problem hiding this 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?
887c3b0
to
1b33d49
Compare
Hey @EvNaverniouk, can you check the failing case I added? Seems a bit off as I needed to provide |
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [] |
There was a problem hiding this comment.
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>;'
'}'
]
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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: [] |
There was a problem hiding this comment.
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 : []; |
There was a problem hiding this comment.
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 : [];
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>;
}
1b33d49
to
0e8a1be
Compare
up! |
This fixes the issue I reported in #1017.
The condition I added is hit when code like the following is used:
This just takes the right side of the annotation (
Message
) and tries to find that variable in scope and then continues on.