]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for id-length rule. | |
3 | * @author Burak Yigit Kaya | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/id-length"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | const tooShortError = { messageId: "tooShort", type: "Identifier" }; | |
609c276f | 21 | const tooShortErrorPrivate = { messageId: "tooShortPrivate", type: "PrivateIdentifier" }; |
eb39fafa | 22 | const tooLongError = { messageId: "tooLong", type: "Identifier" }; |
609c276f | 23 | const tooLongErrorPrivate = { messageId: "tooLongPrivate", type: "PrivateIdentifier" }; |
eb39fafa DC |
24 | |
25 | ruleTester.run("id-length", rule, { | |
26 | valid: [ | |
27 | "var xyz;", | |
28 | "var xy = 1;", | |
29 | "function xyz() {};", | |
30 | "function xyz(abc, de) {};", | |
31 | "var obj = { abc: 1, de: 2 };", | |
32 | "var obj = { 'a': 1, bc: 2 };", | |
33 | "var obj = {}; obj['a'] = 2;", | |
34 | "abc = d;", | |
35 | "try { blah(); } catch (err) { /* pass */ }", | |
36 | "var handler = function ($e) {};", | |
37 | "var _a = 2", | |
38 | "var _ad$$ = new $;", | |
39 | "var xyz = new ΣΣ();", | |
40 | "unrelatedExpressionThatNeedsToBeIgnored();", | |
41 | "var obj = { 'a': 1, bc: 2 }; obj.tk = obj.a;", | |
42 | "var query = location.query.q || '';", | |
43 | "var query = location.query.q ? location.query.q : ''", | |
44 | { code: "let {a: foo} = bar;", parserOptions: { ecmaVersion: 6 } }, | |
45 | { code: "let foo = { [a]: 1 };", parserOptions: { ecmaVersion: 6 } }, | |
46 | { code: "let foo = { [a + b]: 1 };", parserOptions: { ecmaVersion: 6 } }, | |
47 | { code: "var x = Foo(42)", options: [{ min: 1 }] }, | |
48 | { code: "var x = Foo(42)", options: [{ min: 0 }] }, | |
49 | { code: "foo.$x = Foo(42)", options: [{ min: 1 }] }, | |
50 | { code: "var lalala = Foo(42)", options: [{ max: 6 }] }, | |
51 | { code: "for (var q, h=0; h < 10; h++) { console.log(h); q++; }", options: [{ exceptions: ["h", "q"] }] }, | |
52 | { code: "(num) => { num * num };", parserOptions: { ecmaVersion: 6 } }, | |
53 | { code: "function foo(num = 0) { }", parserOptions: { ecmaVersion: 6 } }, | |
54 | { code: "class MyClass { }", parserOptions: { ecmaVersion: 6 } }, | |
55 | { code: "class Foo { method() {} }", parserOptions: { ecmaVersion: 6 } }, | |
56 | { code: "function foo(...args) { }", parserOptions: { ecmaVersion: 6 } }, | |
57 | { code: "var { prop } = {};", parserOptions: { ecmaVersion: 6 } }, | |
58 | { code: "var { [a]: prop } = {};", parserOptions: { ecmaVersion: 6 } }, | |
59 | { code: "var { a: foo } = {};", options: [{ min: 3 }], parserOptions: { ecmaVersion: 6 } }, | |
60 | { code: "var { prop: foo } = {};", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 } }, | |
61 | { code: "var { longName: foo } = {};", options: [{ min: 3, max: 5 }], parserOptions: { ecmaVersion: 6 } }, | |
62 | { code: "var { foo: a } = {};", options: [{ exceptions: ["a"] }], parserOptions: { ecmaVersion: 6 } }, | |
63 | { code: "var { a: { b: { c: longName } } } = {};", parserOptions: { ecmaVersion: 6 } }, | |
64 | { code: "({ a: obj.x.y.z } = {});", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
65 | { code: "import something from 'y';", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, | |
66 | { code: "export var num = 0;", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, | |
67 | { code: "({ prop: obj.x.y.something } = {});", parserOptions: { ecmaVersion: 6 } }, | |
68 | { code: "({ prop: obj.longName } = {});", parserOptions: { ecmaVersion: 6 } }, | |
69 | { code: "var obj = { a: 1, bc: 2 };", options: [{ properties: "never" }] }, | |
70 | { code: "var obj = { [a]: 2 };", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
71 | { code: "var obj = {}; obj.a = 1; obj.bc = 2;", options: [{ properties: "never" }] }, | |
72 | { code: "({ prop: obj.x } = {});", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
73 | { code: "var obj = { aaaaa: 1 };", options: [{ max: 4, properties: "never" }] }, | |
74 | { code: "var obj = {}; obj.aaaaa = 1;", options: [{ max: 4, properties: "never" }] }, | |
75 | { code: "({ a: obj.x.y.z } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
76 | { code: "({ prop: obj.xxxxx } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
77 | { code: "var arr = [i,j,f,b]", parserOptions: { ecmaVersion: 6 } }, | |
78 | { code: "function foo([arr]) {}", parserOptions: { ecmaVersion: 6 } }, | |
79 | { code: "var {x} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
80 | { code: "var {x, y: {z}} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, | |
81 | { code: "let foo = { [a]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } }, | |
6f036462 TL |
82 | { code: "let foo = { [a + b]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } }, |
83 | { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }] }, | |
84 | { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_", "send$"] }] }, | |
85 | { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_", "^A", "^Z"] }] }, | |
86 | { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^A", "^BEFORE_", "^Z"] }] }, | |
609c276f TL |
87 | { code: "var x = 1 ;", options: [{ min: 3, max: 5, exceptionPatterns: ["[x-z]"] }] }, |
88 | ||
89 | // Class Fields | |
90 | { | |
91 | code: "class Foo { #xyz() {} }", | |
92 | parserOptions: { ecmaVersion: 2022 } | |
93 | }, | |
94 | { | |
95 | code: "class Foo { xyz = 1 }", | |
96 | parserOptions: { ecmaVersion: 2022 } | |
97 | }, | |
98 | { | |
99 | code: "class Foo { #xyz = 1 }", | |
100 | parserOptions: { ecmaVersion: 2022 } | |
101 | }, | |
102 | { | |
103 | code: "class Foo { #abc() {} }", | |
104 | options: [{ max: 3 }], | |
105 | parserOptions: { ecmaVersion: 2022 } | |
106 | }, | |
107 | { | |
108 | code: "class Foo { abc = 1 }", | |
109 | options: [{ max: 3 }], | |
110 | parserOptions: { ecmaVersion: 2022 } | |
111 | }, | |
112 | { | |
113 | code: "class Foo { #abc = 1 }", | |
114 | options: [{ max: 3 }], | |
115 | parserOptions: { ecmaVersion: 2022 } | |
116 | } | |
eb39fafa DC |
117 | ], |
118 | invalid: [ | |
119 | { code: "var x = 1;", errors: [tooShortError] }, | |
120 | { code: "var x;", errors: [tooShortError] }, | |
121 | { code: "obj.e = document.body;", errors: [tooShortError] }, | |
122 | { code: "function x() {};", errors: [tooShortError] }, | |
123 | { code: "function xyz(a) {};", errors: [tooShortError] }, | |
124 | { code: "var obj = { a: 1, bc: 2 };", errors: [tooShortError] }, | |
125 | { code: "try { blah(); } catch (e) { /* pass */ }", errors: [tooShortError] }, | |
126 | { code: "var handler = function (e) {};", errors: [tooShortError] }, | |
127 | { code: "for (var i=0; i < 10; i++) { console.log(i); }", errors: [tooShortError] }, | |
128 | { code: "var j=0; while (j > -10) { console.log(--j); }", errors: [tooShortError] }, | |
129 | { code: "var [i] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] }, | |
130 | { code: "var [,i,a] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError, tooShortError] }, | |
131 | { code: "function foo([a]) {}", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] }, | |
132 | { | |
133 | code: "var _$xt_$ = Foo(42)", | |
134 | options: [{ min: 2, max: 4 }], | |
135 | errors: [ | |
136 | tooLongError | |
137 | ] | |
138 | }, | |
139 | { | |
140 | code: "var _$x$_t$ = Foo(42)", | |
141 | options: [{ min: 2, max: 4 }], | |
142 | errors: [ | |
143 | tooLongError | |
144 | ] | |
145 | }, | |
6f036462 TL |
146 | { |
147 | code: "var toString;", | |
148 | options: [{ max: 5 }], | |
149 | errors: [ | |
150 | tooLongError | |
151 | ] | |
152 | }, | |
eb39fafa DC |
153 | { |
154 | code: "(a) => { a * a };", | |
155 | parserOptions: { ecmaVersion: 6 }, | |
156 | errors: [ | |
157 | tooShortError | |
158 | ] | |
159 | }, | |
160 | { | |
161 | code: "function foo(x = 0) { }", | |
162 | parserOptions: { ecmaVersion: 6 }, | |
163 | errors: [ | |
164 | tooShortError | |
165 | ] | |
166 | }, | |
167 | { | |
168 | code: "class x { }", | |
169 | parserOptions: { ecmaVersion: 6 }, | |
170 | errors: [ | |
171 | tooShortError | |
172 | ] | |
173 | }, | |
174 | { | |
175 | code: "class Foo { x() {} }", | |
176 | parserOptions: { ecmaVersion: 6 }, | |
177 | errors: [ | |
178 | tooShortError | |
179 | ] | |
180 | }, | |
181 | { | |
182 | code: "function foo(...x) { }", | |
183 | parserOptions: { ecmaVersion: 6 }, | |
184 | errors: [ | |
185 | tooShortError | |
186 | ] | |
187 | }, | |
188 | { | |
189 | code: "function foo({x}) { }", | |
190 | parserOptions: { ecmaVersion: 6 }, | |
191 | errors: [ | |
192 | tooShortError | |
193 | ] | |
194 | }, | |
195 | { | |
196 | code: "function foo({x: a}) { }", | |
197 | parserOptions: { ecmaVersion: 6 }, | |
198 | errors: [ | |
199 | { | |
200 | messageId: "tooShort", | |
201 | data: { name: "a", min: 2 }, | |
202 | type: "Identifier" | |
203 | } | |
204 | ] | |
205 | }, | |
206 | { | |
207 | code: "function foo({x: a, longName}) { }", | |
208 | parserOptions: { ecmaVersion: 6 }, | |
209 | errors: [ | |
210 | tooShortError | |
211 | ] | |
212 | }, | |
213 | { | |
214 | code: "function foo({ longName: a }) {}", | |
215 | options: [{ min: 3, max: 5 }], | |
216 | parserOptions: { ecmaVersion: 6 }, | |
217 | errors: [ | |
218 | tooShortError | |
219 | ] | |
220 | }, | |
221 | { | |
222 | code: "function foo({ prop: longName }) {};", | |
223 | options: [{ min: 3, max: 5 }], | |
224 | parserOptions: { ecmaVersion: 6 }, | |
225 | errors: [ | |
226 | tooLongError | |
227 | ] | |
228 | }, | |
229 | { | |
230 | code: "function foo({ a: b }) {};", | |
231 | options: [{ exceptions: ["a"] }], | |
232 | parserOptions: { ecmaVersion: 6 }, | |
233 | errors: [ | |
234 | { | |
235 | messageId: "tooShort", | |
236 | data: { name: "b", min: 2 }, | |
237 | line: 1, | |
238 | column: 19, | |
239 | type: "Identifier" | |
240 | } | |
241 | ] | |
242 | }, | |
6f036462 TL |
243 | { |
244 | code: "var hasOwnProperty;", | |
245 | options: [{ max: 10, exceptions: [] }], | |
246 | errors: [ | |
247 | { | |
248 | messageId: "tooLong", | |
249 | data: { name: "hasOwnProperty", max: 10 }, | |
250 | line: 1, | |
251 | column: 5, | |
252 | type: "Identifier" | |
253 | } | |
254 | ] | |
255 | }, | |
eb39fafa DC |
256 | { |
257 | code: "function foo({ a: { b: { c: d, e } } }) { }", | |
258 | parserOptions: { ecmaVersion: 6 }, | |
259 | errors: [ | |
260 | { | |
261 | messageId: "tooShort", | |
262 | data: { name: "d", min: 2 }, | |
263 | line: 1, | |
264 | column: 29, | |
265 | type: "Identifier" | |
266 | }, | |
267 | { | |
268 | messageId: "tooShort", | |
269 | data: { name: "e", min: 2 }, | |
270 | line: 1, | |
271 | column: 32, | |
272 | type: "Identifier" | |
273 | } | |
274 | ] | |
275 | }, | |
276 | { | |
277 | code: "var { x} = {};", | |
278 | parserOptions: { ecmaVersion: 6 }, | |
279 | errors: [ | |
280 | tooShortError | |
281 | ] | |
282 | }, | |
283 | { | |
284 | code: "var { x: a} = {};", | |
285 | parserOptions: { ecmaVersion: 6 }, | |
286 | errors: [ | |
287 | { | |
288 | messageId: "tooShort", | |
289 | data: { name: "a", min: 2 }, | |
290 | type: "Identifier" | |
291 | } | |
292 | ] | |
293 | }, | |
294 | { | |
295 | code: "var { a: a} = {};", | |
296 | parserOptions: { ecmaVersion: 6 }, | |
297 | errors: [ | |
298 | tooShortError | |
299 | ] | |
300 | }, | |
301 | { | |
302 | code: "var { prop: a } = {};", | |
303 | parserOptions: { ecmaVersion: 6 }, | |
304 | errors: [ | |
305 | tooShortError | |
306 | ] | |
307 | }, | |
308 | { | |
309 | code: "var { longName: a } = {};", | |
310 | options: [{ min: 3, max: 5 }], | |
311 | parserOptions: { ecmaVersion: 6 }, | |
312 | errors: [ | |
313 | tooShortError | |
314 | ] | |
315 | }, | |
316 | { | |
317 | code: "var { prop: [x] } = {};", | |
318 | parserOptions: { ecmaVersion: 6 }, | |
319 | errors: [ | |
320 | tooShortError | |
321 | ] | |
322 | }, | |
323 | { | |
324 | code: "var { prop: [[x]] } = {};", | |
325 | parserOptions: { ecmaVersion: 6 }, | |
326 | errors: [ | |
327 | tooShortError | |
328 | ] | |
329 | }, | |
330 | { | |
331 | code: "var { prop: longName } = {};", | |
332 | options: [{ min: 3, max: 5 }], | |
333 | parserOptions: { ecmaVersion: 6 }, | |
334 | errors: [ | |
335 | { | |
336 | messageId: "tooLong", | |
337 | data: { name: "longName", max: 5 }, | |
338 | line: 1, | |
339 | column: 13, | |
340 | type: "Identifier" | |
341 | } | |
342 | ] | |
343 | }, | |
344 | { | |
345 | code: "var { x: a} = {};", | |
346 | options: [{ exceptions: ["x"] }], | |
347 | parserOptions: { ecmaVersion: 6 }, | |
348 | errors: [ | |
349 | { | |
350 | messageId: "tooShort", | |
351 | data: { name: "a", min: 2 }, | |
352 | line: 1, | |
353 | column: 10, | |
354 | type: "Identifier" | |
355 | } | |
356 | ] | |
357 | }, | |
358 | { | |
359 | code: "var { a: { b: { c: d } } } = {};", | |
360 | parserOptions: { ecmaVersion: 6 }, | |
361 | errors: [ | |
362 | { | |
363 | messageId: "tooShort", | |
364 | data: { name: "d", min: 2 }, | |
365 | line: 1, | |
366 | column: 20, | |
367 | type: "Identifier" | |
368 | } | |
369 | ] | |
370 | }, | |
371 | { | |
372 | code: "var { a: { b: { c: d, e } } } = {};", | |
373 | parserOptions: { ecmaVersion: 6 }, | |
374 | errors: [ | |
375 | { | |
376 | messageId: "tooShort", | |
377 | data: { name: "d", min: 2 }, | |
378 | line: 1, | |
379 | column: 20, | |
380 | type: "Identifier" | |
381 | }, | |
382 | { | |
383 | messageId: "tooShort", | |
384 | data: { name: "e", min: 2 }, | |
385 | line: 1, | |
386 | column: 23, | |
387 | type: "Identifier" | |
388 | } | |
389 | ] | |
390 | }, | |
391 | { | |
392 | code: "var { a: { b: { c, e: longName } } } = {};", | |
393 | parserOptions: { ecmaVersion: 6 }, | |
394 | errors: [ | |
395 | { | |
396 | messageId: "tooShort", | |
397 | data: { name: "c", min: 2 }, | |
398 | line: 1, | |
399 | column: 17, | |
400 | type: "Identifier" | |
401 | } | |
402 | ] | |
403 | }, | |
404 | { | |
405 | code: "var { a: { b: { c: d, e: longName } } } = {};", | |
406 | parserOptions: { ecmaVersion: 6 }, | |
407 | errors: [ | |
408 | { | |
409 | messageId: "tooShort", | |
410 | data: { name: "d", min: 2 }, | |
411 | line: 1, | |
412 | column: 20, | |
413 | type: "Identifier" | |
414 | } | |
415 | ] | |
416 | }, | |
417 | { | |
418 | code: "var { a, b: { c: d, e: longName } } = {};", | |
419 | parserOptions: { ecmaVersion: 6 }, | |
420 | errors: [ | |
421 | { | |
422 | messageId: "tooShort", | |
423 | data: { name: "a", min: 2 }, | |
424 | line: 1, | |
425 | column: 7, | |
426 | type: "Identifier" | |
427 | }, | |
428 | { | |
429 | messageId: "tooShort", | |
430 | data: { name: "d", min: 2 }, | |
431 | line: 1, | |
432 | column: 18, | |
433 | type: "Identifier" | |
434 | } | |
435 | ] | |
436 | }, | |
437 | { | |
438 | code: "import x from 'y';", | |
439 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
440 | errors: [ | |
441 | tooShortError | |
442 | ] | |
443 | }, | |
444 | { | |
445 | code: "export var x = 0;", | |
446 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
447 | errors: [ | |
448 | tooShortError | |
449 | ] | |
450 | }, | |
451 | { | |
452 | code: "({ a: obj.x.y.z } = {});", | |
453 | parserOptions: { ecmaVersion: 6 }, | |
454 | errors: [ | |
455 | { | |
456 | messageId: "tooShort", | |
457 | data: { name: "z", min: 2 }, | |
458 | line: 1, | |
459 | column: 15, | |
460 | type: "Identifier" | |
461 | } | |
462 | ] | |
463 | }, | |
464 | { | |
465 | code: "({ prop: obj.x } = {});", | |
466 | parserOptions: { ecmaVersion: 6 }, | |
467 | errors: [ | |
468 | { | |
469 | messageId: "tooShort", | |
470 | data: { name: "x", min: 2 }, | |
471 | line: 1, | |
472 | column: 14, | |
473 | type: "Identifier" | |
474 | } | |
475 | ] | |
476 | }, | |
477 | { code: "var x = 1;", options: [{ properties: "never" }], errors: [tooShortError] }, | |
478 | { | |
479 | code: "var {prop: x} = foo;", | |
480 | options: [{ properties: "never" }], | |
481 | parserOptions: { ecmaVersion: 6 }, | |
482 | errors: [ | |
483 | { | |
484 | messageId: "tooShort", | |
485 | data: { name: "x", min: 2 }, | |
486 | line: 1, | |
487 | column: 12, | |
488 | type: "Identifier" | |
489 | } | |
490 | ] | |
491 | }, | |
492 | { | |
493 | code: "var foo = {x: prop};", | |
494 | options: [{ properties: "always" }], | |
495 | parserOptions: { ecmaVersion: 6 }, | |
496 | errors: [ | |
497 | tooShortError | |
498 | ] | |
6f036462 TL |
499 | }, |
500 | { | |
501 | code: "function BEFORE_send() {};", | |
502 | options: [{ min: 3, max: 5 }], | |
503 | errors: [ | |
504 | tooLongError | |
505 | ] | |
506 | }, | |
507 | { | |
508 | code: "function NOTMATCHED_send() {};", | |
509 | options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }], | |
510 | errors: [ | |
511 | tooLongError | |
512 | ] | |
513 | }, | |
514 | { | |
515 | code: "function N() {};", | |
516 | options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }], | |
517 | errors: [ | |
518 | tooShortError | |
519 | ] | |
609c276f TL |
520 | }, |
521 | ||
522 | // Class Fields | |
523 | { | |
524 | code: "class Foo { #x() {} }", | |
525 | parserOptions: { ecmaVersion: 2022 }, | |
526 | errors: [ | |
527 | tooShortErrorPrivate | |
528 | ] | |
529 | }, | |
530 | { | |
531 | code: "class Foo { x = 1 }", | |
532 | parserOptions: { ecmaVersion: 2022 }, | |
533 | errors: [ | |
534 | tooShortError | |
535 | ] | |
536 | }, | |
537 | { | |
538 | code: "class Foo { #x = 1 }", | |
539 | parserOptions: { ecmaVersion: 2022 }, | |
540 | errors: [ | |
541 | tooShortErrorPrivate | |
542 | ] | |
543 | }, | |
544 | { | |
545 | code: "class Foo { #abcdefg() {} }", | |
546 | options: [{ max: 3 }], | |
547 | parserOptions: { ecmaVersion: 2022 }, | |
548 | errors: [ | |
549 | tooLongErrorPrivate | |
550 | ] | |
551 | }, | |
552 | { | |
553 | code: "class Foo { abcdefg = 1 }", | |
554 | options: [{ max: 3 }], | |
555 | parserOptions: { ecmaVersion: 2022 }, | |
556 | errors: [ | |
557 | tooLongError | |
558 | ] | |
559 | }, | |
560 | { | |
561 | code: "class Foo { #abcdefg = 1 }", | |
562 | options: [{ max: 3 }], | |
563 | parserOptions: { ecmaVersion: 2022 }, | |
564 | errors: [ | |
565 | tooLongErrorPrivate | |
566 | ] | |
eb39fafa DC |
567 | } |
568 | ] | |
569 | }); |