]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/operator-assignment.js
import 8.23.1 source
[pve-eslint.git] / eslint / tests / lib / rules / operator-assignment.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for operator-assignment rule.
3 * @author Brandon Mills
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/operator-assignment"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
609c276f 19const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022 } });
eb39fafa 20
eb39fafa
DC
21ruleTester.run("operator-assignment", rule, {
22
23 valid: [
24 "x = y",
25 "x = y + x",
26 "x += x + y",
27 "x = (x + y) - z",
28 "x -= y",
29 "x = y - x",
30 "x *= x",
31 "x = y * z",
32 "x = (x * y) * z",
33 "x = y / x",
34 "x /= y",
35 "x %= y",
36 "x <<= y",
37 "x >>= x >> y",
38 "x >>>= y",
39 "x &= y",
40 "x **= y",
41 "x ^= y ^ z",
42 "x |= x | y",
43 "x = x && y",
44 "x = x || y",
45 "x = x < y",
46 "x = x > y",
47 "x = x <= y",
48 "x = x >= y",
49 "x = x instanceof y",
50 "x = x in y",
51 "x = x == y",
52 "x = x != y",
53 "x = x === y",
54 "x = x !== y",
55 "x[y] = x['y'] + z",
56 "x.y = x['y'] / z",
57 "x.y = z + x.y",
58 "x[fn()] = x[fn()] + y",
59 {
60 code: "x += x + y",
61 options: ["always"]
62 },
63 {
64 code: "x = x + y",
65 options: ["never"]
66 },
67 {
68 code: "x = x ** y",
69 options: ["never"]
70 },
71 "x = y ** x",
72 "x = x * y + z",
73 {
74 code: "this.x = this.y + z",
75 options: ["always"]
76 },
77 {
78 code: "this.x = foo.x + y",
79 options: ["always"]
80 },
81 {
82 code: "this.x = foo.this.x + y",
83 options: ["always"]
6f036462 84 },
609c276f 85 "const foo = 0; class C { foo = foo + 1; }",
6f036462
TL
86
87 // does not check logical operators
88 {
89 code: "x = x && y",
90 options: ["always"]
91 },
92 {
93 code: "x = x || y",
94 options: ["always"]
95 },
96 {
97 code: "x = x ?? y",
98 options: ["always"]
99 },
100 {
101 code: "x &&= y",
102 options: ["never"]
103 },
104 {
105 code: "x ||= y",
106 options: ["never"]
107 },
108 {
109 code: "x ??= y",
110 options: ["never"]
eb39fafa
DC
111 }
112 ],
113
114 invalid: [{
115 code: "x = x + y",
116 output: "x += y",
8f9d1d4d 117 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
118 }, {
119 code: "x = x - y",
120 output: "x -= y",
8f9d1d4d 121 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "-=" } }]
eb39fafa
DC
122 }, {
123 code: "x = x * y",
124 output: "x *= y",
8f9d1d4d 125 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
126 }, {
127 code: "x = y * x",
128 output: null, // not fixed (possible change in behavior if y and x have valueOf() functions)
8f9d1d4d 129 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
130 }, {
131 code: "x = (y * z) * x",
132 output: null, // not fixed (possible change in behavior if y/z and x have valueOf() functions)
8f9d1d4d 133 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
134 }, {
135 code: "x = x / y",
136 output: "x /= y",
8f9d1d4d 137 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
138 }, {
139 code: "x = x % y",
140 output: "x %= y",
8f9d1d4d 141 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "%=" } }]
eb39fafa
DC
142 }, {
143 code: "x = x << y",
144 output: "x <<= y",
8f9d1d4d 145 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "<<=" } }]
eb39fafa
DC
146 }, {
147 code: "x = x >> y",
148 output: "x >>= y",
8f9d1d4d 149 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: ">>=" } }]
eb39fafa
DC
150 }, {
151 code: "x = x >>> y",
152 output: "x >>>= y",
8f9d1d4d 153 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: ">>>=" } }]
eb39fafa
DC
154 }, {
155 code: "x = x & y",
156 output: "x &= y",
8f9d1d4d 157 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "&=" } }]
eb39fafa
DC
158 }, {
159 code: "x = x ^ y",
160 output: "x ^= y",
8f9d1d4d 161 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "^=" } }]
eb39fafa
DC
162 }, {
163 code: "x = x | y",
164 output: "x |= y",
8f9d1d4d 165 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "|=" } }]
eb39fafa
DC
166 }, {
167 code: "x[0] = x[0] - y",
168 output: "x[0] -= y",
8f9d1d4d 169 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "-=" } }]
eb39fafa
DC
170 }, {
171 code: "x.y[z['a']][0].b = x.y[z['a']][0].b * 2",
172 output: null, // not fixed; might activate getters more than before
8f9d1d4d 173 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
174 }, {
175 code: "x = x + y",
176 output: "x += y",
177 options: ["always"],
8f9d1d4d 178 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
179 }, {
180 code: "x = (x + y)",
181 output: "x += y",
182 options: ["always"],
8f9d1d4d 183 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
184 }, {
185 code: "x = x + (y)",
186 output: "x += (y)",
187 options: ["always"],
8f9d1d4d 188 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
189 }, {
190 code: "x += (y)",
191 output: "x = x + (y)",
192 options: ["never"],
8f9d1d4d 193 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
194 }, {
195 code: "x += y",
196 output: "x = x + y",
197 options: ["never"],
8f9d1d4d 198 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
199 }, {
200 code: "foo.bar = foo.bar + baz",
201 output: "foo.bar += baz",
8f9d1d4d 202 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
203 }, {
204 code: "foo.bar += baz",
205 output: "foo.bar = foo.bar + baz",
206 options: ["never"],
8f9d1d4d 207 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
208 }, {
209 code: "this.foo = this.foo + bar",
210 output: "this.foo += bar",
8f9d1d4d 211 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
212 }, {
213 code: "this.foo += bar",
214 output: "this.foo = this.foo + bar",
215 options: ["never"],
8f9d1d4d 216 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
217 }, {
218 code: "foo.bar.baz = foo.bar.baz + qux",
219 output: null, // not fixed; fixing would cause a foo.bar getter to activate once rather than twice
8f9d1d4d 220 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
221 }, {
222 code: "foo.bar.baz += qux",
223 output: null, // not fixed; fixing would cause a foo.bar getter to activate twice rather than once
224 options: ["never"],
8f9d1d4d 225 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
226 }, {
227 code: "this.foo.bar = this.foo.bar + baz",
228 output: null, // not fixed; fixing would cause a this.foo getter to activate once rather than twice
8f9d1d4d 229 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
230 }, {
231 code: "this.foo.bar += baz",
232 output: null, // not fixed; fixing would cause a this.foo getter to activate twice rather than once
233 options: ["never"],
8f9d1d4d 234 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
235 }, {
236 code: "foo[bar] = foo[bar] + baz",
237 output: null, // not fixed; fixing would cause bar.toString() to get called once instead of twice
8f9d1d4d 238 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
239 }, {
240 code: "this[foo] = this[foo] + bar",
241 output: null, // not fixed; fixing would cause foo.toString() to get called once instead of twice
8f9d1d4d 242 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
243 }, {
244 code: "foo[bar] >>>= baz",
245 output: null, // not fixed; fixing would cause bar.toString() to get called twice instead of once
246 options: ["never"],
8f9d1d4d 247 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: ">>>=" } }]
eb39fafa
DC
248 }, {
249 code: "this[foo] >>>= bar",
250 output: null, // not fixed; fixing would cause foo.toString() to get called twice instead of once
251 options: ["never"],
8f9d1d4d 252 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: ">>>=" } }]
eb39fafa
DC
253 }, {
254 code: "foo[5] = foo[5] / baz",
255 output: "foo[5] /= baz", // this is ok because 5 is a literal, so toString won't get called
8f9d1d4d 256 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
257 }, {
258 code: "this[5] = this[5] / foo",
259 output: "this[5] /= foo", // this is ok because 5 is a literal, so toString won't get called
8f9d1d4d 260 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
261 }, {
262 code: "/*1*/x/*2*/./*3*/y/*4*/= x.y +/*5*/z/*6*/./*7*/w/*8*/;",
263 output: "/*1*/x/*2*/./*3*/y/*4*/+=/*5*/z/*6*/./*7*/w/*8*/;", // these comments are preserved
264 options: ["always"],
8f9d1d4d 265 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
266 }, {
267 code: "x // 1\n . // 2\n y // 3\n = x.y + //4\n z //5\n . //6\n w;",
268 output: "x // 1\n . // 2\n y // 3\n += //4\n z //5\n . //6\n w;", // these comments are preserved
269 options: ["always"],
8f9d1d4d 270 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
271 }, {
272 code: "x = /*1*/ x + y",
273 output: null, // not fixed; fixing would remove this comment
274 options: ["always"],
8f9d1d4d 275 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
276 }, {
277 code: "x = //1\n x + y",
278 output: null, // not fixed; fixing would remove this comment
279 options: ["always"],
8f9d1d4d 280 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
281 }, {
282 code: "x.y = x/*1*/.y + z",
283 output: null, // not fixed; fixing would remove this comment
284 options: ["always"],
8f9d1d4d 285 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
286 }, {
287 code: "x.y = x. //1\n y + z",
288 output: null, // not fixed; fixing would remove this comment
289 options: ["always"],
8f9d1d4d 290 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
291 }, {
292 code: "x = x /*1*/ + y",
293 output: null, // not fixed; fixing would remove this comment
294 options: ["always"],
8f9d1d4d 295 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
296 }, {
297 code: "x = x //1\n + y",
298 output: null, // not fixed; fixing would remove this comment
299 options: ["always"],
8f9d1d4d 300 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
301 }, {
302 code: "/*1*/x +=/*2*/y/*3*/;",
303 output: "/*1*/x = x +/*2*/y/*3*/;", // these comments are preserved and not duplicated
304 options: ["never"],
8f9d1d4d 305 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
306 }, {
307 code: "x +=//1\n y",
308 output: "x = x +//1\n y", // this comment is preserved and not duplicated
309 options: ["never"],
8f9d1d4d 310 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
311 }, {
312 code: "(/*1*/x += y)",
313 output: "(/*1*/x = x + y)", // this comment is preserved and not duplicated
314 options: ["never"],
8f9d1d4d 315 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
316 }, {
317 code: "x/*1*/+= y",
318 output: null, // not fixed; fixing would duplicate this comment
319 options: ["never"],
8f9d1d4d 320 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
321 }, {
322 code: "x //1\n += y",
323 output: null, // not fixed; fixing would duplicate this comment
324 options: ["never"],
8f9d1d4d 325 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
326 }, {
327 code: "(/*1*/x) += y",
328 output: null, // not fixed; fixing would duplicate this comment
329 options: ["never"],
8f9d1d4d 330 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
331 }, {
332 code: "x/*1*/.y += z",
333 output: null, // not fixed; fixing would duplicate this comment
334 options: ["never"],
8f9d1d4d 335 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
336 }, {
337 code: "x.//1\n y += z",
338 output: null, // not fixed; fixing would duplicate this comment
339 options: ["never"],
8f9d1d4d 340 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
341 }, {
342 code: "(foo.bar) ^= ((((((((((((((((baz))))))))))))))))",
343 output: "(foo.bar) = (foo.bar) ^ ((((((((((((((((baz))))))))))))))))",
344 options: ["never"],
8f9d1d4d 345 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "^=" } }]
eb39fafa
DC
346 }, {
347 code: "foo = foo ** bar",
348 output: "foo **= bar",
8f9d1d4d 349 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "**=" } }]
eb39fafa
DC
350 }, {
351 code: "foo **= bar",
352 output: "foo = foo ** bar",
353 options: ["never"],
8f9d1d4d 354 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "**=" } }]
eb39fafa
DC
355 }, {
356 code: "foo *= bar + 1",
357 output: "foo = foo * (bar + 1)",
358 options: ["never"],
8f9d1d4d 359 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
360 }, {
361 code: "foo -= bar - baz",
362 output: "foo = foo - (bar - baz)",
363 options: ["never"],
8f9d1d4d 364 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "-=" } }]
eb39fafa
DC
365 }, {
366 code: "foo += bar + baz",
367 output: "foo = foo + (bar + baz)", // addition is not associative in JS, e.g. (1 + 2) + '3' !== 1 + (2 + '3')
368 options: ["never"],
8f9d1d4d 369 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
370 }, {
371 code: "foo += bar = 1",
372 output: "foo = foo + (bar = 1)",
373 options: ["never"],
8f9d1d4d 374 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
375 }, {
376 code: "foo *= (bar + 1)",
377 output: "foo = foo * (bar + 1)",
378 options: ["never"],
8f9d1d4d 379 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "*=" } }]
eb39fafa
DC
380 }, {
381 code: "foo+=-bar",
382 output: "foo= foo+-bar", // tokens can be adjacent
383 options: ["never"],
8f9d1d4d 384 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
385 }, {
386 code: "foo/=bar",
387 output: "foo= foo/bar", // tokens can be adjacent
388 options: ["never"],
8f9d1d4d 389 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
390 }, {
391 code: "foo/=/**/bar",
392 output: "foo= foo/ /**/bar", // // tokens cannot be adjacent, insert a space between
393 options: ["never"],
8f9d1d4d 394 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
395 }, {
396 code: "foo/=//\nbar",
397 output: "foo= foo/ //\nbar", // // tokens cannot be adjacent, insert a space between
398 options: ["never"],
8f9d1d4d 399 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
400 }, {
401 code: "foo/=/^bar$/",
402 output: "foo= foo/ /^bar$/", // // tokens cannot be adjacent, insert a space between
403 options: ["never"],
8f9d1d4d 404 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "/=" } }]
eb39fafa
DC
405 }, {
406 code: "foo+=+bar",
407 output: "foo= foo+ +bar", // tokens cannot be adjacent, insert a space between
408 options: ["never"],
8f9d1d4d 409 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
410 }, {
411 code: "foo+= +bar",
412 output: "foo= foo+ +bar", // tokens cannot be adjacent, but there is already a space between
413 options: ["never"],
8f9d1d4d 414 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
415 }, {
416 code: "foo+=/**/+bar",
417 output: "foo= foo+/**/+bar", // tokens cannot be adjacent, but there is a comment between
418 options: ["never"],
8f9d1d4d 419 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
eb39fafa
DC
420 }, {
421 code: "foo+=+bar===baz",
422 output: "foo= foo+(+bar===baz)", // tokens cannot be adjacent, but the right side will be parenthesised
423 options: ["never"],
8f9d1d4d 424 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "+=" } }]
6f036462
TL
425 },
426
427 // Optional chaining
428 {
429 code: "(obj?.a).b = (obj?.a).b + y",
430 output: null,
8f9d1d4d 431 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
6f036462
TL
432 },
433 {
434 code: "obj.a = obj?.a + b",
435 output: null,
8f9d1d4d 436 errors: [{ messageId: "replaced", type: "AssignmentExpression", data: { operator: "+=" } }]
6f036462
TL
437 }
438
439 ]
eb39fafa
DC
440
441});