]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-unneeded-ternary.js
25aab8a0cd181477c2f0a102dbd351c8a7820305
[pve-eslint.git] / eslint / tests / lib / rules / no-unneeded-ternary.js
1 /**
2 * @fileoverview Tests for no-unneeded-ternary rule.
3 * @author Gyandeep Singh
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-unneeded-ternary"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-unneeded-ternary", rule, {
22 valid: [
23 "config.newIsCap = config.newIsCap !== false",
24 "var a = x === 2 ? 'Yes' : 'No';",
25 "var a = x === 2 ? true : 'No';",
26 "var a = x === 2 ? 'Yes' : false;",
27 "var a = x === 2 ? 'true' : 'false';",
28 "var a = foo ? foo : bar;",
29 "var value = 'a';var canSet = true;var result = value || (canSet ? 'unset' : 'can not set')",
30 "var a = foo ? bar : foo;",
31 "foo ? bar : foo;",
32 "var a = f(x ? x : 1)",
33 "f(x ? x : 1);",
34 "foo ? foo : bar;",
35 "var a = foo ? 'Yes' : foo;",
36 {
37 code: "var a = foo ? 'Yes' : foo;",
38 options: [{ defaultAssignment: false }]
39 },
40 {
41 code: "var a = foo ? bar : foo;",
42 options: [{ defaultAssignment: false }]
43 },
44 {
45 code: "foo ? bar : foo;",
46 options: [{ defaultAssignment: false }]
47 }
48 ],
49 invalid: [
50 {
51 code: "var a = x === 2 ? true : false;",
52 output: "var a = x === 2;",
53 errors: [{
54 messageId: "unnecessaryConditionalExpression",
55 type: "ConditionalExpression",
56 line: 1,
57 column: 19
58 }]
59 },
60 {
61 code: "var a = x >= 2 ? true : false;",
62 output: "var a = x >= 2;",
63 errors: [{
64 messageId: "unnecessaryConditionalExpression",
65 type: "ConditionalExpression",
66 line: 1,
67 column: 18
68 }]
69 },
70 {
71 code: "var a = x ? true : false;",
72 output: "var a = !!x;",
73 errors: [{
74 messageId: "unnecessaryConditionalExpression",
75 type: "ConditionalExpression",
76 line: 1,
77 column: 13
78 }]
79 },
80 {
81 code: "var a = x === 1 ? false : true;",
82 output: "var a = x !== 1;",
83 errors: [{
84 messageId: "unnecessaryConditionalExpression",
85 type: "ConditionalExpression",
86 line: 1,
87 column: 19
88 }]
89 },
90 {
91 code: "var a = x != 1 ? false : true;",
92 output: "var a = x == 1;",
93 errors: [{
94 messageId: "unnecessaryConditionalExpression",
95 type: "ConditionalExpression",
96 line: 1,
97 column: 18
98 }]
99 },
100 {
101 code: "var a = foo() ? false : true;",
102 output: "var a = !foo();",
103 errors: [{
104 messageId: "unnecessaryConditionalExpression",
105 type: "ConditionalExpression",
106 line: 1,
107 column: 17
108 }]
109 },
110 {
111 code: "var a = !foo() ? false : true;",
112 output: "var a = !!foo();",
113 errors: [{
114 messageId: "unnecessaryConditionalExpression",
115 type: "ConditionalExpression",
116 line: 1,
117 column: 18
118 }]
119 },
120 {
121 code: "var a = foo + bar ? false : true;",
122 output: "var a = !(foo + bar);",
123 errors: [{
124 messageId: "unnecessaryConditionalExpression",
125 type: "ConditionalExpression",
126 line: 1,
127 column: 21
128 }]
129 },
130 {
131 code: "var a = x instanceof foo ? false : true;",
132 output: "var a = !(x instanceof foo);",
133 errors: [{
134 messageId: "unnecessaryConditionalExpression",
135 type: "ConditionalExpression",
136 line: 1,
137 column: 28
138 }]
139 },
140 {
141 code: "var a = foo ? false : false;",
142 output: "var a = false;",
143 errors: [{
144 messageId: "unnecessaryConditionalExpression",
145 type: "ConditionalExpression",
146 line: 1,
147 column: 15
148 }]
149 },
150 {
151 code: "var a = foo() ? false : false;",
152 output: null,
153 errors: [{
154 messageId: "unnecessaryConditionalExpression",
155 type: "ConditionalExpression",
156 line: 1,
157 column: 17
158 }]
159 },
160 {
161 code: "var a = x instanceof foo ? true : false;",
162 output: "var a = x instanceof foo;",
163 errors: [{
164 messageId: "unnecessaryConditionalExpression",
165 type: "ConditionalExpression",
166 line: 1,
167 column: 28
168 }]
169 },
170 {
171 code: "var a = !foo ? true : false;",
172 output: "var a = !foo;",
173 errors: [{
174 messageId: "unnecessaryConditionalExpression",
175 type: "ConditionalExpression",
176 line: 1,
177 column: 16
178 }]
179 },
180 {
181 code: `
182 var value = 'a'
183 var canSet = true
184 var result = value ? value : canSet ? 'unset' : 'can not set'
185 `,
186 output: `
187 var value = 'a'
188 var canSet = true
189 var result = value || (canSet ? 'unset' : 'can not set')
190 `,
191 options: [{ defaultAssignment: false }],
192 errors: [{
193 messageId: "unnecessaryConditionalAssignment",
194 type: "ConditionalExpression",
195 line: 4,
196 column: 38
197 }]
198 },
199 {
200 code: "foo ? foo : (bar ? baz : qux)",
201 output: "foo || (bar ? baz : qux)",
202 options: [{ defaultAssignment: false }],
203 errors: [{
204 messageId: "unnecessaryConditionalAssignment",
205 type: "ConditionalExpression",
206 line: 1,
207 column: 7
208 }]
209 },
210 {
211 code: "function* fn() { foo ? foo : yield bar }",
212 output: "function* fn() { foo || (yield bar) }",
213 options: [{ defaultAssignment: false }],
214 parserOptions: { ecmaVersion: 6 },
215 errors: [{
216 messageId: "unnecessaryConditionalAssignment",
217 type: "ConditionalExpression",
218 line: 1,
219 column: 24
220 }]
221 },
222 {
223 code: "var a = foo ? foo : 'No';",
224 output: "var a = foo || 'No';",
225 options: [{ defaultAssignment: false }],
226 errors: [{
227 messageId: "unnecessaryConditionalAssignment",
228 type: "ConditionalExpression",
229 line: 1,
230 column: 15
231 }]
232 },
233 {
234 code: "var a = ((foo)) ? (((((foo))))) : ((((((((((((((bar))))))))))))));",
235 output: "var a = ((foo)) || ((((((((((((((bar))))))))))))));",
236 options: [{ defaultAssignment: false }],
237 errors: [{
238 messageId: "unnecessaryConditionalAssignment",
239 type: "ConditionalExpression",
240 line: 1,
241 column: 24
242 }]
243 },
244 {
245 code: "var a = b ? b : c => c;",
246 output: "var a = b || (c => c);",
247 options: [{ defaultAssignment: false }],
248 parserOptions: { ecmaVersion: 2015 },
249 errors: [{
250 messageId: "unnecessaryConditionalAssignment",
251 type: "ConditionalExpression",
252 line: 1,
253 column: 13
254 }]
255 },
256 {
257 code: "var a = b ? b : c = 0;",
258 output: "var a = b || (c = 0);",
259 options: [{ defaultAssignment: false }],
260 parserOptions: { ecmaVersion: 2015 },
261 errors: [{
262 messageId: "unnecessaryConditionalAssignment",
263 type: "ConditionalExpression",
264 line: 1,
265 column: 13
266 }]
267 },
268 {
269 code: "var a = b ? b : (c => c);",
270 output: "var a = b || (c => c);",
271 options: [{ defaultAssignment: false }],
272 parserOptions: { ecmaVersion: 2015 },
273 errors: [{
274 messageId: "unnecessaryConditionalAssignment",
275 type: "ConditionalExpression",
276 line: 1,
277 column: 13
278 }]
279 },
280 {
281 code: "var a = b ? b : (c = 0);",
282 output: "var a = b || (c = 0);",
283 options: [{ defaultAssignment: false }],
284 parserOptions: { ecmaVersion: 2015 },
285 errors: [{
286 messageId: "unnecessaryConditionalAssignment",
287 type: "ConditionalExpression",
288 line: 1,
289 column: 13
290 }]
291 },
292 {
293 code: "var a = b ? b : (c) => (c);",
294 output: "var a = b || ((c) => (c));",
295 options: [{ defaultAssignment: false }],
296 parserOptions: { ecmaVersion: 2015 },
297 errors: [{
298 messageId: "unnecessaryConditionalAssignment",
299 type: "ConditionalExpression",
300 line: 1,
301 column: 13
302 }]
303 },
304 {
305 code: "var a = b ? b : c, d; // this is ((b ? b : c), (d))",
306 output: "var a = b || c, d; // this is ((b ? b : c), (d))",
307 options: [{ defaultAssignment: false }],
308 parserOptions: { ecmaVersion: 2015 },
309 errors: [{
310 messageId: "unnecessaryConditionalAssignment",
311 type: "ConditionalExpression",
312 line: 1,
313 column: 13
314 }]
315 },
316 {
317 code: "var a = b ? b : (c, d);",
318 output: "var a = b || (c, d);",
319 options: [{ defaultAssignment: false }],
320 parserOptions: { ecmaVersion: 2015 },
321 errors: [{
322 messageId: "unnecessaryConditionalAssignment",
323 type: "ConditionalExpression",
324 line: 1,
325 column: 13
326 }]
327 },
328 {
329 code: "f(x ? x : 1);",
330 output: "f(x || 1);",
331 options: [{ defaultAssignment: false }],
332 errors: [{
333 messageId: "unnecessaryConditionalAssignment",
334 type: "ConditionalExpression",
335 line: 1,
336 column: 7
337 }]
338 },
339 {
340 code: "x ? x : 1;",
341 output: "x || 1;",
342 options: [{ defaultAssignment: false }],
343 errors: [{
344 messageId: "unnecessaryConditionalAssignment",
345 type: "ConditionalExpression",
346 line: 1,
347 column: 5
348 }]
349 },
350 {
351 code: "var a = foo ? foo : bar;",
352 output: "var a = foo || bar;",
353 options: [{ defaultAssignment: false }],
354 errors: [{
355 messageId: "unnecessaryConditionalAssignment",
356 type: "ConditionalExpression",
357 line: 1,
358 column: 15
359 }]
360 }
361 ]
362 });