]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/multiline-ternary.md
7e4688d637ae8615206924640749e0b477c80639
[pve-eslint.git] / eslint / docs / src / rules / multiline-ternary.md
1 ---
2 title: multiline-ternary
3 rule_type: layout
4 related_rules:
5 - operator-linebreak
6 ---
7
8
9
10 JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.
11
12 For example:
13
14 ```js
15 var foo = bar > baz ? value1 : value2;
16 ```
17
18 The above can be rewritten as the following to improve readability and more clearly delineate the operands:
19
20 ```js
21 var foo = bar > baz ?
22 value1 :
23 value2;
24 ```
25
26 ## Rule Details
27
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.
30
31 ## Options
32
33 This rule has a string option:
34
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.
38
39 ### always
40
41 This is the default option.
42
43 Examples of **incorrect** code for this rule with the `"always"` option:
44
45 ::: incorrect
46
47 ```js
48 /*eslint multiline-ternary: ["error", "always"]*/
49
50 foo > bar ? value1 : value2;
51
52 foo > bar ? value :
53 value2;
54
55 foo > bar ?
56 value : value2;
57 ```
58
59 :::
60
61 Examples of **correct** code for this rule with the `"always"` option:
62
63 ::: correct
64
65 ```js
66 /*eslint multiline-ternary: ["error", "always"]*/
67
68 foo > bar ?
69 value1 :
70 value2;
71
72 foo > bar ?
73 (baz > qux ?
74 value1 :
75 value2) :
76 value3;
77 ```
78
79 :::
80
81 ### always-multiline
82
83 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
84
85 ::: incorrect
86
87 ```js
88 /*eslint multiline-ternary: ["error", "always-multiline"]*/
89
90 foo > bar ? value1 :
91 value2;
92
93 foo > bar ?
94 value1 : value2;
95
96 foo > bar &&
97 bar > baz ? value1 : value2;
98 ```
99
100 :::
101
102 Examples of **correct** code for this rule with the `"always-multiline"` option:
103
104 ::: correct
105
106 ```js
107 /*eslint multiline-ternary: ["error", "always-multiline"]*/
108
109 foo > bar ? value1 : value2;
110
111 foo > bar ?
112 value1 :
113 value2;
114
115 foo > bar ?
116 (baz > qux ? value1 : value2) :
117 value3;
118
119 foo > bar ?
120 (baz > qux ?
121 value1 :
122 value2) :
123 value3;
124
125 foo > bar &&
126 bar > baz ?
127 value1 :
128 value2;
129 ```
130
131 :::
132
133 ### never
134
135 Examples of **incorrect** code for this rule with the `"never"` option:
136
137 ::: incorrect
138
139 ```js
140 /*eslint multiline-ternary: ["error", "never"]*/
141
142 foo > bar ? value :
143 value2;
144
145 foo > bar ?
146 value : value2;
147
148 foo >
149 bar ?
150 value1 :
151 value2;
152 ```
153
154 :::
155
156 Examples of **correct** code for this rule with the `"never"` option:
157
158 ::: correct
159
160 ```js
161 /*eslint multiline-ternary: ["error", "never"]*/
162
163 foo > bar ? value1 : value2;
164
165 foo > bar ? (baz > qux ? value1 : value2) : value3;
166
167 foo > bar ? (
168 baz > qux ? value1 : value2
169 ) : value3;
170 ```
171
172 :::
173
174 ## When Not To Use It
175
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.
177
178 ## Compatibility
179
180 * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary)