-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] add jsx-no-useless-fragment
rule
#2261
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
Conversation
Co-Authored-By: Jordan Harband <[email protected]>
Should this rule be autofixable? |
Autofixing Case like |
hm, maybe if there's cases that aren't safe to autofix, those should be separately controllable by an option? |
017c476
to
17cc2c7
Compare
Now fragments in html element are reported. Example: <section>
<>
<div />
<div />
</>
</section> |
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.
So any time a fragment is being passed to an HTML element, it should be autofixable to inline the fragment's children, no?
Now a fragment is fixable if it is not an outermost element, except for when it might add extra spaces. The fix is a simple removal of openingElement and closingElement. // fixing by simply removing fragments will add a space between "git" and "hub"
<div>
git<>
hub
</>
</div> |
Couldn't that be fixed into: <div>
git{`
hub
`}
</div> or <div>
git{' hub '}
</div> ? |
I think it is ok to leave these cases without a fix. Your fixed code both render "git hub", but my code renders "github". |
ah, |
Hey! I'm curious what the status of this PR is. I just wrote a similar rule myself before I saw this PR. If you plan on coming back and getting this across the finish line, I'll just add one more thing to consider: Fragments can be given props. Specifically If you don't plan on coming back to this, I'd be happy to open a PR with the one I've written once we having it running for a little while and I'm confident there are no major issues. |
@captbaritone please don't open a second PR; it'd be better to update this one. |
7425244
to
ce4336f
Compare
@@ -0,0 +1,50 @@ | |||
# Disallow unnecessary fragments (react/jsx-no-useless-fragment) | |||
|
|||
A fragment is redundant if it contains only one child, or if it is the child of a html element. |
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 like we need to also add prose and examples and tests showing that a fragment is not useless if it has a key
prop and also appears in a context where a key prop makes sense (ie, inside an array literal or returned from a function)
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.
I pushed a version just let pass any keyed fragment.
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.
If a fragment has only a single JSXElement child, it is still useless even if it does have a key.
For example:
<React.Fragment key={someVar}>
<div />
</React.Fragment>
Should be refactored/fixed to:
<div key={someVar} />
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 not entirely true; something might be cloning the element.
Another case which is worth considering:
|
@captbaritone given that you can use backticks for the same effect, it seems like using a fragment there is strictly worse than the alternative (making an object instead of a string literal) |
@ljharb from a technical/performance perspective, I think you’re right, but from a readability/developer experience, perspective I think there’s a reasonable argument for fragments. With back ticks, new lines and indentation can be annoying write cleanly. For example, I don’t think Prettier will word-wrap strings in back ticks for you (since it can’t know if it’s safe) where as I believe it will word wrap JSX for you since it knows line breaks are safe in white space. (I’m on mobile so I haven’t actually checked) So, forcing back ticks may also force users to manually handle line wrapping, which can be a bit annoying. |
Using fragments for linebreak-less strings can help avoid <Author
name="eeee"
description="eee eeeeee eee eeeee. eeeeeeee eeee eeeeeeeeeeeee eeee. eeeeeee eee eeeeeeeeee ee eee eeeeeee eee."
/>; <Author
name="eeee"
description={
<>
eee eeeeee eee eeeee. eeeeeeee eeee eeeeeeeeeeeee eeee. eeeeeee eee
eeeeeeeeee ee eee eeeeeee eee.
</>
}
/>; |
|
05a5abb
to
c025438
Compare
I have rewrite the fixer to fixes more cases. |
973b027
to
177a4c5
Compare
177a4c5
to
7ccff10
Compare
fixes #2042
This rule disallows fragments with less than 2 children.