]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for complexity rule. | |
3 | * @author Gyandeep Singh | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/accessor-pairs"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Helpers | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("accessor-pairs", rule, { | |
22 | valid: [ | |
23 | ||
24 | //------------------------------------------------------------------------------ | |
25 | // General | |
26 | //------------------------------------------------------------------------------ | |
27 | ||
28 | // Does not check object patterns | |
29 | { | |
30 | code: "var { get: foo } = bar; ({ set: foo } = bar);", | |
31 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
32 | parserOptions: { ecmaVersion: 6 } | |
33 | }, | |
34 | { | |
35 | code: "var { set } = foo; ({ get } = foo);", | |
36 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
37 | parserOptions: { ecmaVersion: 6 } | |
38 | }, | |
39 | ||
40 | //------------------------------------------------------------------------------ | |
41 | // Object literals | |
42 | //------------------------------------------------------------------------------ | |
43 | ||
44 | // Test default settings, this would be an error if `getWithoutSet` was set to `true` | |
45 | "var o = { get a() {} }", | |
46 | { | |
47 | code: "var o = { get a() {} }", | |
48 | options: [{}] | |
49 | }, | |
50 | ||
51 | // No accessors | |
52 | { | |
53 | code: "var o = {};", | |
54 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
55 | }, | |
56 | { | |
57 | code: "var o = { a: 1 };", | |
58 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
59 | }, | |
60 | { | |
61 | code: "var o = { a };", | |
62 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
63 | parserOptions: { ecmaVersion: 6 } | |
64 | }, | |
65 | { | |
66 | code: "var o = { a: get };", | |
67 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
68 | }, | |
69 | { | |
70 | code: "var o = { a: set };", | |
71 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
72 | }, | |
73 | { | |
74 | code: "var o = { get: function(){} };", | |
75 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
76 | }, | |
77 | { | |
78 | code: "var o = { set: function(foo){} };", | |
79 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
80 | }, | |
81 | { | |
82 | code: "var o = { get };", | |
83 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
84 | parserOptions: { ecmaVersion: 6 } | |
85 | }, | |
86 | { | |
87 | code: "var o = { set };", | |
88 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
89 | parserOptions: { ecmaVersion: 6 } | |
90 | }, | |
91 | { | |
92 | code: "var o = { [get]: function() {} };", | |
93 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
94 | parserOptions: { ecmaVersion: 6 } | |
95 | }, | |
96 | { | |
97 | code: "var o = { [set]: function(foo) {} };", | |
98 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
99 | parserOptions: { ecmaVersion: 6 } | |
100 | }, | |
101 | { | |
102 | code: "var o = { get() {} };", | |
103 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
104 | parserOptions: { ecmaVersion: 6 } | |
105 | }, | |
106 | { | |
107 | code: "var o = { set(foo) {} };", | |
108 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
109 | parserOptions: { ecmaVersion: 6 } | |
110 | }, | |
111 | ||
112 | // Disabled options | |
113 | { | |
114 | code: "var o = { get a() {} };", | |
115 | options: [{ setWithoutGet: false, getWithoutSet: false }] | |
116 | }, | |
117 | { | |
118 | code: "var o = { get a() {} };", | |
119 | options: [{ setWithoutGet: true, getWithoutSet: false }] | |
120 | }, | |
121 | { | |
122 | code: "var o = { set a(foo) {} };", | |
123 | options: [{ setWithoutGet: false, getWithoutSet: false }] | |
124 | }, | |
125 | { | |
126 | code: "var o = { set a(foo) {} };", | |
127 | options: [{ setWithoutGet: false, getWithoutSet: true }] | |
128 | }, | |
129 | { | |
130 | code: "var o = { set a(foo) {} };", | |
131 | options: [{ setWithoutGet: false }] | |
132 | }, | |
133 | ||
134 | // Valid pairs with identifiers | |
135 | { | |
136 | code: "var o = { get a() {}, set a(foo) {} };", | |
137 | options: [{ setWithoutGet: false, getWithoutSet: true }] | |
138 | }, | |
139 | { | |
140 | code: "var o = { get a() {}, set a(foo) {} };", | |
141 | options: [{ setWithoutGet: true, getWithoutSet: false }] | |
142 | }, | |
143 | { | |
144 | code: "var o = { get a() {}, set a(foo) {} };", | |
145 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
146 | }, | |
147 | { | |
148 | code: "var o = { set a(foo) {}, get a() {} };", | |
149 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
150 | }, | |
151 | ||
152 | // Valid pairs with statically computed names | |
153 | { | |
154 | code: "var o = { get 'a'() {}, set 'a'(foo) {} };", | |
155 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
156 | }, | |
157 | { | |
158 | code: "var o = { get a() {}, set 'a'(foo) {} };", | |
159 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
160 | }, | |
161 | { | |
162 | code: "var o = { get ['abc']() {}, set ['abc'](foo) {} };", | |
163 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
164 | parserOptions: { ecmaVersion: 6 } | |
165 | }, | |
166 | { | |
167 | code: "var o = { get [1e2]() {}, set 100(foo) {} };", | |
168 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
169 | parserOptions: { ecmaVersion: 6 } | |
170 | }, | |
171 | { | |
172 | code: "var o = { get abc() {}, set [`abc`](foo) {} };", | |
173 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
174 | parserOptions: { ecmaVersion: 6 } | |
175 | }, | |
176 | { | |
177 | code: "var o = { get ['123']() {}, set 123(foo) {} };", | |
178 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
179 | parserOptions: { ecmaVersion: 6 } | |
180 | }, | |
181 | ||
182 | // Valid pairs with expressions | |
183 | { | |
184 | code: "var o = { get [a]() {}, set [a](foo) {} };", | |
185 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
186 | parserOptions: { ecmaVersion: 6 } | |
187 | }, | |
188 | { | |
189 | code: "var o = { get [a]() {}, set [(a)](foo) {} };", | |
190 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
191 | parserOptions: { ecmaVersion: 6 } | |
192 | }, | |
193 | { | |
194 | code: "var o = { get [(a)]() {}, set [a](foo) {} };", | |
195 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
196 | parserOptions: { ecmaVersion: 6 } | |
197 | }, | |
198 | { | |
199 | code: "var o = { get [a]() {}, set [ a ](foo) {} };", | |
200 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
201 | parserOptions: { ecmaVersion: 6 } | |
202 | }, | |
203 | { | |
204 | code: "var o = { get [/*comment*/a/*comment*/]() {}, set [a](foo) {} };", | |
205 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
206 | parserOptions: { ecmaVersion: 6 } | |
207 | }, | |
208 | { | |
209 | code: "var o = { get [f()]() {}, set [f()](foo) {} };", | |
210 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
211 | parserOptions: { ecmaVersion: 6 } | |
212 | }, | |
213 | { | |
214 | code: "var o = { get [f(a)]() {}, set [f(a)](foo) {} };", | |
215 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
216 | parserOptions: { ecmaVersion: 6 } | |
217 | }, | |
218 | { | |
219 | code: "var o = { get [a + b]() {}, set [a + b](foo) {} };", | |
220 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
221 | parserOptions: { ecmaVersion: 6 } | |
222 | }, | |
223 | { | |
224 | code: "var o = { get [`${a}`]() {}, set [`${a}`](foo) {} };", | |
225 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
226 | parserOptions: { ecmaVersion: 6 } | |
227 | }, | |
228 | ||
229 | // Multiple valid pairs in the same literal | |
230 | { | |
231 | code: "var o = { get a() {}, set a(foo) {}, get b() {}, set b(bar) {} };", | |
232 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
233 | }, | |
234 | { | |
235 | code: "var o = { get a() {}, set c(foo) {}, set a(bar) {}, get b() {}, get c() {}, set b(baz) {} };", | |
236 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
237 | }, | |
238 | ||
239 | // Valid pairs with other elements | |
240 | { | |
241 | code: "var o = { get a() {}, set a(foo) {}, b: bar };", | |
242 | options: [{ setWithoutGet: true, getWithoutSet: true }] | |
243 | }, | |
244 | { | |
245 | code: "var o = { get a() {}, b, set a(foo) {} };", | |
246 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
247 | parserOptions: { ecmaVersion: 6 } | |
248 | }, | |
249 | { | |
250 | code: "var o = { get a() {}, ...b, set a(foo) {} };", | |
251 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
252 | parserOptions: { ecmaVersion: 2018 } | |
253 | }, | |
254 | { | |
255 | code: "var o = { get a() {}, set a(foo) {}, ...a };", | |
256 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
257 | parserOptions: { ecmaVersion: 2018 } | |
258 | }, | |
259 | ||
260 | // Duplicate keys. This is the responsibility of no-dupe-keys, but this rule still checks is there the other accessor kind. | |
261 | { | |
262 | code: "var o = { get a() {}, get a() {}, set a(foo) {}, };", | |
263 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
264 | parserOptions: { ecmaVersion: 6 } | |
265 | }, | |
266 | { | |
267 | code: "var o = { get a() {}, set a(foo) {}, get a() {} };", | |
268 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
269 | parserOptions: { ecmaVersion: 6 } | |
270 | }, | |
271 | { | |
272 | code: "var o = { get a() {}, set a(foo) {}, set a(foo) {} };", | |
273 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
274 | parserOptions: { ecmaVersion: 6 } | |
275 | }, | |
276 | { | |
277 | code: "var o = { set a(bar) {}, get a() {}, set a(foo) {} };", | |
278 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
279 | parserOptions: { ecmaVersion: 6 } | |
280 | }, | |
281 | { | |
282 | code: "var o = { get a() {}, get a() {} };", | |
283 | options: [{ setWithoutGet: true, getWithoutSet: false }], | |
284 | parserOptions: { ecmaVersion: 6 } | |
285 | }, | |
286 | { | |
287 | code: "var o = { set a(foo) {}, set a(foo) {} };", | |
288 | options: [{ setWithoutGet: false, getWithoutSet: true }], | |
289 | parserOptions: { ecmaVersion: 6 } | |
290 | }, | |
291 | { | |
292 | code: "var o = { get a() {}, set a(foo) {}, a };", | |
293 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
294 | parserOptions: { ecmaVersion: 6 } | |
295 | }, | |
296 | { | |
297 | code: "var o = { a, get a() {}, set a(foo) {} };", | |
298 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
299 | parserOptions: { ecmaVersion: 6 } | |
300 | }, | |
301 | ||
302 | /* | |
303 | * This should be actually invalid by this rule! | |
304 | * This code creates a property with the setter only, the getter will be ignored. | |
305 | * It's treated as 3 attempts to define the same key, and the last wins. | |
306 | * However, this edge case is not covered, it should be reported by no-dupe-keys anyway. | |
307 | */ | |
308 | { | |
309 | code: "var o = { get a() {}, a:1, set a(foo) {} };", | |
310 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
311 | parserOptions: { ecmaVersion: 6 } | |
312 | }, | |
313 | ||
314 | //------------------------------------------------------------------------------ | |
315 | // Property descriptors | |
316 | //------------------------------------------------------------------------------ | |
317 | ||
318 | "var o = {a: 1};\n Object.defineProperty(o, 'b', \n{set: function(value) {\n val = value; \n},\n get: function() {\n return val; \n} \n});", | |
319 | ||
320 | // https://github.com/eslint/eslint/issues/3262 | |
321 | "var o = {set: function() {}}", | |
322 | "Object.defineProperties(obj, {set: {value: function() {}}});", | |
323 | "Object.create(null, {set: {value: function() {}}});", | |
324 | { code: "var o = {get: function() {}}", options: [{ getWithoutSet: true }] }, | |
325 | { code: "var o = {[set]: function() {}}", parserOptions: { ecmaVersion: 6 } }, | |
326 | { code: "var set = 'value'; Object.defineProperty(obj, 'foo', {[set]: function(value) {}});", parserOptions: { ecmaVersion: 6 } }, | |
327 | ||
328 | //------------------------------------------------------------------------------ | |
329 | // Classes | |
330 | //------------------------------------------------------------------------------ | |
331 | ||
332 | // Test default settings | |
333 | { | |
334 | code: "class A { get a() {} }", | |
335 | options: [{ enforceForClassMembers: true }], | |
336 | parserOptions: { ecmaVersion: 6 } | |
337 | }, | |
338 | ||
339 | // Explicitly disabled option | |
340 | { | |
341 | code: "class A { set a(foo) {} }", | |
342 | options: [{ enforceForClassMembers: false }], | |
343 | parserOptions: { ecmaVersion: 6 } | |
344 | }, | |
345 | { | |
346 | code: "class A { get a() {} set b(foo) {} static get c() {} static set d(bar) {} }", | |
347 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: false }], | |
348 | parserOptions: { ecmaVersion: 6 } | |
349 | }, | |
350 | { | |
351 | code: "(class A { get a() {} set b(foo) {} static get c() {} static set d(bar) {} });", | |
352 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: false }], | |
353 | parserOptions: { ecmaVersion: 6 } | |
354 | }, | |
355 | ||
356 | // Disabled accessor kind options | |
357 | { | |
358 | code: "class A { get a() {} }", | |
359 | options: [{ setWithoutGet: true, getWithoutSet: false, enforceForClassMembers: true }], | |
360 | parserOptions: { ecmaVersion: 6 } | |
361 | }, | |
362 | { | |
363 | code: "class A { set a(foo) {} }", | |
364 | options: [{ setWithoutGet: false, getWithoutSet: true, enforceForClassMembers: true }], | |
365 | parserOptions: { ecmaVersion: 6 } | |
366 | }, | |
367 | { | |
368 | code: "class A { static get a() {} }", | |
369 | options: [{ setWithoutGet: true, getWithoutSet: false, enforceForClassMembers: true }], | |
370 | parserOptions: { ecmaVersion: 6 } | |
371 | }, | |
372 | { | |
373 | code: "class A { static set a(foo) {} }", | |
374 | options: [{ setWithoutGet: false, getWithoutSet: true, enforceForClassMembers: true }], | |
375 | parserOptions: { ecmaVersion: 6 } | |
376 | }, | |
377 | { | |
378 | code: "A = class { set a(foo) {} };", | |
379 | options: [{ setWithoutGet: false, getWithoutSet: true, enforceForClassMembers: true }], | |
380 | parserOptions: { ecmaVersion: 6 } | |
381 | }, | |
382 | { | |
383 | code: "class A { get a() {} set b(foo) {} static get c() {} static set d(bar) {} }", | |
384 | options: [{ setWithoutGet: false, getWithoutSet: false, enforceForClassMembers: true }], | |
385 | parserOptions: { ecmaVersion: 6 } | |
386 | }, | |
387 | ||
388 | // No accessors | |
389 | { | |
390 | code: "class A {}", | |
391 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
392 | parserOptions: { ecmaVersion: 6 } | |
393 | }, | |
394 | { | |
395 | code: "(class {})", | |
396 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
397 | parserOptions: { ecmaVersion: 6 } | |
398 | }, | |
399 | { | |
400 | code: "class A { constructor () {} }", | |
401 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
402 | parserOptions: { ecmaVersion: 6 } | |
403 | }, | |
404 | { | |
405 | code: "class A { a() {} }", | |
406 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
407 | parserOptions: { ecmaVersion: 6 } | |
408 | }, | |
409 | { | |
410 | code: "class A { static a() {} 'b'() {} }", | |
411 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
412 | parserOptions: { ecmaVersion: 6 } | |
413 | }, | |
414 | { | |
415 | code: "class A { [a]() {} }", | |
416 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
417 | parserOptions: { ecmaVersion: 6 } | |
418 | }, | |
419 | { | |
420 | code: "A = class { a() {} static a() {} b() {} static c() {} }", | |
421 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
422 | parserOptions: { ecmaVersion: 6 } | |
423 | }, | |
424 | ||
425 | // Valid pairs with identifiers | |
426 | { | |
427 | code: "class A { get a() {} set a(foo) {} }", | |
428 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
429 | parserOptions: { ecmaVersion: 6 } | |
430 | }, | |
431 | { | |
432 | code: "class A { set a(foo) {} get a() {} }", | |
433 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
434 | parserOptions: { ecmaVersion: 6 } | |
435 | }, | |
436 | { | |
437 | code: "class A { static get a() {} static set a(foo) {} }", | |
438 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
439 | parserOptions: { ecmaVersion: 6 } | |
440 | }, | |
441 | { | |
442 | code: "class A { static set a(foo) {} static get a() {} }", | |
443 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
444 | parserOptions: { ecmaVersion: 6 } | |
445 | }, | |
446 | { | |
447 | code: "(class { set a(foo) {} get a() {} });", | |
448 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
449 | parserOptions: { ecmaVersion: 6 } | |
450 | }, | |
451 | ||
452 | // Valid pairs with statically computed names | |
453 | { | |
454 | code: "class A { get 'a'() {} set ['a'](foo) {} }", | |
455 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
456 | parserOptions: { ecmaVersion: 6 } | |
457 | }, | |
458 | { | |
459 | code: "class A { set [`a`](foo) {} get a() {} }", | |
460 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
461 | parserOptions: { ecmaVersion: 6 } | |
462 | }, | |
463 | { | |
464 | code: "class A { get 'a'() {} set a(foo) {} }", | |
465 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
466 | parserOptions: { ecmaVersion: 6 } | |
467 | }, | |
468 | { | |
469 | code: "A = class { static get 1e2() {} static set [100](foo) {} };", | |
470 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
471 | parserOptions: { ecmaVersion: 6 } | |
472 | }, | |
473 | ||
474 | // Valid pairs with expressions | |
475 | { | |
476 | code: "class A { get [a]() {} set [a](foo) {} }", | |
477 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
478 | parserOptions: { ecmaVersion: 6 } | |
479 | }, | |
480 | { | |
481 | code: "A = class { set [(f())](foo) {} get [(f())]() {} };", | |
482 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
483 | parserOptions: { ecmaVersion: 6 } | |
484 | }, | |
485 | { | |
486 | code: "class A { static set [f(a)](foo) {} static get [f(a)]() {} }", | |
487 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
488 | parserOptions: { ecmaVersion: 6 } | |
489 | }, | |
490 | ||
491 | // Multiple valid pairs in the same class | |
492 | { | |
493 | code: "class A { get a() {} set b(foo) {} set a(bar) {} get b() {} }", | |
494 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
495 | parserOptions: { ecmaVersion: 6 } | |
496 | }, | |
497 | { | |
498 | code: "class A { get a() {} set a(bar) {} b() {} set c(foo) {} get c() {} }", | |
499 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
500 | parserOptions: { ecmaVersion: 6 } | |
501 | }, | |
502 | { | |
503 | code: "(class { get a() {} static set a(foo) {} set a(bar) {} static get a() {} });", | |
504 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
505 | parserOptions: { ecmaVersion: 6 } | |
506 | }, | |
507 | ||
508 | // Valid pairs with other elements | |
509 | { | |
510 | code: "class A { get a() {} b() {} set a(foo) {} }", | |
511 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
512 | parserOptions: { ecmaVersion: 6 } | |
513 | }, | |
514 | { | |
515 | code: "class A { set a(foo) {} get a() {} b() {} }", | |
516 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
517 | parserOptions: { ecmaVersion: 6 } | |
518 | }, | |
519 | { | |
520 | code: "class A { a() {} get b() {} c() {} set b(foo) {} d() {} }", | |
521 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
522 | parserOptions: { ecmaVersion: 6 } | |
523 | }, | |
524 | { | |
525 | code: "class A { get a() {} set a(foo) {} static a() {} }", | |
526 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
527 | parserOptions: { ecmaVersion: 6 } | |
528 | }, | |
529 | { | |
530 | code: "A = class { static get a() {} static b() {} static set a(foo) {} };", | |
531 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
532 | parserOptions: { ecmaVersion: 6 } | |
533 | }, | |
534 | { | |
535 | code: "A = class { static set a(foo) {} static get a() {} a() {} };", | |
536 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
537 | parserOptions: { ecmaVersion: 6 } | |
538 | }, | |
539 | ||
540 | // Duplicate keys. This is the responsibility of no-dupe-class-members, but this rule still checks if there is the other accessor kind. | |
541 | { | |
542 | code: "class A { get a() {} get a() {} set a(foo) {} }", | |
543 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
544 | parserOptions: { ecmaVersion: 6 } | |
545 | }, | |
546 | { | |
547 | code: "class A { get [a]() {} set [a](foo) {} set [a](foo) {} }", | |
548 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
549 | parserOptions: { ecmaVersion: 6 } | |
550 | }, | |
551 | { | |
552 | code: "class A { get a() {} set 'a'(foo) {} get [`a`]() {} }", | |
553 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
554 | parserOptions: { ecmaVersion: 6 } | |
555 | }, | |
556 | { | |
557 | code: "A = class { get a() {} set a(foo) {} a() {} }", | |
558 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
559 | parserOptions: { ecmaVersion: 6 } | |
560 | }, | |
561 | { | |
562 | code: "A = class { a() {} get a() {} set a(foo) {} }", | |
563 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
564 | parserOptions: { ecmaVersion: 6 } | |
565 | }, | |
566 | { | |
567 | code: "class A { static set a(foo) {} static set a(foo) {} static get a() {} }", | |
568 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
569 | parserOptions: { ecmaVersion: 6 } | |
570 | }, | |
571 | { | |
572 | code: "class A { static get a() {} static set a(foo) {} static get a() {} }", | |
573 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
574 | parserOptions: { ecmaVersion: 6 } | |
575 | }, | |
576 | { | |
577 | code: "class A { static set a(foo) {} static get a() {} static a() {} }", | |
578 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
579 | parserOptions: { ecmaVersion: 6 } | |
580 | }, | |
581 | ||
582 | /* | |
583 | * This code should be invalid by this rule because it creates a class with the setter only, while the getter is ignored. | |
584 | * However, this edge case is not covered, it should be reported by no-dupe-class-members anyway. | |
585 | */ | |
586 | { | |
587 | code: "class A { get a() {} a() {} set a(foo) {} }", | |
588 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
589 | parserOptions: { ecmaVersion: 6 } | |
590 | }, | |
591 | { | |
592 | code: "class A { static set a(foo) {} static a() {} static get a() {} }", | |
593 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
594 | parserOptions: { ecmaVersion: 6 } | |
595 | } | |
596 | ], | |
597 | ||
598 | invalid: [ | |
599 | ||
600 | //------------------------------------------------------------------------------ | |
601 | // Object literals | |
602 | //------------------------------------------------------------------------------ | |
603 | ||
604 | // Test default settings | |
605 | { | |
606 | code: "var o = { set a(value) {} };", | |
607 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property" }] | |
608 | }, | |
609 | { | |
610 | code: "var o = { set a(value) {} };", | |
611 | options: [{}], | |
612 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property" }] | |
613 | }, | |
614 | ||
615 | // Test that the options do not affect each other | |
616 | { | |
617 | code: "var o = { set a(value) {} };", | |
618 | options: [{ setWithoutGet: true, getWithoutSet: false }], | |
619 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property" }] | |
620 | }, | |
621 | { | |
622 | code: "var o = { set a(value) {} };", | |
623 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
624 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property" }] | |
625 | }, | |
626 | { | |
627 | code: "var o = { get a() {} };", | |
628 | options: [{ setWithoutGet: false, getWithoutSet: true }], | |
629 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property" }] | |
630 | }, | |
631 | { | |
632 | code: "var o = { get a() {} };", | |
633 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
634 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property" }] | |
635 | }, | |
636 | { | |
637 | code: "var o = { get a() {} };", | |
638 | options: [{ getWithoutSet: true }], | |
639 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property" }] | |
640 | }, | |
641 | ||
642 | // Various kinds of the getter's key | |
643 | { | |
644 | code: "var o = { get abc() {} };", | |
645 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
646 | errors: [{ message: "Setter is not present for getter 'abc'.", type: "Property" }] | |
647 | }, | |
648 | { | |
649 | code: "var o = { get 'abc'() {} };", | |
650 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
651 | errors: [{ message: "Setter is not present for getter 'abc'.", type: "Property" }] | |
652 | }, | |
653 | { | |
654 | code: "var o = { get 123() {} };", | |
655 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
656 | errors: [{ message: "Setter is not present for getter '123'.", type: "Property" }] | |
657 | }, | |
658 | { | |
659 | code: "var o = { get 1e2() {} };", | |
660 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
661 | errors: [{ message: "Setter is not present for getter '100'.", type: "Property" }] | |
662 | }, | |
663 | { | |
664 | code: "var o = { get ['abc']() {} };", | |
665 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
666 | parserOptions: { ecmaVersion: 6 }, | |
667 | errors: [{ message: "Setter is not present for getter 'abc'.", type: "Property" }] | |
668 | }, | |
669 | { | |
670 | code: "var o = { get [`abc`]() {} };", | |
671 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
672 | parserOptions: { ecmaVersion: 6 }, | |
673 | errors: [{ message: "Setter is not present for getter 'abc'.", type: "Property" }] | |
674 | }, | |
675 | { | |
676 | code: "var o = { get [123]() {} };", | |
677 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
678 | parserOptions: { ecmaVersion: 6 }, | |
679 | errors: [{ message: "Setter is not present for getter '123'.", type: "Property" }] | |
680 | }, | |
681 | { | |
682 | code: "var o = { get [abc]() {} };", | |
683 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
684 | parserOptions: { ecmaVersion: 6 }, | |
685 | errors: [{ message: "Setter is not present for getter.", type: "Property" }] | |
686 | }, | |
687 | { | |
688 | code: "var o = { get [f(abc)]() {} };", | |
689 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
690 | parserOptions: { ecmaVersion: 6 }, | |
691 | errors: [{ message: "Setter is not present for getter.", type: "Property" }] | |
692 | }, | |
693 | { | |
694 | code: "var o = { get [a + b]() {} };", | |
695 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
696 | parserOptions: { ecmaVersion: 6 }, | |
697 | errors: [{ message: "Setter is not present for getter.", type: "Property" }] | |
698 | }, | |
699 | ||
700 | // Various kinds of the setter's key | |
701 | { | |
702 | code: "var o = { set abc(foo) {} };", | |
703 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
704 | errors: [{ message: "Getter is not present for setter 'abc'.", type: "Property" }] | |
705 | }, | |
706 | { | |
707 | code: "var o = { set 'abc'(foo) {} };", | |
708 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
709 | errors: [{ message: "Getter is not present for setter 'abc'.", type: "Property" }] | |
710 | }, | |
711 | { | |
712 | code: "var o = { set 123(foo) {} };", | |
713 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
714 | errors: [{ message: "Getter is not present for setter '123'.", type: "Property" }] | |
715 | }, | |
716 | { | |
717 | code: "var o = { set 1e2(foo) {} };", | |
718 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
719 | errors: [{ message: "Getter is not present for setter '100'.", type: "Property" }] | |
720 | }, | |
721 | { | |
722 | code: "var o = { set ['abc'](foo) {} };", | |
723 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
724 | parserOptions: { ecmaVersion: 6 }, | |
725 | errors: [{ message: "Getter is not present for setter 'abc'.", type: "Property" }] | |
726 | }, | |
727 | { | |
728 | code: "var o = { set [`abc`](foo) {} };", | |
729 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
730 | parserOptions: { ecmaVersion: 6 }, | |
731 | errors: [{ message: "Getter is not present for setter 'abc'.", type: "Property" }] | |
732 | }, | |
733 | { | |
734 | code: "var o = { set [123](foo) {} };", | |
735 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
736 | parserOptions: { ecmaVersion: 6 }, | |
737 | errors: [{ message: "Getter is not present for setter '123'.", type: "Property" }] | |
738 | }, | |
739 | { | |
740 | code: "var o = { set [abc](foo) {} };", | |
741 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
742 | parserOptions: { ecmaVersion: 6 }, | |
743 | errors: [{ message: "Getter is not present for setter.", type: "Property" }] | |
744 | }, | |
745 | { | |
746 | code: "var o = { set [f(abc)](foo) {} };", | |
747 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
748 | parserOptions: { ecmaVersion: 6 }, | |
749 | errors: [{ message: "Getter is not present for setter.", type: "Property" }] | |
750 | }, | |
751 | { | |
752 | code: "var o = { set [a + b](foo) {} };", | |
753 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
754 | parserOptions: { ecmaVersion: 6 }, | |
755 | errors: [{ message: "Getter is not present for setter.", type: "Property" }] | |
756 | }, | |
757 | ||
758 | // Different keys | |
759 | { | |
760 | code: "var o = { get a() {}, set b(foo) {} };", | |
761 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
762 | errors: [ | |
763 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
764 | { message: "Getter is not present for setter 'b'.", type: "Property", column: 23 } | |
765 | ] | |
766 | }, | |
767 | { | |
768 | code: "var o = { set a(foo) {}, get b() {} };", | |
769 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
770 | errors: [ | |
771 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }, | |
772 | { message: "Setter is not present for getter 'b'.", type: "Property", column: 26 } | |
773 | ] | |
774 | }, | |
775 | { | |
776 | code: "var o = { get 1() {}, set b(foo) {} };", | |
777 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
778 | errors: [ | |
779 | { message: "Setter is not present for getter '1'.", type: "Property", column: 11 }, | |
780 | { message: "Getter is not present for setter 'b'.", type: "Property", column: 23 } | |
781 | ] | |
782 | }, | |
783 | { | |
784 | code: "var o = { get a() {}, set 1(foo) {} };", | |
785 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
786 | errors: [ | |
787 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
788 | { message: "Getter is not present for setter '1'.", type: "Property", column: 23 } | |
789 | ] | |
790 | }, | |
791 | { | |
792 | code: "var o = { get a() {}, set 'a '(foo) {} };", | |
793 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
794 | errors: [ | |
795 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
796 | { message: "Getter is not present for setter 'a '.", type: "Property", column: 23 } | |
797 | ] | |
798 | }, | |
799 | { | |
800 | code: "var o = { get ' a'() {}, set 'a'(foo) {} };", | |
801 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
802 | errors: [ | |
803 | { message: "Setter is not present for getter ' a'.", type: "Property", column: 11 }, | |
804 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 26 } | |
805 | ] | |
806 | }, | |
807 | { | |
808 | code: "var o = { get ''() {}, set ' '(foo) {} };", | |
809 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
810 | errors: [ | |
811 | { message: "Setter is not present for getter ''.", type: "Property", column: 11 }, | |
812 | { message: "Getter is not present for setter ' '.", type: "Property", column: 24 } | |
813 | ] | |
814 | }, | |
815 | { | |
816 | code: "var o = { get ''() {}, set null(foo) {} };", | |
817 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
818 | errors: [ | |
819 | { message: "Setter is not present for getter ''.", type: "Property", column: 11 }, | |
820 | { message: "Getter is not present for setter 'null'.", type: "Property", column: 24 } | |
821 | ] | |
822 | }, | |
823 | { | |
824 | code: "var o = { get [`a`]() {}, set b(foo) {} };", | |
825 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
826 | parserOptions: { ecmaVersion: 6 }, | |
827 | errors: [ | |
828 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
829 | { message: "Getter is not present for setter 'b'.", type: "Property", column: 27 } | |
830 | ] | |
831 | }, | |
832 | { | |
833 | code: "var o = { get [a]() {}, set [b](foo) {} };", | |
834 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
835 | parserOptions: { ecmaVersion: 6 }, | |
836 | errors: [ | |
837 | { message: "Setter is not present for getter.", type: "Property", column: 11 }, | |
838 | { message: "Getter is not present for setter.", type: "Property", column: 25 } | |
839 | ] | |
840 | }, | |
841 | { | |
842 | code: "var o = { get [a]() {}, set a(foo) {} };", | |
843 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
844 | parserOptions: { ecmaVersion: 6 }, | |
845 | errors: [ | |
846 | { message: "Setter is not present for getter.", type: "Property", column: 11 }, | |
847 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 25 } | |
848 | ] | |
849 | }, | |
850 | { | |
851 | code: "var o = { get a() {}, set [a](foo) {} };", | |
852 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
853 | parserOptions: { ecmaVersion: 6 }, | |
854 | errors: [ | |
855 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
856 | { message: "Getter is not present for setter.", type: "Property", column: 23 } | |
857 | ] | |
858 | }, | |
859 | { | |
860 | code: "var o = { get [a + b]() {}, set [a - b](foo) {} };", | |
861 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
862 | parserOptions: { ecmaVersion: 6 }, | |
863 | errors: [ | |
864 | { message: "Setter is not present for getter.", type: "Property", column: 11 }, | |
865 | { message: "Getter is not present for setter.", type: "Property", column: 29 } | |
866 | ] | |
867 | }, | |
868 | { | |
869 | code: "var o = { get [`${0} `]() {}, set [`${0}`](foo) {} };", | |
870 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
871 | parserOptions: { ecmaVersion: 6 }, | |
872 | errors: [ | |
873 | { message: "Setter is not present for getter.", type: "Property", column: 11 }, | |
874 | { message: "Getter is not present for setter.", type: "Property", column: 31 } | |
875 | ] | |
876 | }, | |
877 | ||
878 | // Multiple invalid of same and different kinds | |
879 | { | |
880 | code: "var o = { get a() {}, get b() {} };", | |
881 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
882 | errors: [ | |
883 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
884 | { message: "Setter is not present for getter 'b'.", type: "Property", column: 23 } | |
885 | ] | |
886 | }, | |
887 | { | |
888 | code: "var o = { set a(foo) {}, set b(bar) {} };", | |
889 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
890 | errors: [ | |
891 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }, | |
892 | { message: "Getter is not present for setter 'b'.", type: "Property", column: 26 } | |
893 | ] | |
894 | }, | |
895 | { | |
896 | code: "var o = { get a() {}, set b(foo) {}, set c(foo) {}, get d() {} };", | |
897 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
898 | errors: [ | |
899 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
900 | { message: "Getter is not present for setter 'b'.", type: "Property", column: 23 }, | |
901 | { message: "Getter is not present for setter 'c'.", type: "Property", column: 38 }, | |
902 | { message: "Setter is not present for getter 'd'.", type: "Property", column: 53 } | |
903 | ] | |
904 | }, | |
905 | ||
906 | // Checks per object literal | |
907 | { | |
908 | code: "var o1 = { get a() {} }, o2 = { set a(foo) {} };", | |
909 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
910 | errors: [ | |
911 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 12 }, | |
912 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 33 } | |
913 | ] | |
914 | }, | |
915 | { | |
916 | code: "var o1 = { set a(foo) {} }, o2 = { get a() {} };", | |
917 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
918 | errors: [ | |
919 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 12 }, | |
920 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 36 } | |
921 | ] | |
922 | }, | |
923 | ||
924 | // Combinations or valid and invalid | |
925 | { | |
926 | code: "var o = { get a() {}, get b() {}, set b(foo) {} };", | |
927 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
928 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }] | |
929 | }, | |
930 | { | |
931 | code: "var o = { get b() {}, get a() {}, set b(foo) {} };", | |
932 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
933 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 23 }] | |
934 | }, | |
935 | { | |
936 | code: "var o = { get b() {}, set b(foo) {}, get a() {} };", | |
937 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
938 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 38 }] | |
939 | }, | |
940 | { | |
941 | code: "var o = { set a(foo) {}, get b() {}, set b(bar) {} };", | |
942 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
943 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }] | |
944 | }, | |
945 | { | |
946 | code: "var o = { get b() {}, set a(foo) {}, set b(bar) {} };", | |
947 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
948 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 23 }] | |
949 | }, | |
950 | { | |
951 | code: "var o = { get b() {}, set b(bar) {}, set a(foo) {} };", | |
952 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
953 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 38 }] | |
954 | }, | |
955 | { | |
956 | code: "var o = { get v1() {}, set i1(foo) {}, get v2() {}, set v2(bar) {}, get i2() {}, set v1(baz) {} };", | |
957 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
958 | errors: [ | |
959 | { message: "Getter is not present for setter 'i1'.", type: "Property", column: 24 }, | |
960 | { message: "Setter is not present for getter 'i2'.", type: "Property", column: 69 } | |
961 | ] | |
962 | }, | |
963 | ||
964 | // In the case of duplicates which don't have the other kind, all nodes are reported | |
965 | { | |
966 | code: "var o = { get a() {}, get a() {} };", | |
967 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
968 | parserOptions: { ecmaVersion: 6 }, | |
969 | errors: [ | |
970 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }, | |
971 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 23 } | |
972 | ] | |
973 | }, | |
974 | { | |
975 | code: "var o = { set a(foo) {}, set a(foo) {} };", | |
976 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
977 | parserOptions: { ecmaVersion: 6 }, | |
978 | errors: [ | |
979 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }, | |
980 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 26 } | |
981 | ] | |
982 | }, | |
983 | ||
984 | // Other elements or even value property duplicates in the same literal do not affect this rule | |
985 | { | |
986 | code: "var o = { a, get b() {}, c };", | |
987 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
988 | parserOptions: { ecmaVersion: 6 }, | |
989 | errors: [{ message: "Setter is not present for getter 'b'.", type: "Property", column: 14 }] | |
990 | }, | |
991 | { | |
992 | code: "var o = { a, get b() {}, c, set d(foo) {} };", | |
993 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
994 | parserOptions: { ecmaVersion: 6 }, | |
995 | errors: [ | |
996 | { message: "Setter is not present for getter 'b'.", type: "Property", column: 14 }, | |
997 | { message: "Getter is not present for setter 'd'.", type: "Property", column: 29 } | |
998 | ] | |
999 | }, | |
1000 | { | |
1001 | code: "var o = { get a() {}, a:1 };", | |
1002 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1003 | parserOptions: { ecmaVersion: 6 }, | |
1004 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }] | |
1005 | }, | |
1006 | { | |
1007 | code: "var o = { a, get a() {} };", | |
1008 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1009 | parserOptions: { ecmaVersion: 6 }, | |
1010 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 14 }] | |
1011 | }, | |
1012 | { | |
1013 | code: "var o = { set a(foo) {}, a:1 };", | |
1014 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1015 | parserOptions: { ecmaVersion: 6 }, | |
1016 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }] | |
1017 | }, | |
1018 | { | |
1019 | code: "var o = { a, set a(foo) {} };", | |
1020 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1021 | parserOptions: { ecmaVersion: 6 }, | |
1022 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 14 }] | |
1023 | }, | |
1024 | { | |
1025 | code: "var o = { get a() {}, ...b };", | |
1026 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1027 | parserOptions: { ecmaVersion: 2018 }, | |
1028 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }] | |
1029 | }, | |
1030 | { | |
1031 | code: "var o = { get a() {}, ...a };", | |
1032 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1033 | parserOptions: { ecmaVersion: 2018 }, | |
1034 | errors: [{ message: "Setter is not present for getter 'a'.", type: "Property", column: 11 }] | |
1035 | }, | |
1036 | { | |
1037 | code: "var o = { set a(foo) {}, ...a };", | |
1038 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1039 | parserOptions: { ecmaVersion: 2018 }, | |
1040 | errors: [{ message: "Getter is not present for setter 'a'.", type: "Property", column: 11 }] | |
1041 | }, | |
1042 | ||
1043 | // Full location tests | |
1044 | { | |
1045 | code: "var o = { get a() {} };", | |
1046 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1047 | errors: [{ | |
1048 | message: "Setter is not present for getter 'a'.", | |
1049 | type: "Property", | |
1050 | line: 1, | |
1051 | column: 11, | |
1052 | endLine: 1, | |
1053 | endColumn: 16 | |
1054 | }] | |
1055 | }, | |
1056 | { | |
1057 | code: "var o = {\n set [\n a](foo) {} };", | |
1058 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1059 | parserOptions: { ecmaVersion: 2015 }, | |
1060 | errors: [{ | |
1061 | message: "Getter is not present for setter.", | |
1062 | type: "Property", | |
1063 | line: 2, | |
1064 | column: 3, | |
1065 | endLine: 3, | |
1066 | endColumn: 4 | |
1067 | }] | |
1068 | }, | |
1069 | ||
1070 | //------------------------------------------------------------------------------ | |
1071 | // Property descriptors | |
1072 | //------------------------------------------------------------------------------ | |
1073 | ||
1074 | { | |
1075 | code: "var o = {d: 1};\n Object.defineProperty(o, 'c', \n{set: function(value) {\n val = value; \n} \n});", | |
1076 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1077 | }, | |
1078 | { | |
1079 | code: "Reflect.defineProperty(obj, 'foo', {set: function(value) {}});", | |
1080 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1081 | }, | |
1082 | { | |
1083 | code: "Object.defineProperties(obj, {foo: {set: function(value) {}}});", | |
1084 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1085 | }, | |
1086 | { | |
1087 | code: "Object.create(null, {foo: {set: function(value) {}}});", | |
1088 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1089 | }, | |
6f036462 TL |
1090 | { |
1091 | code: "var o = {d: 1};\n Object?.defineProperty(o, 'c', \n{set: function(value) {\n val = value; \n} \n});", | |
1092 | parserOptions: { ecmaVersion: 2020 }, | |
1093 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1094 | }, | |
1095 | { | |
1096 | code: "Reflect?.defineProperty(obj, 'foo', {set: function(value) {}});", | |
1097 | parserOptions: { ecmaVersion: 2020 }, | |
1098 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1099 | }, | |
1100 | { | |
1101 | code: "Object?.defineProperties(obj, {foo: {set: function(value) {}}});", | |
1102 | parserOptions: { ecmaVersion: 2020 }, | |
1103 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1104 | }, | |
1105 | { | |
1106 | code: "Object?.create(null, {foo: {set: function(value) {}}});", | |
1107 | parserOptions: { ecmaVersion: 2020 }, | |
1108 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1109 | }, | |
1110 | { | |
1111 | code: "var o = {d: 1};\n (Object?.defineProperty)(o, 'c', \n{set: function(value) {\n val = value; \n} \n});", | |
1112 | parserOptions: { ecmaVersion: 2020 }, | |
1113 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1114 | }, | |
1115 | { | |
1116 | code: "(Reflect?.defineProperty)(obj, 'foo', {set: function(value) {}});", | |
1117 | parserOptions: { ecmaVersion: 2020 }, | |
1118 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1119 | }, | |
1120 | { | |
1121 | code: "(Object?.defineProperties)(obj, {foo: {set: function(value) {}}});", | |
1122 | parserOptions: { ecmaVersion: 2020 }, | |
1123 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1124 | }, | |
1125 | { | |
1126 | code: "(Object?.create)(null, {foo: {set: function(value) {}}});", | |
1127 | parserOptions: { ecmaVersion: 2020 }, | |
1128 | errors: [{ message: "Getter is not present in property descriptor.", type: "ObjectExpression" }] | |
1129 | }, | |
eb39fafa DC |
1130 | |
1131 | //------------------------------------------------------------------------------ | |
1132 | // Classes | |
1133 | //------------------------------------------------------------------------------ | |
1134 | ||
1135 | // Test default settings | |
1136 | { | |
1137 | code: "class A { set a(foo) {} }", | |
1138 | parserOptions: { ecmaVersion: 6 }, | |
1139 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition" }] | |
1140 | }, | |
1141 | { | |
1142 | code: "class A { get a() {} set b(foo) {} }", | |
1143 | options: [{}], | |
1144 | parserOptions: { ecmaVersion: 6 }, | |
1145 | errors: [{ message: "Getter is not present for class setter 'b'.", type: "MethodDefinition" }] | |
1146 | }, | |
1147 | { | |
1148 | code: "class A { get a() {} }", | |
1149 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1150 | parserOptions: { ecmaVersion: 6 }, | |
1151 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition" }] | |
1152 | }, | |
1153 | { | |
1154 | code: "class A { set a(foo) {} }", | |
1155 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1156 | parserOptions: { ecmaVersion: 6 }, | |
1157 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition" }] | |
1158 | }, | |
1159 | { | |
1160 | code: "class A { static get a() {} }", | |
1161 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1162 | parserOptions: { ecmaVersion: 6 }, | |
1163 | errors: [{ message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition" }] | |
1164 | }, | |
1165 | { | |
1166 | code: "class A { static set a(foo) {} }", | |
1167 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1168 | parserOptions: { ecmaVersion: 6 }, | |
1169 | errors: [{ message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition" }] | |
1170 | }, | |
1171 | { | |
1172 | code: "A = class { get a() {} };", | |
1173 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1174 | parserOptions: { ecmaVersion: 6 }, | |
1175 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition" }] | |
1176 | }, | |
1177 | { | |
1178 | code: "A = class { get a() {} set b(foo) {} };", | |
1179 | options: [{ setWithoutGet: true, getWithoutSet: true }], | |
1180 | parserOptions: { ecmaVersion: 6 }, | |
1181 | errors: [ | |
1182 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition" }, | |
1183 | { message: "Getter is not present for class setter 'b'.", type: "MethodDefinition" } | |
1184 | ] | |
1185 | }, | |
1186 | { | |
1187 | code: "class A { set a(value) {} }", | |
1188 | options: [{ enforceForClassMembers: true }], | |
1189 | parserOptions: { ecmaVersion: 6 }, | |
1190 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition" }] | |
1191 | }, | |
1192 | { | |
1193 | code: "class A { static set a(value) {} }", | |
1194 | options: [{ enforceForClassMembers: true }], | |
1195 | parserOptions: { ecmaVersion: 6 }, | |
1196 | errors: [{ message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition" }] | |
1197 | }, | |
1198 | { | |
1199 | code: "A = class { set a(value) {} };", | |
1200 | options: [{ enforceForClassMembers: true }], | |
1201 | parserOptions: { ecmaVersion: 6 }, | |
1202 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition" }] | |
1203 | }, | |
1204 | { | |
1205 | code: "(class A { static set a(value) {} });", | |
1206 | options: [{ enforceForClassMembers: true }], | |
1207 | parserOptions: { ecmaVersion: 6 }, | |
1208 | errors: [{ message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition" }] | |
1209 | }, | |
1210 | ||
1211 | // Test that the accessor kind options do not affect each other | |
1212 | { | |
1213 | code: "class A { set a(value) {} }", | |
1214 | options: [{ setWithoutGet: true, getWithoutSet: false, enforceForClassMembers: true }], | |
1215 | parserOptions: { ecmaVersion: 6 }, | |
1216 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition" }] | |
1217 | }, | |
1218 | { | |
1219 | code: "A = class { static set a(value) {} };", | |
1220 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1221 | parserOptions: { ecmaVersion: 6 }, | |
1222 | errors: [{ message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition" }] | |
1223 | }, | |
1224 | { | |
1225 | code: "let foo = class A { get a() {} };", | |
1226 | options: [{ setWithoutGet: false, getWithoutSet: true, enforceForClassMembers: true }], | |
1227 | parserOptions: { ecmaVersion: 6 }, | |
1228 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition" }] | |
1229 | }, | |
1230 | { | |
1231 | code: "class A { static get a() {} };", | |
1232 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1233 | parserOptions: { ecmaVersion: 6 }, | |
1234 | errors: [{ message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition" }] | |
1235 | }, | |
1236 | { | |
1237 | code: "(class { get a() {} });", | |
1238 | options: [{ getWithoutSet: true, enforceForClassMembers: true }], | |
1239 | parserOptions: { ecmaVersion: 6 }, | |
1240 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition" }] | |
1241 | }, | |
1242 | ||
1243 | // Various kinds of keys | |
1244 | { | |
1245 | code: "class A { get abc() {} }", | |
1246 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1247 | parserOptions: { ecmaVersion: 6 }, | |
1248 | errors: [{ message: "Setter is not present for class getter 'abc'.", type: "MethodDefinition" }] | |
1249 | }, | |
1250 | { | |
1251 | code: "A = class { static set 'abc'(foo) {} };", | |
1252 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1253 | parserOptions: { ecmaVersion: 6 }, | |
1254 | errors: [{ message: "Getter is not present for class static setter 'abc'.", type: "MethodDefinition" }] | |
1255 | }, | |
1256 | { | |
1257 | code: "(class { get 123() {} });", | |
1258 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1259 | parserOptions: { ecmaVersion: 6 }, | |
1260 | errors: [{ message: "Setter is not present for class getter '123'.", type: "MethodDefinition" }] | |
1261 | }, | |
1262 | { | |
1263 | code: "class A { static get 1e2() {} }", | |
1264 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1265 | parserOptions: { ecmaVersion: 6 }, | |
1266 | errors: [{ message: "Setter is not present for class static getter '100'.", type: "MethodDefinition" }] | |
1267 | }, | |
1268 | { | |
1269 | code: "A = class { get ['abc']() {} };", | |
1270 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1271 | parserOptions: { ecmaVersion: 6 }, | |
1272 | errors: [{ message: "Setter is not present for class getter 'abc'.", type: "MethodDefinition" }] | |
1273 | }, | |
1274 | { | |
1275 | code: "class A { set [`abc`](foo) {} }", | |
1276 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1277 | parserOptions: { ecmaVersion: 6 }, | |
1278 | errors: [{ message: "Getter is not present for class setter 'abc'.", type: "MethodDefinition" }] | |
1279 | }, | |
1280 | { | |
1281 | code: "class A { static get [123]() {} }", | |
1282 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1283 | parserOptions: { ecmaVersion: 6 }, | |
1284 | errors: [{ message: "Setter is not present for class static getter '123'.", type: "MethodDefinition" }] | |
1285 | }, | |
1286 | { | |
1287 | code: "class A { get [abc]() {} }", | |
1288 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1289 | parserOptions: { ecmaVersion: 6 }, | |
1290 | errors: [{ message: "Setter is not present for class getter.", type: "MethodDefinition" }] | |
1291 | }, | |
1292 | { | |
1293 | code: "class A { static get [f(abc)]() {} }", | |
1294 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1295 | parserOptions: { ecmaVersion: 6 }, | |
1296 | errors: [{ message: "Setter is not present for class static getter.", type: "MethodDefinition" }] | |
1297 | }, | |
1298 | { | |
1299 | code: "A = class { set [a + b](foo) {} };", | |
1300 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1301 | parserOptions: { ecmaVersion: 6 }, | |
1302 | errors: [{ message: "Getter is not present for class setter.", type: "MethodDefinition" }] | |
1303 | }, | |
1304 | { | |
1305 | code: "class A { get ['constructor']() {} }", | |
1306 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1307 | parserOptions: { ecmaVersion: 6 }, | |
1308 | errors: [{ message: "Setter is not present for class getter 'constructor'.", type: "MethodDefinition" }] | |
1309 | }, | |
1310 | ||
1311 | // Different keys | |
1312 | { | |
1313 | code: "class A { get a() {} set b(foo) {} }", | |
1314 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1315 | parserOptions: { ecmaVersion: 6 }, | |
1316 | errors: [ | |
1317 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1318 | { message: "Getter is not present for class setter 'b'.", type: "MethodDefinition", column: 22 } | |
1319 | ] | |
1320 | }, | |
1321 | { | |
1322 | code: "A = class { set a(foo) {} get b() {} }", | |
1323 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1324 | parserOptions: { ecmaVersion: 6 }, | |
1325 | errors: [ | |
1326 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 13 }, | |
1327 | { message: "Setter is not present for class getter 'b'.", type: "MethodDefinition", column: 27 } | |
1328 | ] | |
1329 | }, | |
1330 | { | |
1331 | code: "A = class { static get a() {} static set b(foo) {} }", | |
1332 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1333 | parserOptions: { ecmaVersion: 6 }, | |
1334 | errors: [ | |
1335 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 13 }, | |
1336 | { message: "Getter is not present for class static setter 'b'.", type: "MethodDefinition", column: 31 } | |
1337 | ] | |
1338 | }, | |
1339 | { | |
1340 | code: "class A { get a() {} set b(foo) {} }", | |
1341 | options: [{ setWithoutGet: false, getWithoutSet: true, enforceForClassMembers: true }], | |
1342 | parserOptions: { ecmaVersion: 6 }, | |
1343 | errors: [ | |
1344 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 } | |
1345 | ] | |
1346 | }, | |
1347 | { | |
1348 | code: "class A { get a() {} set b(foo) {} }", | |
1349 | options: [{ setWithoutGet: true, getWithoutSet: false, enforceForClassMembers: true }], | |
1350 | parserOptions: { ecmaVersion: 6 }, | |
1351 | errors: [ | |
1352 | { message: "Getter is not present for class setter 'b'.", type: "MethodDefinition", column: 22 } | |
1353 | ] | |
1354 | }, | |
1355 | { | |
1356 | code: "class A { get 'a '() {} set 'a'(foo) {} }", | |
1357 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1358 | parserOptions: { ecmaVersion: 6 }, | |
1359 | errors: [ | |
1360 | { message: "Setter is not present for class getter 'a '.", type: "MethodDefinition", column: 11 }, | |
1361 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 25 } | |
1362 | ] | |
1363 | }, | |
1364 | { | |
1365 | code: "class A { get 'a'() {} set 1(foo) {} }", | |
1366 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1367 | parserOptions: { ecmaVersion: 6 }, | |
1368 | errors: [ | |
1369 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1370 | { message: "Getter is not present for class setter '1'.", type: "MethodDefinition", column: 24 } | |
1371 | ] | |
1372 | }, | |
1373 | { | |
1374 | code: "class A { get 1() {} set 2(foo) {} }", | |
1375 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1376 | parserOptions: { ecmaVersion: 6 }, | |
1377 | errors: [ | |
1378 | { message: "Setter is not present for class getter '1'.", type: "MethodDefinition", column: 11 }, | |
1379 | { message: "Getter is not present for class setter '2'.", type: "MethodDefinition", column: 22 } | |
1380 | ] | |
1381 | }, | |
1382 | { | |
1383 | code: "class A { get ''() {} set null(foo) {} }", | |
1384 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1385 | parserOptions: { ecmaVersion: 6 }, | |
1386 | errors: [ | |
1387 | { message: "Setter is not present for class getter ''.", type: "MethodDefinition", column: 11 }, | |
1388 | { message: "Getter is not present for class setter 'null'.", type: "MethodDefinition", column: 23 } | |
1389 | ] | |
1390 | }, | |
1391 | { | |
1392 | code: "class A { get a() {} set [a](foo) {} }", | |
1393 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1394 | parserOptions: { ecmaVersion: 6 }, | |
1395 | errors: [ | |
1396 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1397 | { message: "Getter is not present for class setter.", type: "MethodDefinition", column: 22 } | |
1398 | ] | |
1399 | }, | |
1400 | { | |
1401 | code: "class A { get [a]() {} set [b](foo) {} }", | |
1402 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1403 | parserOptions: { ecmaVersion: 6 }, | |
1404 | errors: [ | |
1405 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 11 }, | |
1406 | { message: "Getter is not present for class setter.", type: "MethodDefinition", column: 24 } | |
1407 | ] | |
1408 | }, | |
1409 | { | |
1410 | code: "class A { get [a]() {} set [a++](foo) {} }", | |
1411 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1412 | parserOptions: { ecmaVersion: 6 }, | |
1413 | errors: [ | |
1414 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 11 }, | |
1415 | { message: "Getter is not present for class setter.", type: "MethodDefinition", column: 24 } | |
1416 | ] | |
1417 | }, | |
1418 | { | |
1419 | code: "class A { get [a + b]() {} set [a - b](foo) {} }", | |
1420 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1421 | parserOptions: { ecmaVersion: 6 }, | |
1422 | errors: [ | |
1423 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 11 }, | |
1424 | { message: "Getter is not present for class setter.", type: "MethodDefinition", column: 28 } | |
1425 | ] | |
1426 | }, | |
1427 | ||
1428 | // Prototype and static accessors with same keys | |
1429 | { | |
1430 | code: "class A { get a() {} static set a(foo) {} }", | |
1431 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1432 | parserOptions: { ecmaVersion: 6 }, | |
1433 | errors: [ | |
1434 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1435 | { message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition", column: 22 } | |
1436 | ] | |
1437 | }, | |
1438 | { | |
1439 | code: "A = class { static get a() {} set a(foo) {} };", | |
1440 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1441 | parserOptions: { ecmaVersion: 6 }, | |
1442 | errors: [ | |
1443 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 13 }, | |
1444 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 31 } | |
1445 | ] | |
1446 | }, | |
1447 | { | |
1448 | code: "class A { set [a](foo) {} static get [a]() {} }", | |
1449 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1450 | parserOptions: { ecmaVersion: 6 }, | |
1451 | errors: [ | |
1452 | { message: "Getter is not present for class setter.", type: "MethodDefinition", column: 11 }, | |
1453 | { message: "Setter is not present for class static getter.", type: "MethodDefinition", column: 27 } | |
1454 | ] | |
1455 | }, | |
1456 | { | |
1457 | code: "class A { static set [a](foo) {} get [a]() {} }", | |
1458 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1459 | parserOptions: { ecmaVersion: 6 }, | |
1460 | errors: [ | |
1461 | { message: "Getter is not present for class static setter.", type: "MethodDefinition", column: 11 }, | |
1462 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 34 } | |
1463 | ] | |
1464 | }, | |
1465 | ||
1466 | // Multiple invalid of same and different kinds | |
1467 | { | |
1468 | code: "class A { get a() {} get b() {} }", | |
1469 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1470 | parserOptions: { ecmaVersion: 6 }, | |
1471 | errors: [ | |
1472 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1473 | { message: "Setter is not present for class getter 'b'.", type: "MethodDefinition", column: 22 } | |
1474 | ] | |
1475 | }, | |
1476 | { | |
1477 | code: "A = class { get a() {} get [b]() {} }", | |
1478 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1479 | parserOptions: { ecmaVersion: 6 }, | |
1480 | errors: [ | |
1481 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 13 }, | |
1482 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 24 } | |
1483 | ] | |
1484 | }, | |
1485 | { | |
1486 | code: "class A { get [a]() {} get [b]() {} }", | |
1487 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1488 | parserOptions: { ecmaVersion: 6 }, | |
1489 | errors: [ | |
1490 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 11 }, | |
1491 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 24 } | |
1492 | ] | |
1493 | }, | |
1494 | { | |
1495 | code: "A = class { set a(foo) {} set b(bar) {} };", | |
1496 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1497 | parserOptions: { ecmaVersion: 6 }, | |
1498 | errors: [ | |
1499 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 13 }, | |
1500 | { message: "Getter is not present for class setter 'b'.", type: "MethodDefinition", column: 27 } | |
1501 | ] | |
1502 | }, | |
1503 | { | |
1504 | code: "class A { static get a() {} static get b() {} }", | |
1505 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1506 | parserOptions: { ecmaVersion: 6 }, | |
1507 | errors: [ | |
1508 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1509 | { message: "Setter is not present for class static getter 'b'.", type: "MethodDefinition", column: 29 } | |
1510 | ] | |
1511 | }, | |
1512 | { | |
1513 | code: "A = class { static set a(foo) {} static set b(bar) {} }", | |
1514 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1515 | parserOptions: { ecmaVersion: 6 }, | |
1516 | errors: [ | |
1517 | { message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition", column: 13 }, | |
1518 | { message: "Getter is not present for class static setter 'b'.", type: "MethodDefinition", column: 34 } | |
1519 | ] | |
1520 | }, | |
1521 | { | |
1522 | code: "class A { static get a() {} set b(foo) {} static set c(bar) {} get d() {} }", | |
1523 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1524 | parserOptions: { ecmaVersion: 6 }, | |
1525 | errors: [ | |
1526 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1527 | { message: "Getter is not present for class setter 'b'.", type: "MethodDefinition", column: 29 }, | |
1528 | { message: "Getter is not present for class static setter 'c'.", type: "MethodDefinition", column: 43 }, | |
1529 | { message: "Setter is not present for class getter 'd'.", type: "MethodDefinition", column: 64 } | |
1530 | ] | |
1531 | }, | |
1532 | ||
1533 | // Checks per class | |
1534 | { | |
1535 | code: "class A { get a() {} } class B { set a(foo) {} }", | |
1536 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1537 | parserOptions: { ecmaVersion: 6 }, | |
1538 | errors: [ | |
1539 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1540 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 34 } | |
1541 | ] | |
1542 | }, | |
1543 | { | |
1544 | code: "A = class { set a(foo) {} }, class { get a() {} };", | |
1545 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1546 | parserOptions: { ecmaVersion: 6 }, | |
1547 | errors: [ | |
1548 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 13 }, | |
1549 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 38 } | |
1550 | ] | |
1551 | }, | |
1552 | { | |
1553 | code: "A = class { get a() {} }, { set a(foo) {} }", | |
1554 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1555 | parserOptions: { ecmaVersion: 6 }, | |
1556 | errors: [ | |
1557 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 13 }, | |
1558 | { message: "Getter is not present for setter 'a'.", type: "Property", column: 29 } | |
1559 | ] | |
1560 | }, | |
1561 | { | |
1562 | code: "A = { get a() {} }, class { set a(foo) {} }", | |
1563 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1564 | parserOptions: { ecmaVersion: 6 }, | |
1565 | errors: [ | |
1566 | { message: "Setter is not present for getter 'a'.", type: "Property", column: 7 }, | |
1567 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 29 } | |
1568 | ] | |
1569 | }, | |
1570 | ||
1571 | // Combinations or valid and invalid | |
1572 | { | |
1573 | code: "class A { get a() {} get b() {} set b(foo) {} }", | |
1574 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1575 | parserOptions: { ecmaVersion: 6 }, | |
1576 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }] | |
1577 | }, | |
1578 | { | |
1579 | code: "A = class { get b() {} get a() {} set b(foo) {} };", | |
1580 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1581 | parserOptions: { ecmaVersion: 6 }, | |
1582 | errors: [{ message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 24 }] | |
1583 | }, | |
1584 | { | |
1585 | code: "class A { set b(foo) {} get b() {} set a(bar) {} }", | |
1586 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1587 | parserOptions: { ecmaVersion: 6 }, | |
1588 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 36 }] | |
1589 | }, | |
1590 | { | |
1591 | code: "A = class { static get b() {} set a(foo) {} static set b(bar) {} };", | |
1592 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1593 | parserOptions: { ecmaVersion: 6 }, | |
1594 | errors: [{ message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 31 }] | |
1595 | }, | |
1596 | { | |
1597 | code: "class A { static set a(foo) {} get b() {} set b(bar) {} }", | |
1598 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1599 | parserOptions: { ecmaVersion: 6 }, | |
1600 | errors: [{ message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition", column: 11 }] | |
1601 | }, | |
1602 | { | |
1603 | code: "class A { get b() {} static get a() {} set b(bar) {} }", | |
1604 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1605 | parserOptions: { ecmaVersion: 6 }, | |
1606 | errors: [{ message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 22 }] | |
1607 | }, | |
1608 | { | |
1609 | code: "class A { static set b(foo) {} static get a() {} static get b() {} }", | |
1610 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1611 | parserOptions: { ecmaVersion: 6 }, | |
1612 | errors: [{ message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 32 }] | |
1613 | }, | |
1614 | { | |
1615 | code: "class A { get [v1](){} static set i1(foo){} static set v2(bar){} get [i2](){} static get i3(){} set [v1](baz){} static get v2(){} set i4(quux){} }", | |
1616 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1617 | parserOptions: { ecmaVersion: 6 }, | |
1618 | errors: [ | |
1619 | { message: "Getter is not present for class static setter 'i1'.", type: "MethodDefinition", column: 24 }, | |
1620 | { message: "Setter is not present for class getter.", type: "MethodDefinition", column: 66 }, | |
1621 | { message: "Setter is not present for class static getter 'i3'.", type: "MethodDefinition", column: 79 }, | |
1622 | { message: "Getter is not present for class setter 'i4'.", type: "MethodDefinition", column: 131 } | |
1623 | ] | |
1624 | }, | |
1625 | ||
1626 | // In the case of duplicates which don't have the other kind, all nodes are reported | |
1627 | { | |
1628 | code: "class A { get a() {} get a() {} }", | |
1629 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1630 | parserOptions: { ecmaVersion: 6 }, | |
1631 | errors: [ | |
1632 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 11 }, | |
1633 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 22 } | |
1634 | ] | |
1635 | }, | |
1636 | { | |
1637 | code: "A = class { set a(foo) {} set a(foo) {} };", | |
1638 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1639 | parserOptions: { ecmaVersion: 6 }, | |
1640 | errors: [ | |
1641 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 13 }, | |
1642 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 27 } | |
1643 | ] | |
1644 | }, | |
1645 | { | |
1646 | code: "A = class { static get a() {} static get a() {} };", | |
1647 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1648 | parserOptions: { ecmaVersion: 6 }, | |
1649 | errors: [ | |
1650 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 13 }, | |
1651 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 31 } | |
1652 | ] | |
1653 | }, | |
1654 | { | |
1655 | code: "class A { set a(foo) {} set a(foo) {} }", | |
1656 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1657 | parserOptions: { ecmaVersion: 6 }, | |
1658 | errors: [ | |
1659 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 11 }, | |
1660 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 25 } | |
1661 | ] | |
1662 | }, | |
1663 | ||
1664 | // Other elements or even method duplicates in the same class do not affect this rule | |
1665 | { | |
1666 | code: "class A { a() {} get b() {} c() {} }", | |
1667 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1668 | parserOptions: { ecmaVersion: 6 }, | |
1669 | errors: [ | |
1670 | { message: "Setter is not present for class getter 'b'.", type: "MethodDefinition", column: 18 } | |
1671 | ] | |
1672 | }, | |
1673 | { | |
1674 | code: "A = class { a() {} get b() {} c() {} set d(foo) {} };", | |
1675 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1676 | parserOptions: { ecmaVersion: 6 }, | |
1677 | errors: [ | |
1678 | { message: "Setter is not present for class getter 'b'.", type: "MethodDefinition", column: 20 }, | |
1679 | { message: "Getter is not present for class setter 'd'.", type: "MethodDefinition", column: 38 } | |
1680 | ] | |
1681 | }, | |
1682 | { | |
1683 | code: "class A { static a() {} get b() {} static c() {} }", | |
1684 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1685 | parserOptions: { ecmaVersion: 6 }, | |
1686 | errors: [ | |
1687 | { message: "Setter is not present for class getter 'b'.", type: "MethodDefinition", column: 25 } | |
1688 | ] | |
1689 | }, | |
1690 | { | |
1691 | code: "class A { a() {} get a() {} }", | |
1692 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1693 | parserOptions: { ecmaVersion: 6 }, | |
1694 | errors: [ | |
1695 | { message: "Setter is not present for class getter 'a'.", type: "MethodDefinition", column: 18 } | |
1696 | ] | |
1697 | }, | |
1698 | { | |
1699 | code: "A = class { static a() {} set a(foo) {} };", | |
1700 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1701 | parserOptions: { ecmaVersion: 6 }, | |
1702 | errors: [ | |
1703 | { message: "Getter is not present for class setter 'a'.", type: "MethodDefinition", column: 27 } | |
1704 | ] | |
1705 | }, | |
1706 | { | |
1707 | code: "class A { a() {} static get b() {} c() {} }", | |
1708 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1709 | parserOptions: { ecmaVersion: 6 }, | |
1710 | errors: [ | |
1711 | { message: "Setter is not present for class static getter 'b'.", type: "MethodDefinition", column: 18 } | |
1712 | ] | |
1713 | }, | |
1714 | { | |
1715 | code: "A = class { static a() {} static set b(foo) {} static c() {} d() {} };", | |
1716 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1717 | parserOptions: { ecmaVersion: 6 }, | |
1718 | errors: [ | |
1719 | { message: "Getter is not present for class static setter 'b'.", type: "MethodDefinition", column: 27 } | |
1720 | ] | |
1721 | }, | |
1722 | { | |
1723 | code: "class A { a() {} static get a() {} a() {} }", | |
1724 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1725 | parserOptions: { ecmaVersion: 6 }, | |
1726 | errors: [ | |
1727 | { message: "Setter is not present for class static getter 'a'.", type: "MethodDefinition", column: 18 } | |
1728 | ] | |
1729 | }, | |
1730 | { | |
1731 | code: "class A { static set a(foo) {} static a() {} }", | |
1732 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1733 | parserOptions: { ecmaVersion: 6 }, | |
1734 | errors: [ | |
1735 | { message: "Getter is not present for class static setter 'a'.", type: "MethodDefinition", column: 11 } | |
1736 | ] | |
1737 | }, | |
1738 | ||
1739 | // Full location tests | |
1740 | { | |
1741 | code: "class A { get a() {} };", | |
1742 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1743 | parserOptions: { ecmaVersion: 6 }, | |
1744 | errors: [{ | |
1745 | message: "Setter is not present for class getter 'a'.", | |
1746 | type: "MethodDefinition", | |
1747 | line: 1, | |
1748 | column: 11, | |
1749 | endLine: 1, | |
1750 | endColumn: 16 | |
1751 | }] | |
1752 | }, | |
1753 | { | |
1754 | code: "A = class {\n set [\n a](foo) {} };", | |
1755 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1756 | parserOptions: { ecmaVersion: 6 }, | |
1757 | errors: [{ | |
1758 | message: "Getter is not present for class setter.", | |
1759 | type: "MethodDefinition", | |
1760 | line: 2, | |
1761 | column: 3, | |
1762 | endLine: 3, | |
1763 | endColumn: 4 | |
1764 | }] | |
1765 | }, | |
1766 | { | |
1767 | code: "class A { static get a() {} };", | |
1768 | options: [{ setWithoutGet: true, getWithoutSet: true, enforceForClassMembers: true }], | |
1769 | parserOptions: { ecmaVersion: 6 }, | |
1770 | errors: [{ | |
1771 | message: "Setter is not present for class static getter 'a'.", | |
1772 | type: "MethodDefinition", | |
1773 | line: 1, | |
1774 | column: 11, | |
1775 | endLine: 1, | |
1776 | endColumn: 23 | |
1777 | }] | |
1778 | } | |
1779 | ] | |
1780 | }); |