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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix] jsx-no-useless-fragment
: accept fragments with call expressions
#2744
[Fix] jsx-no-useless-fragment
: accept fragments with call expressions
#2744
Conversation
@@ -63,6 +63,10 @@ ruleTester.run('jsx-no-useless-fragment', rule, { | |||
{ | |||
code: '<Fooo content={<>eeee ee eeeeeee eeeeeeee</>} />', | |||
parser: parsers.BABEL_ESLINT | |||
}, | |||
{ | |||
code: '<>{foos.map(foo => foo)}</>', |
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.
this seems like a useless fragment to me tho? since foos.map
produces an array, and an array is a valid render value.
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.
In case if foos
is an array, the fragment wraps an array (a React node) into a React element. It's not useless if it's intentional (e.g. a function where you pass this treats arrays different than single elements -- requires a single child or sth like that).
But actually, we can't assume what map
does, and even if map
suggests an array, we treat all call expressions the same. It may return undefined
and then we have a crash at runtime if render it without wrapping in the fragment.
const foos = { map: () => undefined };
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 crash at runtime? A component that returns [undefined]
is a valid component.
You're right that it wouldn't be useless if it's being passed somewhere, but since the introduction of fragments it seems like anything that accepts a fragment should also accept an array?
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 crash at runtime? A component that returns [undefined] is a valid component.
Yes, but it's not valid if it returns undefined
. There is no array here.
This doesn't have to be map
, but even when the function is called "map" we have no guarantee that this is Array.prototype.map
. A function may return undefined.
f6fabc1
to
f8e2b55
Compare
f8e2b55
to
a77c0d1
Compare
How to disable this jsx-no-useless-frragments |
@Vishwas-75 same way to disable any rule; set it to "off" in your eslint config, or with an inline eslint override comment. eslint's docs are a good resource. |
馃憢
Solves part of #2584.