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
General JSX Improvements #73
Comments
The code would preferably just be The above is not exactly intentional, but just how the different rules play out. Block-less arrow expressions want to be on the same line as the params. Usually you are doing things like At a certain point, a formatter like this is going to influence how you write code. I think the above code should be a normal function definition. But we could potentially see about specially formatting JSX that is returned from block-less arrow functions. |
Dig. And point taken wrt to the arrow function not being necessary. I'm still a bit curious why this works though: function StatelessComponent({ children }) {
return (
<div className='whatever'>
<span>{children}</span>
</div>
);
}
// prettier output is the same In this case, the parens are preserved. This is a specific parser rule wrt to block-less arrows, then? Personally, I know I use arrows in a lot of places I know I don't need to, purely for the succinctness of the implicit return. I certainly don't want to start a code-style war here, this just seems like an unexpected result at first. 🤷♂️ |
This also seems a bit unfortunate to me: const els = items.map(item => (
<div className='whatever'>
<span>{children}</span>
</div>
)); becomes const els = items.map(
item => <div className="whatever">
<span>{children}</span>
</div>
); I would normally expect multiline JSX start and end tags to match up instead: const els = items.map(item =>
<div className="whatever">
<span>{children}</span>
</div>
); This is a very common pattern. |
Is there ever a case where JSX tags don't fit one of these cases?
Off-hand I can't think of any 🤔 Might be a good heuristic |
Let's use this issue to track all JSX changes. |
Need to make each attribute on its own line when they overflow <div a="1" b="2" c="3" d="4" e="5" f="6" g="7" h="8"/> should be <div
a="1"
b="2"
c="3"
d="4"
e="5"
f="6"
g="7"
h="8"
/> |
single quote settings should not affect jsx attributes. I've never seen anyone write <div id="a" /> should remain <div id="a" /> even with singleQuote option enabled |
JSX quotes must be html escaped and not string escaped. <div id="'" /> is currently converted to invalid JSX <div id='\'' /> it should be <div id="'" /> instead |
@vramana "@jlongster Let's use this issue to track all JSX changes." I'm following orders :p |
Okay. I'll close other in favour of this issue. |
@vramana typically I'd say yes, but I think a lot of these can be done in a single pass to improve JSX. I worked on it some but knew that it needed another pass. Having a lot of issues causes stress, for me at least. I like to group things when we can. Comments and JSX are two things that generally need another pass to make it solid, and after that we can file individual issues for them. Usually when features are not quite ready like these many of the issues are actually inter-related and fixing one fixes another, etc, which is why I think it makes sense to group them. |
Copying issue #30 here Non-Optional Spaces Removed From JSX: Input: <span>
foo <span>bar</span>
</span> Actual output <span>
foo<span>bar</span>
</span>; |
I suggest to turn the OP post into a checklist, like how we do in React: facebook/react#7925. |
I just submitted #123 which addresses one of these issues. Happy to attempt some of the others later today. |
Hi there, I notice Prettier has trouble formatting a component name with a dot. in routes.js
The offending JSX in Nav.js is something like this:
It doesn't like
Apologies if this should have been a separate issue. |
@hoolymama I think this issue is already fixed on master. Which version of prettier are you using? |
@vramana Ah okay, thank you |
@vramana - Yes I updated and its fixed. Cheers! |
For what it's worth... I'm not a fan of |
I've definitely seen this, usually in any react codebase where eslint was set to single quotes. |
I've mostly fixed the stateless component jsx case, just ironing out a few edge cases. |
Hmm, the challenges in #53 are interesting to me, I'm not sure how to fix them. this:
compiles to
that is, the last line is aligned to the opening Similarly, ternaries align just as they would outside of JSX, but then the I'm not aware of a construct in Separately, ternaries should probably be prettier, but I think that's a largely unrelated problem. |
There are few things that I have noticed with attributes : http://bit.ly/2jel07w Firstly I think that there should not be an extra indentation for array in this case : Instead of : <View style={
[
...
]
} /> I would expect : <View style={[
...
]} /> Secondly when an attribute content force going to next line, I think that in general most people will want the attribute itself to be on next line. so : Instead of : <View style={
[
...
]
} /> I would expect : <View
style={[
...
]}
/> Finally when an attribute force new line inside of a tag, I would expect all attributes to be on his own line : Instead of : <View onClick={onClick} style={
[
...
]
} /> I would expect : <View
onClick={onClick}
style={[
...
]}
/> I think that most of the JSX users expect the same kinds of results, but it have to be confirmed. |
@knowbody This has been mentioned in several other issues. |
@fdecampredon explained what I've been doing perfectly |
Now that #123 has landed I wonder how much of this is fixed. I'd like to go through here and check all of them, and at this point probably split off into different issues. I don't have time right now, but if anyone wants to go through this issue and make a short, succinct list of issues that would be great. It would make it easier to check them off. |
I just updated the original comment with a list of things that have been fixed and remaining. So much progress was made! |
Not gonna check any off yet because I'm not sure what's been fixed, but here's a first pass at a list for everything that has been reported: Edit: @vjeux beat me to it 😅
|
@suchipi race condition! |
@vjeux can you add this one? When Eg, if prettier split this up... <div>
hello <span>world</span> <strong>foo</strong>
</div> into this... <div>
hello
<span>world</span>
<strong>foo</strong>
</div> (which it currently wouldn't, because those lines are short – but pretend they're long 😉)
to
generating "helloworldfoo" instead of "hello world foo" to the user. Oops! That's not just a little "oops" – if someone is trying to adopt (the good news? I have a PR mostly-written to fix this) |
oh, @vjeux may also want to add this: if <div>
<div>{pretend} {"there's"} {a lot of children here}</div><span>foo</span>
</div> into this... <div>
<div>
{pretend}
{"there's"}
{a lot of children here}
</div><span>foo</span>
</div> ...we should add a line after the I may also have this partially solved, especially thanks to a recent helpful tip from @jlongster |
I think at this point we should close this issue and open up new individual ones for the few remaining issues. What do you think? |
Agreed |
Amen, I'll do that with the above. |
Sorry, (@vjeux) this is such a silly thing, I hesitate even asking, but I'd like to understand why this is becoming a rule. It seems like it only introduces complexity to the formatter and potential confusion to the user. If I say I want single quotes, why are JSX attributes special? It just seems like a mistake to me. 🤷♂️ If there's legitimate reason why JSX attributes should always use double quotes, I'd love to know. 😄 |
Yep, feel free to close this an open all new issues (sorry, it's Friday night and not going to spend a whole lot of time on this right now :)) |
I just opened #195, #196 and #197. @rattrayalex, please create issues about the problems you mentioned :) |
@bspaulding Totally fair question. From what I've seen, the vast majority of HTML written is using double quotes. The Chrome devtools displays double quotes even though you authored them as single quotes. JSX is a bit weird because it is HTML inside of JavaScript. At Facebook, we've been following this convention of using double quotes for JSX (and XHP) in order to look closer to typically written HTML. All the official React docs are using single quotes for JavaScript strings and double quotes for JSX attribute strings: https://facebook.github.io/react/docs/introducing-jsx.html#jsx-represents-objects |
I agree double quotes in JSX is a weird rule. The most compelling reason for me to use it is because the community has already converged on it with the Airbnb rule set. So at this point there is no sense forking. |
This may have been captured elsewhere. But when you have a ternary and the true case is a function with an object passed to it and the false is jsx, the jsx can exceed the line length. (not saying this is good code) |
@jimmyhmiller, do you mind creating another issue for it, this way we don't need to spam the 20 people in this conversation ;) |
Remaining
Right now turns to
{}
should be
and
should be
Fixed
should turn into
--single-quote
.
correctlyshould be
(Note: the original issue has been replaced with this list of remaining items, so comments below might not make sense)
The text was updated successfully, but these errors were encountered: