-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New Ternary Formatting: A Curious Case of the Ternaries #13183
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
The only part that I would prefer as in the current release, would be for simple ternaries to be:
|
I will review this PR this weekend. |
@sosukesuzuki how about this weekend? 😇 |
Does this one work?! |
Gotta say, after reading it more thoroughly, I'm not a huge fan of how this looks... I hear the OP in that he experienced that he has come to like this style more and more after using it for a while, and so that he hopes people would be willing to try it for a while before jumping to conclusions. The thing is, I'm not sure how I could try this for a while before casting my final vote let's say. Like, either this gets merged and then everybody can (/ will be forced to) "try" it, but I'm pretty sure after it's been merged, it's very unlikely to be reverted if people turn out to really dislike it. Or this PR doesn't get merged yet for a while, but then how am I supposed to be able to try it? @rattrayalex, am I supposed to point to your fork and branch in my EditTo be more specific about the things that, at least at first glance, I don't like:
I'm so used to seeing the The case-style is also new to me, but that one I can more easily imagine getting used to and liking after having some more experience with it.
This one I can live with. But that's just because I'm sensitive to line lengths and I prefer to see multiple lines that are similar in length than to see one outlier that is significantly longer or shorter than the lines around it.
Same example as the first one. Like, okay besides the fact that now my line-length-sensitivity is triggered... 😅 But okay, besides that, I mean really, if the consequent has to go to to the next line, then why not bring the const redirectUrl = pathName
? pathName
: nextPathName + nextSearch || defaultAuthParams.afterLoginUrl; I do understand what you say that you have come to read this So then I think it should be up to you and the (other) maintainers of this formatter to provide us with a trial period. Like this gets merged, but it's opt-in or opt-out via a newly added configuration option. And then a few months later we take a poll on whether the new style indeed has grown on people or not. What do you guys think about that? @rattrayalex @sosukesuzuki |
Thanks Evert. I really appreciate your willingness to try this out, even though it looks weird.
A full blog post is due to answer this question, but here's a quick attempt off the cuff, from what I can remember:
Does that help clarify anything at all? I'm keen for feedback – which of these arguments make the most sense / feel most compelling, and which the least? |
Oh, and as for this:
You can set |
return ( | ||
this.hasPlugin("dynamicImports") && | ||
this.lookahead().type === tt.parenLeft.right | ||
) ? |
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 like the parenthesis and structure here
function (v, colors) { | ||
return util.inspect(v, { colors: colors }); | ||
}; | ||
var inspect = 4 === util.inspect.length ? |
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.
Nice that the condition now fits in one line
: <GreenColorThing /> | ||
} | ||
/> | ||
); |
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.
Would be great to ship all those examples in master (regardless of this PR) and rebase so we can see the before/after your changes
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.
Would it be possible to split this into two PRs? One from the previous discussion reverting to old behavior, and another to consider introducing your additional concerns? |
Great to see people are actually doing something about this. I stopped using prettier precisely because of this issue, so I really hope this PR solves it for good. |
I was thinking about this because my gut reaction was "well I don't need the question mark to see there's more going on" and I realized it's probably because I rely on the presence of a semicolon to know that a line is finished. If I configured Prettier to format without semicolons and rely on ASI then the ? would be pretty helpful, but currently (and I guess this is the default configuration) my eyes instantly move to the next line to keep reading. |
666f0be
to
bf278e3
Compare
615d1c1
to
85ebd7b
Compare
e080d79
to
a23d196
Compare
Now that #9559, this PR is blocked by:
@sosukesuzuki and @fisker, please let me know if there's anything I can do to help you in review! I've tried to structure commits such that you can see the diff between this and main in both the |
efbb3a2
to
dda498d
Compare
2c728b8
to
ec946a5
Compare
ec946a5
to
b84b6cf
Compare
website/blog/2023-10-01-3.1.0.md
Outdated
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.
The release blog should be created at ./scripts/draft-blog-post.js
. Also, other features and fixes should be mentioned.
Therefore, I would like it to be removed from this PR.
Based on the content of this post, I'll be opening a PR to create a new release blog, and I'll be asking for your reviews at that time as well.
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.
Sure, will remove shortly! Any feedback on the PR itself (is all good there?) or the other blog post?
I still prefer keep the old |
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 my review comments are solved, LGTM.
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.
Can I separate this blog addition into a separate PR? I don't want to merge features and post blogs at the same time. I think it's best to post blog at the same time as the 3.1 release.
@rattrayalex I've removed two blog posts from this PR. I'll re-create PRs for each blog posts. |
Terrific, thank you so much @sosukesuzuki ! I'll resume work on the blog post in a separate PR. |
Summary
Adds an
--experimental-ternaries
option, which moves the?
in multiline ternaries to the end of the first line instead of the start of the second, along with several related changes.While it might look weird at first, beta-testing shows that after a little use, developers find it makes nested ternaries much more readable and useful.
This PR resolves one of our most highly-upvoted issues in a way that fixes the problems the naive solution reintroduced, following extensive discussion, exploration, and experimentation.
Example
For more examples, see the tests:
Walkthrough
foo ?
it's like asking a question about foo – "if foo? then, …".:
is an "else".: foo
that means, "else, foo".: foo ?
that means "else, if foo?".foo
, that means, "then foo".Reception
Typically, developers say they dislike this at first glance. Fortunately, beta testing shows that after using it for a little while, developers find it much more readable than other formatting options, and don't want to ever go back.
For example, here's someone commenting on a PR adding this style to their codebase at work:
And here they are a few weeks later:
Someone else from the same team, who was also quite skeptical at first, had this to say:
Like JSX and Prettier itself, which both had many detractors at first, we expect most skeptics to become advocates after using this for a while.
Context and Motivation
Printing nested ternaries nicely in a wide variety of scenarios is a tricky challenge.
Prettier’s original, naive approach of indentation worked fine in simple cases, but didn’t scale and had other problems.
In 2018, we replaced that with flat ternaries, which the community did not receive well – the issue asking it to be reverted has over 500 upvotes.
Over the last few years, we’ve worked hard to find a solution which would satisfy our constraints in a wide variety of situations.
Ideally, we'd find one scheme that would fluidly flow from a single ternary, to a chain of 2, to a long chain of simple cases, to something more complex with a few nested conditions. The syntax in JSX, TypeScript conditional expressions (which cannot be expressed with
if
), and normal JS should all look and feel the same. And in all cases, it should be easy to follow what's the "if", what's the "then", and what's the "else" – and what they map to.Details and Notes
Note that it mostly removes parens from ternaries in JSX (there is almost no special-casing for JSX needed anymore), eg:
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