conditional-expression.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // There are two ways to print ConditionalExpressions: "normal mode" and
  2. // "JSX mode". This is normal mode (when breaking):
  3. //
  4. // test
  5. // ? consequent
  6. // : alternate;
  7. //
  8. // And this is JSX mode (when breaking):
  9. //
  10. // test ? (
  11. // consequent
  12. // ) : (
  13. // alternate
  14. // );
  15. //
  16. // When non-breaking, they look the same:
  17. //
  18. // test ? consequent : alternate;
  19. //
  20. // We only print a conditional expression in JSX mode if its test,
  21. // consequent, or alternate are JSXElements.
  22. // Otherwise, we print in normal mode.
  23. // This ConditionalExpression has no JSXElements so it prints in normal mode.
  24. // The line does not break.
  25. normalModeNonBreaking ? "a" : "b";
  26. // This ConditionalExpression has no JSXElements so it prints in normal mode.
  27. // Its consequent is very long, so it breaks out to multiple lines.
  28. normalModeBreaking
  29. ? johnJacobJingleHeimerSchmidtHisNameIsMyNameTooWheneverWeGoOutThePeopleAlwaysShoutThereGoesJohnJacobJingleHeimerSchmidtYaDaDaDaDaDaDa
  30. : "c";
  31. // This ConditionalExpression prints in JSX mode because its test is a
  32. // JSXElement. It is non-breaking.
  33. // Note: I have never, ever seen someone use a JSXElement as the test in a
  34. // ConditionalExpression. But this test is included for completeness.
  35. <div /> ? jsxModeFromElementNonBreaking : "a";
  36. // This ConditionalExpression prints in JSX mode because its consequent is a
  37. // JSXElement. It is non-breaking.
  38. jsxModeFromElementNonBreaking ? <div /> : "a";
  39. // This ConditionalExpression prints in JSX mode because its alternate is a
  40. // JSXElement. It is non-breaking.
  41. jsxModeFromElementNonBreaking ? "a" : <div />;
  42. // This ConditionalExpression prints in JSX mode because its test is a
  43. // JSXElement. It is breaking.
  44. // Note: I have never, ever seen someone use a JSXElement as the test in a
  45. // ConditionalExpression. But this test is included for completeness.
  46. <div>
  47. <span>thisIsASongAboutYourPoorSickPenguinHeHasAFeverAndHisToesAreBlueButIfISingToYourPoorSickPenguinHeWillFeelBetterInADayOrTwo</span>
  48. </div> ? (
  49. "jsx mode from element breaking"
  50. ) : (
  51. "a"
  52. );
  53. // This ConditionalExpression prints in JSX mode because its consequent is a
  54. // JSXElement. It is breaking.
  55. jsxModeFromElementBreaking ? (
  56. <div>
  57. <span>thisIsASongAboutYourPoorSickPenguinHeHasAFeverAndHisToesAreBlueButIfISingToYourPoorSickPenguinHeWillFeelBetterInADayOrTwo</span>
  58. </div>
  59. ) : (
  60. "a"
  61. );
  62. // This ConditionalExpression prints in JSX mode because its alternate is a
  63. // JSXElement. It is breaking.
  64. jsxModeFromElementBreaking ? (
  65. "a"
  66. ) : (
  67. <div>
  68. <span>thisIsASongAboutYourPoorSickPenguinHeHasAFeverAndHisToesAreBlueButIfISingToYourPoorSickPenguinHeWillFeelBetterInADayOrTwo</span>
  69. </div>
  70. );
  71. // This chain of ConditionalExpressions prints in JSX mode because the parent of
  72. // the outermost ConditionalExpression is a JSXExpressionContainer. It is
  73. // non-breaking.
  74. <div>
  75. {a ? "a" : b ? "b" : "c"}
  76. </div>;
  77. // This chain of ConditionalExpressions prints in JSX mode because there is a
  78. // JSX element somewhere in the chain. It is non-breaking.
  79. cable ? "satellite" : public ? "affairs" : network ? <span id="c" /> : "dunno";
  80. // This chain of ConditionalExpressions prints in JSX mode because there is a
  81. // JSX element somewhere in the chain (in this case, at the end). It is
  82. // breaking; notice the consequents and alternates in the entire chain get
  83. // wrapped in parens.
  84. cable ? (
  85. "satellite"
  86. ) : public ? (
  87. "affairs"
  88. ) : network ? (
  89. <div>
  90. <span>thisIsASongAboutYourPoorSickPenguinHeHasAFeverAndHisToesAreBlueButIfISingToYourPoorSickPenguinHeWillFeelBetterInADayOrTwo</span>
  91. </div>
  92. ) : "dunno";
  93. // This chain of ConditionalExpressions prints in JSX mode because there is a
  94. // JSX element somewhere in the chain (in this case, at the beginning). It is
  95. // breaking; notice the consequents and alternates in the entire chain get
  96. // wrapped in parens.
  97. cable ? (
  98. <div>
  99. <span>thisIsASongAboutYourPoorSickPenguinHeHasAFeverAndHisToesAreBlueButIfISingToYourPoorSickPenguinHeWillFeelBetterInADayOrTwo</span>
  100. </div>
  101. ) : sateline ? (
  102. "public"
  103. ) : affairs ? (
  104. "network"
  105. ) : "dunno";
  106. // This chain of ConditionalExpressions prints in JSX mode because there is a
  107. // JSX element somewhere in the chain. It is breaking; notice the consequents
  108. // and alternates in the entire chain get wrapped in parens.
  109. <div>
  110. {properties.length > 1 ||
  111. (properties.length === 1 && properties[0].apps.size > 1) ? (
  112. draggingApp == null || newPropertyName == null ? (
  113. <MigrationPropertyListItem />
  114. ) : (
  115. <MigrationPropertyListItem apps={Immutable.List()} />
  116. )
  117. ) : null}
  118. </div>;