Skip to content

Conversation

zeorin
Copy link

@zeorin zeorin commented Jun 28, 2018

See #737 for the issue that sparked the discussion.

Primarily, it changes ternaries that were of the form (nesting in alternate sub-expressions):

aaaaaaaaaaaaaaa
  ? bbbbbbbbbbbbbbbbbb
  : ccccccccccccccc
    ? ddddddddddddddd
    : eeeeeeeeeeeeeee
      ? fffffffffffffff
      : gggggggggggggggg;

into:

aaaaaaaaaaaaaaa ?
  bbbbbbbbbbbbbbbbbb :
ccccccccccccccc ?
  ddddddddddddddd :
eeeeeeeeeeeeeee ?
  fffffffffffffff :
gggggggggggggggg;

as suggested in #737 (comment).

It also changes ternaries that nested consequent sub-expressions slightly to be more consistent with the new alternate form.

See the changes to tests/ternaries/__snapshots__/jsfmt.spec.js.snap for a great overview of the new format.

JSX-mode ternaries were not changed.

@hawkrives
Copy link
Contributor

This also changes single-step ternaries, i.e.

mySuperDuperExtraLongTernary
  ? "yes"
  : "no"

becomes

mySuperDuperExtraLongTernary ?
  "yes" :
  "no"

and … I really really like the current placement of ? / :.

I'm OK with this proposal for multi-step ternaries, but then that'd be inconsistent.

My 2¢.

@zeorin
Copy link
Author

zeorin commented Jun 28, 2018

@hawkrives that was intentionally done, since the convention for other operators is to keep the operator at the end of the line instead of the beginning when breaking. Ternaries were the odd one out here. I assumed that consistency would be desired.

It'd be easy enough to account for non-nested ternaries and keep the old behaviour of placing the operators at the beginning of the line, I don't mind doing it, but how do I know what the Prettier maintainers decide?

@jaufgang
Copy link

Someone should create a poll for the preferred format. I think having the operators at the beginning of the line is much more readable, but there were a handful of competing formatting suggestions in #737.

@bakkot
Copy link
Collaborator

bakkot commented Jun 28, 2018

Personally not a fan of this style (in large part because it seems very much out of keeping with how most people write JS), especially for non-nested ternaries, but that aside, one thing:

aaaaaaaaaaaaaaa ?
  bbbbbbbbbbbbbbbbbb :
ccccccccccccccc ?
  ddddddddddddddd :
eeeeeeeeeeeeeee ?
  fffffffffffffff :
gggggggggggggggg;

why is the gggggggggggggggg not indented? That seems really odd, to my eye, since gggggggggggggggg is a value like b / d / f and not a condition like a / c / e.

@zeorin
Copy link
Author

zeorin commented Jun 28, 2018

It is not indented because it is the form of the proposal suggested in #737 (comment). There was no clear direction or expressed preference for a specific proposal in that thread from Prettier maintainers, so I implemented the one that I felt had the most merit.

As to why I personally like the final value to be dedented, it's because it's like a default case in a switch statement. It's the value of the entire expression when none of the tests match. Not dedenting it makes it seem like it's related only to the last test when it's related to all the tests.

Having said that, I'm not dead-set on dedenting the ultimate alternate expression (nor the trailing operators), primarily I want to get rid of the stepped nesting for alternate expression nesting.

@iclanzan
Copy link
Contributor

I've never seen ternaries formatted like this before. At first glance I was disturbed but then I realized it makes sense, it's consistent and actually very readable. I think I like it!
Splendid work @zeorin!

@suchipi
Copy link
Member

suchipi commented Jun 29, 2018

I think this style is too significant of a change. If nothing else, we shouldn't change the way non-nested ternaries format.

See #737 for the issue that
sparked the discussion.

Primarily, it changes ternaries that were of the form (nesting in
alternate sub-expressions):
```js
aaaaaaaaaaaaaaa
  ? bbbbbbbbbbbbbbbbbb
  : ccccccccccccccc
    ? ddddddddddddddd
    : eeeeeeeeeeeeeee
      ? fffffffffffffff
      : gggggggggggggggg;
```
into:
```js
aaaaaaaaaaaaaaa ?
  bbbbbbbbbbbbbbbbbb :
ccccccccccccccc ?
  ddddddddddddddd :
eeeeeeeeeeeeeee ?
  fffffffffffffff :
gggggggggggggggg;
```
as suggested in #737 (comment).

It also changes ternaries that nested consequent sub-expressions
slightly to be more consistent with the new alternate form.

JSX-mode ternaries were not changed.
@zeorin
Copy link
Author

