]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: multiline-ternary | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - operator-linebreak | |
6 | --- | |
7 | ||
8 | ||
eb39fafa DC |
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. | |
8f9d1d4d | 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. |
eb39fafa DC |
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. | |
6f036462 | 37 | * `"never"` disallows newlines between the operands of a ternary expression. |
eb39fafa DC |
38 | |
39 | ### always | |
40 | ||
41 | This is the default option. | |
42 | ||
43 | Examples 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 | ||
50 | foo > bar ? value1 : value2; | |
51 | ||
52 | foo > bar ? value : | |
53 | value2; | |
54 | ||
55 | foo > bar ? | |
56 | value : value2; | |
57 | ``` | |
58 | ||
8f9d1d4d DC |
59 | ::: |
60 | ||
eb39fafa DC |
61 | Examples 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 | ||
68 | foo > bar ? | |
69 | value1 : | |
70 | value2; | |
71 | ||
72 | foo > bar ? | |
73 | (baz > qux ? | |
74 | value1 : | |
75 | value2) : | |
76 | value3; | |
77 | ``` | |
78 | ||
8f9d1d4d DC |
79 | ::: |
80 | ||
eb39fafa DC |
81 | ### always-multiline |
82 | ||
83 | Examples 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 | ||
90 | foo > bar ? value1 : | |
91 | value2; | |
92 | ||
93 | foo > bar ? | |
94 | value1 : value2; | |
95 | ||
96 | foo > bar && | |
97 | bar > baz ? value1 : value2; | |
98 | ``` | |
99 | ||
8f9d1d4d DC |
100 | ::: |
101 | ||
eb39fafa DC |
102 | Examples 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 | ||
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 | ||
8f9d1d4d DC |
131 | ::: |
132 | ||
eb39fafa DC |
133 | ### never |
134 | ||
135 | Examples 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 | ||
142 | foo > bar ? value : | |
143 | value2; | |
144 | ||
145 | foo > bar ? | |
146 | value : value2; | |
147 | ||
148 | foo > | |
149 | bar ? | |
150 | value1 : | |
151 | value2; | |
152 | ``` | |
153 | ||
8f9d1d4d DC |
154 | ::: |
155 | ||
eb39fafa DC |
156 | Examples 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 | ||
163 | foo > bar ? value1 : value2; | |
164 | ||
165 | foo > bar ? (baz > qux ? value1 : value2) : value3; | |
6f036462 TL |
166 | |
167 | foo > 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 | ||
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 | ||
eb39fafa DC |
178 | ## Compatibility |
179 | ||
180 | * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary) |