-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
JSX {" "} spaces #4223
Comments
An alternative might be to use the HTML entity representation of the breaking space, <p>
hello 
<a href="#" target="_blank">
LINK
</a> 
world
</p>; |
@timothykang That’s not better, is it? |
@lydell The length is the same, but I disagree that the entity "could be anything". While the ASCII code might be a bit cryptic, the entity canonically represents the normal space as a single unit. That being said, I'm just pointing out an alternative. Do as you see fit. 😀 |
Yeah, it's not so much that it actually "could be anything", but that the developer might think it "could be anything" before researching it. |
How about a boolean value in the config to allow||disallow formatting a string if it means adding spaces in this manner? Something like:
Yes, the wording might need some changes - I'm no good at naming things. |
@Kosai106 Just to be clear, Prettier has to add those spaces when it breaks over multiple lines. With HTML, whitespace between two tags is collapsed to a single space - <span>
a
<strong>tag</strong>
</span will produce "a tag". With jsx, though, trailing and leading whitespace on a line is removed: <span>
a
<strong>tag</strong>
</span becomes "atag". You have to add The only way to avoid the explicit space is to not have leading/trailing whitespace, and instead keep all of the whitespace that needs to be kept between the tags on any given line: <span>
a <strong>tag</strong>
</span That will produce "a tag", just like the HTML one. I think everything I wrote here is correct. Anyway, I just wanted to clarify that the ugly spaces are actually semantically important to JSX, unlike HTML, so Prettier really can't add an option to not print them. |
@hawkrives What you wrote is correct, yes. But an option to ensure it doesn't break the code onto multiple lines if there's a need to add those types of spaces, would be ideal I believe - Otherwise Prettier is useless in JSX for me and the people I work with. |
@Kosai106 Prettier's output here is the right thing. JSX's space handling is different. It is important to draw out attention to this fact in your code so that subsequent developers do not glance over this subtle difference. P.S. And yes, I also had the same WTF reaction the first time I saw prettier doing this. 😄 |
If it did fit in one line, then I can see how we would want to keep it in one line. I guess that if we don’t today is because it’s not trivial to do so. When we break, I’m happy with the current way it is printed. I don’t think that we should change it now as it would be a pretty invasive change for little rewards |
Don't forget Facebook's own JSX space rules. See #1671 and #1581 (comment) |
When I first worked on the JSX formatting I did have it always put {“ “} on a line by itself, but decided to put it at the end of the previous line after some discussion: My gut feeling on this is that either way we print this (end of line or line by itself) there will be some pros and cons. So I’d only suggest changing this if we can show that the pros of having it on a line by itself heavily outway any cons. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
How about if we change prettier to break only when it doesn't need to introduce Isn't this easier to read? <p>
hello <a href="#" target="_blank">
LINK
</a> world
</p>; |
@Pyrolistical That's pretty much what I'm suggesting - At the very least as a setting, although at this point I've just stopped using Prettier for my React code. |
I'm in the "no settings" camp, which is why I'm trying to just change this rule for everybody. I really don't understand how more people would prefer their code littered with |
I personally do prefer my code formatted with Because it was a simple change to make I've created a PR that shows how Prettier would behave if we never introduced If anyone wants to add this as an option to |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@panlina do you have a specific example where you add // prettier-ignore related to this issue that you’d be willing to share? |
We're doing i18n using our own babel plugin and we are meeting the same situation of i18next/react-i18next#575. Currently we've managed to solve this by adjusting our matching algorithm to treat But I still think it valuable to provide an option to disable this formatting. |
Is there any solution to remove useless |
Hi All! Any update on this? Would love the option to disable |
Looking forward a way to disable this as well. 👀 |
If this isn't getting addressed, I'd like to know if there are any other formatters out there that aren't religiously opinionated about silly things like this? |
helloo, how can i disable this within one file only? |
This is NOT the solution, this should be able to be disabled project wide. |
Has anyone found an answer on how to solve this problem? |
Then don't use Prettier. |
prettier staff is so annoying. This formating breaks my tests, what's the problem to add a setting to disable it... |
So annoying, I was getting a mf error because of this for like 30 min, jesus stop adding useless characters. STOP ADDING CHARACTERS JUST FORMAT THE TEXT, ADDING CHARACTERS ISN'T FORMATTING |
Hi everyone! Someone found a solution? I found that if you delete the {' '} and the breakline after it and reformat again, the {' '} doesn't appears again in that reformat, at least in webstorm. |
If I have something strangely formatted like this, <React.StrictMode>
<Router> <Frame control={control} />
</Router>
</React.StrictMode> I think it should instead be formatted like this, <React.StrictMode>
<Router>
<Frame control={control} />
</Router>
</React.StrictMode> Than this, <React.StrictMode>
<Router>
{" "}
<Frame control={control} />
</Router>
</React.StrictMode>; If this is not possible with prettier, is there a formatter that allows this? |
Hi currently I'm facing a problem with prettier deleting this space inside a jsx element but i actually need this space. Can anyone tell me how to get this space back?! |
You could replace the empty space with |
@dhiegomaga is completly right, Prettier should not add any characters but should just format. I think there should really be an option to Disable adding characters like this since i only want to format something and not add Characters while doing so. |
While this has been closed I'll add a comment in case it's ever resurrected... We use JSX for a documentation site, meaning we have prose in JSX. Maintaining prose quickly becomes annoying when Prettier stuffs it full of We are left having to ignore that whole section of the repo, or inject ignore comments in a large number of files (and then try to ensure everyone remembers to preserve those comments etc etc). A setting similar to the markdown prose settings might make sense here instead; or even just a way to configure "preserve" for line length. I realise Prettier doesn't want to add lots of options, but in this case it's altering the code in a really unhelpful/unproductive way; and being able to control that would make Prettier more useful to us. |
Thats beacause file .js is not compatible with HTML inside it. Changing your file extension to .jsx will FIX IT |
This is crazy |
This line
But the same code (below) with trailing space works fine. Trailing space just removed after formatting.
What's the difference between those two examples? Why the fist works "incorrect"? |
Is there any way at all to stop Prettier adding these horribly ugly spaces?
Perhaps just not format strings like this if it needs to add spaces like this.
Prettier 1.11.1
Playground link
Input:
Output:
The text was updated successfully, but these errors were encountered: