-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Rule proposal: detect unnecessary use of fragments #2042
Comments
This is a good rule (altho until enzyme supports rendering non-nodes, a fragment with a non-node child is still necessary, so perhaps that should be controllable via a separate option). Both forms - syntax and API - should be checked. |
I would love to work on this one! Could you elaborate more about both options? How would look like "non-node" child? Code snippet would be helpful. |
Other cases:
const Foo = () => (
<div>
<>
<div />
<div />
</>
</div>
)
const Goo = () => (
<div>
<>
<div />
<div />
</>
<section />
</div>
)
const Hoo = () => (
<Moo>
<>
<div />
<div />
</>
</Moo>
) |
I do think it’s always useless to pass a fragment as a child to an html element; the rule should warn on that too. |
Here comes the problem of whether a tag is a html element, also noted in #1752 (comment). Maybe in this case it is ok to maintain a list of html tags in this repo, since in #1752 an omission of an html tag name is false positive, whereas in this rule, an omission is false negative. |
It’s an html element if the tag name is lowercase and doesn’t have a dot in it; that’s how jsx works - that comment is about knowing whether it’s a valid tag name or not. |
I suppose the autofix for that case wouldn’t be as safe if it wasn’t a valid tag name; but we could still warn on it. |
When can we expect this to be released? It's been some time since this has been finished ... |
What if I have a component that looks like this (very trivial example, usually the stuff in the map is a bit more complicated):
The rule reports an error but I think there shouldn't be one. Obviously it is possible that there will be 0, 1 or more children inside fragment, but I think there should be way to disable the rule for these situations through config. |
@paolostyle in that case, you're already missing a |
That was just an artificial example, I always have keys in arrays in actual code because I use a linting rule for that, I don't think that's relevant here. My whole codebase is in Typescript and I can't return an array from function components (and 95% of them are function components), there's a couple of links to Typescript and React types GH issues in this SO thread: https://stackoverflow.com/questions/46643517/return-react-16-array-elements-in-typescript. I suppose my example is actually just a workaround so I just won't use this rule until it's fixed in TS/React types. Looking at how old this issue is I probably won't ever use it but oh well. |
TypeScript having broken types for React is something that TS needs to fix; my suggested workaround would be to not use TS :-p (but that SO answer tells you how to patch the types so it works) |
The idea behind fragments is to group a list of children without adding extra nodes to the DOM.
Fragments are completely redundant when wrapping a single child, so the rule should detect examples like:
and flag them as a warning/error.
Example of valid usage of fragments:
(any case where
children.length > 1
)The text was updated successfully, but these errors were encountered: