Skip to content

Conversation

rattrayalex
Copy link
Collaborator

@rattrayalex rattrayalex commented Oct 31, 2020

Reverts a7580eb, which was intended to address #737

Fixes #5814

Alternative/complementary approaches were proposed in #9561, with #13183 emerging as the intended successor, as it addresses several of the shortcomings of this approach.

Headline examples, pulled from the tests:

const message =
  i % 3 === 0 && i % 5 === 0
    ? "fizzbuzz"
    : i % 3 === 0
      ? "fizz"
      : i % 5 === 0
        ? "buzz"
        : String(i);

const paymentMessage =
  state == "success"
    ? "Payment completed successfully"
    : state == "processing"
      ? "Payment processing"
      : state == "invalid_cvc"
        ? "There was an issue with your CVC number"
        : state == "invalid_expiry"
          ? "Expiry must be sometime in the past."
          : "There was an issue with the payment.  Please contact support.";

children && !isEmptyChildren(children)
  ? children
  : props.match
    ? component
      ? React.createElement(component, props)
      : render
        ? render(props)
        : null
    : null;

// Note that JSX stays the same, eg:
const StorybookLoader = ({ match }) =>
  match.params.storyId === "button" ? (
    <ButtonStorybook />
  ) : match.params.storyId === "color" ? (
    <ColorBook />
  ) : match.params.storyId === "typography" ? (
    <TypographyBook />
  ) : match.params.storyId === "loading" ? (
    <LoaderStorybook />
  ) : match.params.storyId === "deal-list" ? (
    <DealListStory />
  ) : (
    <Message>
      <Title>{"Missing story book"}</Title>
      <Content>
        <BackButton />
      </Content>
    </Message>
  );
  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@askuzminov
Copy link

askuzminov commented Nov 1, 2020

Original

const pickBgColor = (colorName: string) =>
  colorName === 'active'
    ? colorName === 'active'
      ? 'var(--blue2)'
      : colorName === 'inactive'
    : colorName === 'inactive'
    ? 'var(--blue1)'
    : colorName === 'success' || colorName === 'abc'
    ? colorName === 'abc'
      ? 'var(--blue2)'
      : 'var(--blue23)'
    : 'var(--red4)';

const message = (i: number) =>
  i % 3 === 0 && i % 5 === 0
    ? 'fizzbuzzfizzbuzzfizzbuzzfizzbuzz'
    : i % 3 === 0
    ? i % 3 === 0
      ? 'ffizzbuzzfizzbuzzfizzbuzzfizzbuzzizz'
      : i % 5 === 0
    : i % 5 === 0
    ? 'fizzbuzzfizzbuzzfizzbuzzfizzbuzz'
    : String(i);

Current RP

const pickBgColor = (colorName: string) =>
  colorName === "active"
    ? colorName === "active"
      ? "var(--blue2)"
      : colorName === "inactive"
    : colorName === "inactive"
      ? "var(--blue1)"
      : colorName === "success" || colorName === "abc"
        ? colorName === "abc"
          ? "var(--blue2)"
          : "var(--blue23)"
        : "var(--red4)";

const message = (i: number) =>
  i % 3 === 0 && i % 5 === 0
    ? "fizzbuzzfizzbuzzfizzbuzzfizzbuzz"
    : i % 3 === 0
      ? i % 3 === 0
        ? "ffizzbuzzfizzbuzzfizzbuzzfizzbuzzizz"
        : i % 5 === 0
      : i % 5 === 0
        ? "fizzbuzzfizzbuzzfizzbuzzfizzbuzz"
        : String(i);

This is a good change as the tree of conditions is visible. It is easier to navigate and refactor using it (for example, move too large branches into separate variables to simplify the logic).

@thorn0
Copy link
Member

thorn0 commented Nov 2, 2020

How about keeping nested ternaries flat (like now) only if all the nesting happens in the else-branches?

So it'd be either a tree or a fully flat chain, but never a semi-flat uh... thing like it happens now sometimes.

This was proposed earlier in #5258.

@troglotit
Copy link

@thorn0 as I said in original issue, it will create noise in falsy branch when you add or remove in truthy branch.

@thorn0
Copy link
Member

thorn0 commented Nov 2, 2020

@troglotit But how often is that going to happen? Not too often, isn't it? So this might be a good enough trade-off.

@troglotit
Copy link

@thorn0 yeah... but the idea of alternating styles based on an arbitrary "looks good" is what irks me here.

@rattrayalex
Copy link
Collaborator Author

I think it's best to keep this PR a straight up-and-down, one-line revert to previous behavior.

@thorn0
Copy link
Member

thorn0 commented Nov 2, 2020

It can't remain a one-liner. If only because turning TS conditional chains into pyramids is not an option. As you already know, the current formatting for TS conditionals gets its share of criticism, but those complaints have nothing to do with "bring the indentation back".

Base automatically changed from master to main January 23, 2021 17:13
@fisker fisker mentioned this pull request Jan 27, 2021
3 tasks
@fisker
Copy link
Member

fisker commented Jul 21, 2022

We are close to v3 release, @rattrayalex want push this forward? Personally, I'm 👍 for this change.

@rattrayalex
Copy link
Collaborator Author

Yeah I really do. I've just been blocked on writing an extensive PR description / blog post justifying the change, and any merge conflicts or corner case bugs that may be lurking.

I'd love help getting it over the line!

I might be able to devote some time to it this weekend...

@rattrayalex
Copy link
Collaborator Author

(thanks for the words of support btw)

@rattrayalex
Copy link
Collaborator Author

rattrayalex commented Jul 28, 2022

@fisker (and/or @thorn0 if you're around) would you mind taking a look at #13183 instead, which supersedes this?

@rattrayalex rattrayalex reopened this Aug 16, 2023
@rattrayalex rattrayalex force-pushed the ralex/nested-ternaries-1 branch 2 times, most recently from c6bc200 to 91168c6 Compare September 16, 2023 23:59
@rattrayalex rattrayalex force-pushed the ralex/nested-ternaries-1 branch 2 times, most recently from cb7ee37 to 120261c Compare September 17, 2023 00:32
@sosukesuzuki
Copy link
Member

Wait for @fisker review before merging

@ADTC
Copy link
Contributor

ADTC commented Sep 19, 2023

My support goes 100% to this, and 0% to #13183 😄

@rattrayalex
Copy link
Collaborator Author

gentle bump @fisker (this is blocked on you, and you'd previously given it a thumbs-up)

@rattrayalex rattrayalex merged commit f25a592 into prettier:main Sep 24, 2023
@u3u
Copy link
Contributor

u3u commented Sep 25, 2023

Unbelievable! It's been 3 years! It finally merged!

@ADTC
Copy link
Contributor

ADTC commented Sep 25, 2023

At least, we finally have this, until it gets superseded by #13183 (which will probably happen, whether we like it or not).

Regardless, thank you so much for the merge. This should have been there in the first place.

@sosukesuzuki sosukesuzuki added this to the 3.1 milestone Sep 25, 2023
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 16, 2024
* Add indentation back to nested ternaries (except JSX)

* Add a real-world test case

* Add changelog entry

---------

Co-authored-by: Alex Rattray <rattrayalex@stripe.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested ternary formatting - add indents back
9 participants