-
-
Notifications
You must be signed in to change notification settings - Fork 506
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
💅 noUselessFragments
false negatives
#4751
Comments
The second case declare const condition: boolean;
const C2 = () => <div>{condition ? "foo" : "bar"}</div>; maybe this is not a good way to fix the fragment here 🤔? The first case same. |
Why not? There might be a reason, especially if the equivalent ESLint rule isn't doing this, but I can't think what that reason might be. 🤔 |
I'm not sure... maybe you are right. I will fix the bug in your issue first. Let's try to ensure that the behavior is aligned with eslint. The other behaviour will need to be discussed(or refer to why eslint do't support this, IMO, eslint support this is easier than biome), i can also submit another pr to support this. |
// ESLint: no error 🤔
// Biome: no error 🤔
const C1 = () => <>foo</>; In this example, it can't be an error, because you essentially change what's returned by the function. When you use a fragment, you signal that you're returning a JSX component. If you remove the Fragment, you'll return a variable. Removing the fragment will break your code (especially if your code is typed). // ESLint: no error 🤔
// Biome: no error 🤔
declare const condition: boolean;
const C2 = () => <div>{condition ? <>foo</> : <>bar</>}</div>; Here |
The current fix for the rule breaks some TypeScript usecases. Some examples of times when the autofix causes problems if an element is expected:
|
Usually you can widen the type to |
Widening the scope to ReactNode only works if you have control over the component that you're passing things into. A more concrete example that causes problems (regardless of typing) is HTML entities. The current rules seem to treat them as if they're just text strings, but they're not.
gets transformed into:
So if you use it later on like:
You wind up with the text string "& nbsp; " shown on the page instead of a non-breaking space. |
Environment information
Rule name
noUselessFragments
Playground link
https://biomejs.dev/playground/?code=LwAvACAARQBTAEwAaQBuAHQAOgAgAGUAcgByAG8AcgAKAC8ALwAgAEIAaQBvAG0AZQA6ACAAbgBvACAAZQByAHIAbwByACAATCcKADwAcwBlAGMAdABpAG8AbgA%2BAAoAIAAgADwAPgAKACAAIAAgACAAPABkAGkAdgAgAC8APgAKACAAIAAgACAAPABkAGkAdgAgAC8APgAKACAAIAA8AC8APgAKADwALwBzAGUAYwB0AGkAbwBuAD4AOwAKAAoALwAvACAARQBTAEwAaQBuAHQAOgAgAGUAcgByAG8AcgAKAC8ALwAgAEIAaQBvAG0AZQA6ACAAbgBvACAAZQByAHIAbwByACAATCcKAHMAaABvAHcARgB1AGwAbABOAGEAbQBlACAAPwAgADwAPgB7AGYAdQBsAGwATgBhAG0AZQB9ADwALwA%2BACAAOgAgADwAPgB7AGYAaQByAHMAdABOAGEAbQBlAH0APAAvAD4AOwAKAAoALwAvACAARQBTAEwAaQBuAHQAOgAgAG4AbwAgAGUAcgByAG8AcgAgAEwnCgAvAC8AIABCAGkAbwBtAGUAOgAgAG4AbwAgAGUAcgByAG8AcgAgAEwnCgBjAG8AbgBzAHQAIABDADEAIAA9ACAAKAApACAAPQA%2BACAAPAA%2BAGYAbwBvADwALwA%2BADsACgAKAC8ALwAgAEUAUwBMAGkAbgB0ADoAIABuAG8AIABlAHIAcgBvAHIAIABMJwoALwAvACAAQgBpAG8AbQBlADoAIABuAG8AIABlAHIAcgBvAHIAIABMJwoAZABlAGMAbABhAHIAZQAgAGMAbwBuAHMAdAAgAGMAbwBuAGQAaQB0AGkAbwBuADoAIABiAG8AbwBsAGUAYQBuADsACgBjAG8AbgBzAHQAIABDADIAIAA9ACAAKAApACAAPQA%2BACAAPABkAGkAdgA%2BAHsAYwBvAG4AZABpAHQAaQBvAG4AIAA%2FACAAPAA%2BAGYAbwBvADwALwA%2BACAAOgAgADwAPgBiAGEAcgA8AC8APgB9ADwALwBkAGkAdgA%2BADsACgA%3D
Expected result
Copying the examples from
react/jsx-no-useless-fragment
:And here are some other examples I found, where ESLint also doesn't error, but I think it maybe should?:
Code of Conduct
The text was updated successfully, but these errors were encountered: