2 title: multiline-ternary
10 JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.
15 var foo = bar > baz ? value1 : value2;
18 The above can be rewritten as the following to improve readability and more clearly delineate the operands:
28 This rule enforces or disallows newlines between operands of a ternary expression.
29 Note: The location of the operators is not enforced by this rule. Please see the [operator-linebreak](operator-linebreak) rule if you are interested in enforcing the location of the operators themselves.
33 This rule has a string option:
35 * `"always"` (default) enforces newlines between the operands of a ternary expression.
36 * `"always-multiline"` enforces newlines between the operands of a ternary expression if the expression spans multiple lines.
37 * `"never"` disallows newlines between the operands of a ternary expression.
41 This is the default option.
43 Examples of **incorrect** code for this rule with the `"always"` option:
48 /*eslint multiline-ternary: ["error", "always"]*/
50 foo > bar ? value1 : value2;
61 Examples of **correct** code for this rule with the `"always"` option:
66 /*eslint multiline-ternary: ["error", "always"]*/
83 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
88 /*eslint multiline-ternary: ["error", "always-multiline"]*/
97 bar > baz ? value1 : value2;
102 Examples of **correct** code for this rule with the `"always-multiline"` option:
107 /*eslint multiline-ternary: ["error", "always-multiline"]*/
109 foo > bar ? value1 : value2;
116 (baz > qux ? value1 : value2) :
135 Examples of **incorrect** code for this rule with the `"never"` option:
140 /*eslint multiline-ternary: ["error", "never"]*/
156 Examples of **correct** code for this rule with the `"never"` option:
161 /*eslint multiline-ternary: ["error", "never"]*/
163 foo > bar ? value1 : value2;
165 foo > bar ? (baz > qux ? value1 : value2) : value3;
168 baz > qux ? value1 : value2
174 ## When Not To Use It
176 You can safely disable this rule if you do not have any strict conventions about whether the operands of a ternary expression should be separated by newlines.
180 * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary)