Skip to content
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

Merged

Conversation

hasparus
Copy link
Contributor

@hasparus hasparus commented Aug 6, 2020

馃憢

Solves part of #2584.

@@ -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)}</>',
Copy link
Member

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.

Copy link
Contributor Author

@hasparus hasparus Aug 9, 2020

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).

#2584 (comment)

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 };

Copy link
Member

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?

Copy link
Contributor Author

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.

tests/lib/rules/jsx-no-useless-fragment.js Show resolved Hide resolved
@ljharb ljharb force-pushed the no-useless-fragment-call-expression branch from f6fabc1 to f8e2b55 Compare August 12, 2020 05:59
@ljharb ljharb force-pushed the no-useless-fragment-call-expression branch from f8e2b55 to a77c0d1 Compare August 12, 2020 06:09
@ljharb ljharb merged commit a77c0d1 into jsx-eslint:master Aug 12, 2020
@hasparus hasparus deleted the no-useless-fragment-call-expression branch September 27, 2021 12:42
@Vishwas-75
Copy link

How to disable this jsx-no-useless-frragments

@ljharb
Copy link
Member

ljharb commented Jan 12, 2022

@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.

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

Successfully merging this pull request may close these issues.

None yet

3 participants