zeorin commented Jul 2, 2018

I do think this style has the most merit, in addition to the points raised by @0x24a537r9 in #737 (comment):

  • operators at the end are easier to work with when maintaining the code, needing less nit-picky moving around of operators (which, admittedly, use of Prettier-on-save can obviate), and thus also leading to less noise in VCS diffs (much like trailing commas in multiline object & array literals).

Myself, I wasn't a fan of this proposal at first, either, but over time it grew on me, on the strengths of its merits. (Incidentally, I went through the same process when I first encountered trailing commas.)

I personally think that changing non-nested ternaries to be consistent with nested ternaries is more important than some one-time mental friction of getting used to the new style (especially as it would be consistent with operators in general, not just the nested ternaries).

Having said that, I mostly care about removing the "infinite stairs" for alterate sub-expression ternary nesting and the other details of the style don't matter nearly as much.

I'm happy to make changes to whatever style is settled on, but at this point this thread is suffering from the same bike-shedding as #737 was. I was hoping that shipping some actual code of an implementation would act as a sort of Occam's Razor and we could move on.

This means that I'm not sure what to change, as whatever I change it to, someone will have issue with. Who knew that ternary style preferences are so diverse?

Anyway, I think we need to make a decision on what style we go with, and to that end, @vjeux, please will you make a call?

@lydell
Copy link
Member

lydell commented Jul 2, 2018

I think we should experiment with a style that causes as little change as possible to maximize the chance of moving forward. So don’t move the ? and :. Just flatten the indentation (when it makes sense). Optimize for the common case.

type TypeName<T> = T extends string
  ? "string"
  : T extends number
  ? "number"
  : T extends boolean
  ? "boolean"
  : T extends undefined
  ? "undefined"
  : T extends Function ? "function" : "object"

@suchipi suchipi self-assigned this Aug 18, 2018
@suchipi
Copy link
Member

suchipi commented Aug 18, 2018

I'm gonna implement @lydell's proposal this weekend and make a PR.

Edit: Well, I obviously didn't get to that last weekend. I'll do it this weekend, instead

@suchipi suchipi removed their assignment Aug 23, 2018
@suchipi
Copy link
Member

suchipi commented Aug 23, 2018

Removing my assignment because I intended to assign myself to the issue, not the PR.

@thgreasi
Copy link

@zeorin many 👍s on this.
How will this work for cases like #4199 #4874 #4979 ?

@suchipi
Copy link
Member

suchipi commented Aug 30, 2018

@thgreasi I don't intend to change that behavior as part of this. That is intended behavior; please comment on one of those issues if you have input.

@suchipi suchipi mentioned this pull request Aug 31, 2018
3 tasks
@lydell lydell mentioned this pull request Oct 13, 2018
@ikatyang
Copy link
Member

ikatyang commented Nov 3, 2018

Fixed in #5039.

@ikatyang ikatyang closed this Nov 3, 2018
@zeorin zeorin deleted the feature/nested-ternary-print branch November 6, 2018 13:10
@zeorin
Copy link
Author

zeorin commented Nov 6, 2018

Whilst I'm glad that this issue is now fixed (:tada:), It's regrettable that there was no direction from the maintainers on this PR.

There was a lot of discussion, sure, by many people, but not one maintainer saying something like: "Please tweak foo, bar, and baz, and then it's good to merge".

The barrier to entry on making this PR was pretty high. I read the original paper to get a sense of how Prettier works, and spent a whole weekend coming up with the first draft of the PR, tweaking it a bunch over the following days.

That effort was wasted.

I urge the Prettier maintainers to be more forthcoming and transparent.

@vjeux
Copy link
Contributor

vjeux commented Nov 6, 2018

@zeorin I'm sorry that you didn't have the best experience here.

This particular issue has been long standing and is likely one of the oldest issues still opened. The reason is that we needed to decide on the way we wanted to print this piece of syntax. Your PR has actually been critical to get the discussion started again and it finally drove to a final decision.

About the actual implementation, it's a bit unfortunate that we created a new PR rather than asking you to update yours. There are two reasons for this:

  • The eventual design was different than the one you suggested and it wasn't immediately obvious that code could be shared.
  • The actual implementation has never been an issue in this case. This is a very simple change for anyone that has already contributed to prettier. So it only took something like 5-10min to spin up the PR and it would have likely taken an order magnitude more time to teach you how to morph your PR into the final one.

Thanks for raising this issue and I hope that this gives you some insight around why things happened this way. But please remember that your PR was not useless, this long standing bug has been closed now because of you.

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Feb 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants