]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/multiline-ternary.md
0c38bf7d3f16f714d7460a8bbb6f3affc78bee21
[pve-eslint.git] / eslint / docs / rules / multiline-ternary.md
1 # Enforce or disallow newlines between operands of ternary expressions (multiline-ternary)
2
3 JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.
4
5 For example:
6
7 ```js
8 var foo = bar > baz ? value1 : value2;
9 ```
10
11 The above can be rewritten as the following to improve readability and more clearly delineate the operands:
12
13 ```js
14 var foo = bar > baz ?
15 value1 :
16 value2;
17 ```
18
19 ## Rule Details
20
21 This rule enforces or disallows newlines between operands of a ternary expression.
22 Note: The location of the operators is not enforced by this rule. Please see the [operator-linebreak](operator-linebreak.md) rule if you are interested in enforcing the location of the operators themselves.
23
24 ## Options
25
26 This rule has a string option:
27
28 * `"always"` (default) enforces newlines between the operands of a ternary expression.
29 * `"always-multiline"` enforces newlines between the operands of a ternary expression if the expression spans multiple lines.
30 * `"never"` disallows newlines between the operands of a ternary expression.
31
32 ### always
33
34 This is the default option.
35
36 Examples of **incorrect** code for this rule with the `"always"` option:
37
38 ```js
39 /*eslint multiline-ternary: ["error", "always"]*/
40
41 foo > bar ? value1 : value2;
42
43 foo > bar ? value :
44 value2;
45
46 foo > bar ?
47 value : value2;
48 ```
49
50 Examples of **correct** code for this rule with the `"always"` option:
51
52 ```js
53 /*eslint multiline-ternary: ["error", "always"]*/
54
55 foo > bar ?
56 value1 :
57 value2;
58
59 foo > bar ?
60 (baz > qux ?
61 value1 :
62 value2) :
63 value3;
64 ```
65
66 ### always-multiline
67
68 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
69
70 ```js
71 /*eslint multiline-ternary: ["error", "always-multiline"]*/
72
73 foo > bar ? value1 :
74 value2;
75
76 foo > bar ?
77 value1 : value2;
78
79 foo > bar &&
80 bar > baz ? value1 : value2;
81 ```
82
83 Examples of **correct** code for this rule with the `"always-multiline"` option:
84
85 ```js
86 /*eslint multiline-ternary: ["error", "always-multiline"]*/
87
88 foo > bar ? value1 : value2;
89
90 foo > bar ?
91 value1 :
92 value2;
93
94 foo > bar ?
95 (baz > qux ? value1 : value2) :
96 value3;
97
98 foo > bar ?
99 (baz > qux ?
100 value1 :
101 value2) :
102 value3;
103
104 foo > bar &&
105 bar > baz ?
106 value1 :
107 value2;
108 ```
109
110 ### never
111
112 Examples of **incorrect** code for this rule with the `"never"` option:
113
114 ```js
115 /*eslint multiline-ternary: ["error", "never"]*/
116
117 foo > bar ? value :
118 value2;
119
120 foo > bar ?
121 value : value2;
122
123 foo >
124 bar ?
125 value1 :
126 value2;
127 ```
128
129 Examples of **correct** code for this rule with the `"never"` option:
130
131 ```js
132 /*eslint multiline-ternary: ["error", "never"]*/
133
134 foo > bar ? value1 : value2;
135
136 foo > bar ? (baz > qux ? value1 : value2) : value3;
137
138 foo > bar ? (
139 baz > qux ? value1 : value2
140 ) : value3;
141 ```
142
143 ## When Not To Use It
144
145 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.
146
147 ## Related Rules
148
149 * [operator-linebreak](operator-linebreak.md)
150
151 ## Compatibility
152
153 * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary)