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