2 title: multiline-ternary
11 JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.
16 var foo = bar > baz ? value1 : value2;
19 The above can be rewritten as the following to improve readability and more clearly delineate the operands:
29 This rule enforces or disallows newlines between operands of a ternary expression.
30 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.
34 This rule has a string option:
36 * `"always"` (default) enforces newlines between the operands of a ternary expression.
37 * `"always-multiline"` enforces newlines between the operands of a ternary expression if the expression spans multiple lines.
38 * `"never"` disallows newlines between the operands of a ternary expression.
42 This is the default option.
44 Examples of **incorrect** code for this rule with the `"always"` option:
49 /*eslint multiline-ternary: ["error", "always"]*/
51 foo > bar ? value1 : value2;
62 Examples of **correct** code for this rule with the `"always"` option:
67 /*eslint multiline-ternary: ["error", "always"]*/
84 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
89 /*eslint multiline-ternary: ["error", "always-multiline"]*/
98 bar > baz ? value1 : value2;
103 Examples of **correct** code for this rule with the `"always-multiline"` option:
108 /*eslint multiline-ternary: ["error", "always-multiline"]*/
110 foo > bar ? value1 : value2;
117 (baz > qux ? value1 : value2) :
136 Examples of **incorrect** code for this rule with the `"never"` option:
141 /*eslint multiline-ternary: ["error", "never"]*/
157 Examples of **correct** code for this rule with the `"never"` option:
162 /*eslint multiline-ternary: ["error", "never"]*/
164 foo > bar ? value1 : value2;
166 foo > bar ? (baz > qux ? value1 : value2) : value3;
169 baz > qux ? value1 : value2
175 ## When Not To Use It
177 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.
181 * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary)