]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/strict.js
92abffda16ccc9b41152b564ebb4b752de05e2a3
[pve-eslint.git] / eslint / tests / lib / rules / strict.js
1 /**
2 * @fileoverview Tests for strict rule.
3 * @author Nicholas C. Zakas
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/strict"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 const ruleTester = new RuleTester();
16
17 ruleTester.run("strict", rule, {
18 valid: [
19
20 // "never" mode
21 { code: "foo();", options: ["never"] },
22 { code: "function foo() { return; }", options: ["never"] },
23 { code: "var foo = function() { return; };", options: ["never"] },
24 { code: "foo(); 'use strict';", options: ["never"] },
25 { code: "function foo() { bar(); 'use strict'; return; }", options: ["never"] },
26 { code: "var foo = function() { { 'use strict'; } return; };", options: ["never"] },
27 { code: "(function() { bar('use strict'); return; }());", options: ["never"] },
28 { code: "var fn = x => 1;", options: ["never"], parserOptions: { ecmaVersion: 6 } },
29 { code: "var fn = x => { return; };", options: ["never"], parserOptions: { ecmaVersion: 6 } },
30 { code: "foo();", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
31 { code: "function foo() { return; }", options: ["never"], parserOptions: { ecmaFeatures: { impliedStrict: true } } },
32
33 // "global" mode
34 { code: "// Intentionally empty", options: ["global"] },
35 { code: "\"use strict\"; foo();", options: ["global"] },
36 { code: "foo();", options: ["global"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
37 { code: "function foo() { return; }", options: ["global"], parserOptions: { ecmaFeatures: { impliedStrict: true } } },
38 { code: "'use strict'; function foo() { return; }", options: ["global"] },
39 { code: "'use strict'; var foo = function() { return; };", options: ["global"] },
40 { code: "'use strict'; function foo() { bar(); 'use strict'; return; }", options: ["global"] },
41 { code: "'use strict'; var foo = function() { bar(); 'use strict'; return; };", options: ["global"] },
42 { code: "'use strict'; function foo() { return function() { bar(); 'use strict'; return; }; }", options: ["global"] },
43 { code: "'use strict'; var foo = () => { return () => { bar(); 'use strict'; return; }; }", options: ["global"], parserOptions: { ecmaVersion: 6 } },
44
45 // "function" mode
46 { code: "function foo() { 'use strict'; return; }", options: ["function"] },
47 { code: "function foo() { return; }", options: ["function"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
48 { code: "function foo() { return; }", options: ["function"], parserOptions: { ecmaFeatures: { impliedStrict: true } } },
49 { code: "var foo = function() { return; }", options: ["function"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
50 { code: "var foo = function() { 'use strict'; return; }", options: ["function"] },
51 { code: "function foo() { 'use strict'; return; } var bar = function() { 'use strict'; bar(); };", options: ["function"] },
52 { code: "var foo = function() { 'use strict'; function bar() { return; } bar(); };", options: ["function"] },
53 { code: "var foo = () => { 'use strict'; var bar = () => 1; bar(); };", options: ["function"], parserOptions: { ecmaVersion: 6 } },
54 { code: "var foo = () => { var bar = () => 1; bar(); };", options: ["function"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
55 {
56 code: "class A { constructor() { } }",
57 options: ["function"],
58 parserOptions: { ecmaVersion: 6 }
59 },
60 {
61 code: "class A { foo() { } }",
62 options: ["function"],
63 parserOptions: { ecmaVersion: 6 }
64 },
65 {
66 code: "class A { foo() { function bar() { } } }",
67 options: ["function"],
68 parserOptions: { ecmaVersion: 6 }
69 },
70 {
71 code: "(function() { 'use strict'; function foo(a = 0) { } }())",
72 options: ["function"],
73 parserOptions: { ecmaVersion: 6 }
74 },
75
76
77 // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
78 { code: "function foo() { 'use strict'; return; }", options: ["safe"] },
79 { code: "'use strict'; function foo() { return; }", options: ["safe"], parserOptions: { ecmaFeatures: { globalReturn: true } } },
80 { code: "function foo() { return; }", options: ["safe"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
81 { code: "function foo() { return; }", options: ["safe"], parserOptions: { ecmaFeatures: { impliedStrict: true } } },
82
83 // defaults to "safe" mode
84 "function foo() { 'use strict'; return; }",
85 { code: "'use strict'; function foo() { return; }", parserOptions: { ecmaFeatures: { globalReturn: true } } },
86 { code: "function foo() { return; }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
87 { code: "function foo() { return; }", parserOptions: { ecmaFeatures: { impliedStrict: true } } }
88
89 ],
90 invalid: [
91
92 // "never" mode
93 {
94 code: "\"use strict\"; foo();",
95 output: null,
96 options: ["never"],
97 errors: [
98 { messageId: "never", type: "ExpressionStatement" }
99 ]
100 }, {
101 code: "function foo() { 'use strict'; return; }",
102 output: null,
103 options: ["never"],
104 errors: [
105 { messageId: "never", type: "ExpressionStatement" }
106 ]
107 }, {
108 code: "var foo = function() { 'use strict'; return; };",
109 output: null,
110 options: ["never"],
111 errors: [
112 { messageId: "never", type: "ExpressionStatement" }
113 ]
114 }, {
115 code: "function foo() { return function() { 'use strict'; return; }; }",
116 output: null,
117 options: ["never"],
118 errors: [
119 { messageId: "never", type: "ExpressionStatement" }
120 ]
121 }, {
122 code: "'use strict'; function foo() { \"use strict\"; return; }",
123 output: null,
124 options: ["never"],
125 errors: [
126 { messageId: "never", type: "ExpressionStatement" },
127 { messageId: "never", type: "ExpressionStatement" }
128 ]
129 }, {
130 code: "\"use strict\"; foo();",
131 output: " foo();",
132 options: ["never"],
133 parserOptions: { ecmaVersion: 6, sourceType: "module" },
134 errors: [
135 { messageId: "module", type: "ExpressionStatement" }
136 ]
137 }, {
138 code: "'use strict'; function foo() { 'use strict'; return; }",
139 output: " function foo() { return; }",
140 options: ["never"],
141 parserOptions: { ecmaFeatures: { impliedStrict: true } },
142 errors: [
143 { messageId: "implied", type: "ExpressionStatement" },
144 { messageId: "implied", type: "ExpressionStatement" }
145 ]
146 }, {
147 code: "'use strict'; function foo() { 'use strict'; return; }",
148 output: " function foo() { return; }",
149 options: ["never"],
150 parserOptions: { ecmaVersion: 6, sourceType: "module", ecmaFeatures: { impliedStrict: true } },
151 errors: [
152 { messageId: "module", type: "ExpressionStatement" },
153 { messageId: "module", type: "ExpressionStatement" }
154 ]
155 },
156
157 // "global" mode
158 {
159 code: "foo();",
160 output: null,
161 options: ["global"],
162 errors: [
163 { messageId: "global", type: "Program" }
164 ]
165 }, {
166 code: "function foo() { 'use strict'; return; }",
167 output: null,
168 options: ["global"],
169 errors: [
170 { messageId: "global", type: "Program" },
171 { messageId: "global", type: "ExpressionStatement" }
172 ]
173 }, {
174 code: "var foo = function() { 'use strict'; return; }",
175 output: null,
176 options: ["global"],
177 errors: [
178 { messageId: "global", type: "Program" },
179 { messageId: "global", type: "ExpressionStatement" }
180 ]
181 }, {
182 code: "var foo = () => { 'use strict'; return () => 1; }",
183 output: null,
184 options: ["global"],
185 parserOptions: { ecmaVersion: 6 },
186 errors: [
187 { messageId: "global", type: "Program" },
188 { messageId: "global", type: "ExpressionStatement" }
189 ]
190 }, {
191 code: "'use strict'; function foo() { 'use strict'; return; }",
192 output: null,
193 options: ["global"],
194 errors: [
195 { messageId: "global", type: "ExpressionStatement" }
196 ]
197 }, {
198 code: "'use strict'; var foo = function() { 'use strict'; return; };",
199 output: null,
200 options: ["global"],
201 errors: [
202 { messageId: "global", type: "ExpressionStatement" }
203 ]
204 }, {
205 code: "'use strict'; 'use strict'; foo();",
206 output: "'use strict'; foo();",
207 options: ["global"],
208 errors: [
209 { messageId: "multiple", type: "ExpressionStatement" }
210 ]
211 }, {
212 code: "'use strict'; foo();",
213 output: " foo();",
214 options: ["global"],
215 parserOptions: { ecmaVersion: 6, sourceType: "module" },
216 errors: [
217 { messageId: "module", type: "ExpressionStatement" }
218 ]
219 }, {
220 code: "'use strict'; function foo() { 'use strict'; return; }",
221 output: " function foo() { return; }",
222 options: ["global"],
223 parserOptions: { ecmaFeatures: { impliedStrict: true } },
224 errors: [
225 { messageId: "implied", type: "ExpressionStatement" },
226 { messageId: "implied", type: "ExpressionStatement" }
227 ]
228 }, {
229 code: "'use strict'; function foo() { 'use strict'; return; }",
230 output: " function foo() { return; }",
231 options: ["global"],
232 parserOptions: { ecmaVersion: 6, sourceType: "module", ecmaFeatures: { impliedStrict: true } },
233 errors: [
234 { messageId: "module", type: "ExpressionStatement" },
235 { messageId: "module", type: "ExpressionStatement" }
236 ]
237 },
238
239 // "function" mode
240 {
241 code: "'use strict'; foo();",
242 output: null,
243 options: ["function"],
244 errors: [
245 { messageId: "function", type: "ExpressionStatement" }
246 ]
247 }, {
248 code: "'use strict'; (function() { 'use strict'; return true; }());",
249 output: null,
250 options: ["function"],
251 errors: [
252 { messageId: "function", type: "ExpressionStatement" }
253 ]
254 }, {
255 code: "(function() { 'use strict'; function f() { 'use strict'; return } return true; }());",
256 output: "(function() { 'use strict'; function f() { return } return true; }());",
257 options: ["function"],
258 errors: [
259 { messageId: "unnecessary", type: "ExpressionStatement" }
260 ]
261 }, {
262 code: "(function() { return true; }());",
263 output: null,
264 options: ["function"],
265 errors: [
266 { messageId: "function", type: "FunctionExpression" }
267 ]
268 }, {
269 code: "(() => { return true; })();",
270 output: null,
271 options: ["function"],
272 parserOptions: { ecmaVersion: 6 },
273 errors: [
274 { messageId: "function", type: "ArrowFunctionExpression" }
275 ]
276 }, {
277 code: "(() => true)();",
278 output: null,
279 options: ["function"],
280 parserOptions: { ecmaVersion: 6 },
281 errors: [
282 { messageId: "function", type: "ArrowFunctionExpression" }
283 ]
284 }, {
285 code: "var foo = function() { foo(); 'use strict'; return; }; function bar() { foo(); 'use strict'; }",
286 output: null,
287 options: ["function"],
288 errors: [
289 { messageId: "function", type: "FunctionExpression" },
290 { messageId: "function", type: "FunctionDeclaration" }
291 ]
292 }, {
293 code: "function foo() { 'use strict'; 'use strict'; return; }",
294 output: "function foo() { 'use strict'; return; }",
295 options: ["function"],
296 errors: [
297 { messageId: "multiple", type: "ExpressionStatement" }
298 ]
299 }, {
300 code: "var foo = function() { 'use strict'; 'use strict'; return; }",
301 output: "var foo = function() { 'use strict'; return; }",
302 options: ["function"],
303 errors: [
304 { messageId: "multiple", type: "ExpressionStatement" }
305 ]
306 }, {
307 code: "var foo = function() { 'use strict'; return; }",
308 output: "var foo = function() { return; }",
309 options: ["function"],
310 parserOptions: { ecmaVersion: 6, sourceType: "module" },
311 errors: [
312 { messageId: "module", type: "ExpressionStatement" }
313 ]
314 }, {
315 code: "'use strict'; function foo() { 'use strict'; return; }",
316 output: " function foo() { return; }",
317 options: ["function"],
318 parserOptions: { ecmaFeatures: { impliedStrict: true } },
319 errors: [
320 { messageId: "implied", type: "ExpressionStatement" },
321 { messageId: "implied", type: "ExpressionStatement" }
322 ]
323 }, {
324 code: "'use strict'; function foo() { 'use strict'; return; }",
325 output: " function foo() { return; }",
326 options: ["function"],
327 parserOptions: { ecmaVersion: 6, sourceType: "module", ecmaFeatures: { impliedStrict: true } },
328 errors: [
329 { messageId: "module", type: "ExpressionStatement" },
330 { messageId: "module", type: "ExpressionStatement" }
331 ]
332 }, {
333 code: "function foo() { return function() { 'use strict'; return; }; }",
334 output: null,
335 options: ["function"],
336 errors: [
337 { messageId: "function", type: "FunctionDeclaration" }
338 ]
339 }, {
340 code: "var foo = function() { function bar() { 'use strict'; return; } return; }",
341 output: null,
342 options: ["function"],
343 errors: [
344 { messageId: "function", type: "FunctionExpression" }
345 ]
346 }, {
347 code: "function foo() { 'use strict'; return; } var bar = function() { return; };",
348 output: null,
349 options: ["function"],
350 errors: [
351 { messageId: "function", type: "FunctionExpression" }
352 ]
353 }, {
354 code: "var foo = function() { 'use strict'; return; }; function bar() { return; };",
355 output: null,
356 options: ["function"],
357 errors: [
358 { messageId: "function", type: "FunctionDeclaration" }
359 ]
360 }, {
361 code: "function foo() { 'use strict'; return function() { 'use strict'; 'use strict'; return; }; }",
362 output: "function foo() { 'use strict'; return function() { return; }; }",
363 options: ["function"],
364 errors: [
365 { messageId: "unnecessary", type: "ExpressionStatement" },
366 { messageId: "multiple", type: "ExpressionStatement" }
367 ]
368 }, {
369 code: "var foo = function() { 'use strict'; function bar() { 'use strict'; 'use strict'; return; } }",
370 output: "var foo = function() { 'use strict'; function bar() { return; } }",
371 options: ["function"],
372 errors: [
373 { messageId: "unnecessary", type: "ExpressionStatement" },
374 { messageId: "multiple", type: "ExpressionStatement" }
375 ]
376 },
377 {
378 code: "var foo = () => { return; };",
379 output: null,
380 options: ["function"],
381 parserOptions: { ecmaVersion: 6 },
382 errors: [{ messageId: "function", type: "ArrowFunctionExpression" }]
383 },
384
385 // Classes
386 {
387 code: "class A { constructor() { \"use strict\"; } }",
388 output: "class A { constructor() { } }",
389 options: ["function"],
390 parserOptions: { ecmaVersion: 6 },
391 errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
392 },
393 {
394 code: "class A { foo() { \"use strict\"; } }",
395 output: "class A { foo() { } }",
396 options: ["function"],
397 parserOptions: { ecmaVersion: 6 },
398 errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
399 },
400 {
401 code: "class A { foo() { function bar() { \"use strict\"; } } }",
402 output: "class A { foo() { function bar() { } } }",
403 options: ["function"],
404 parserOptions: { ecmaVersion: 6 },
405 errors: [{ messageId: "unnecessaryInClasses", type: "ExpressionStatement" }]
406 },
407
408
409 // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
410 {
411 code: "'use strict'; function foo() { return; }",
412 output: null,
413 options: ["safe"],
414 errors: [
415 { messageId: "function", type: "ExpressionStatement" },
416 { messageId: "function", type: "FunctionDeclaration" }
417 ]
418 },
419 {
420 code: "function foo() { 'use strict'; return; }",
421 output: null,
422 options: ["safe"],
423 parserOptions: { ecmaFeatures: { globalReturn: true } },
424 errors: [
425 { messageId: "global", type: "Program" },
426 { messageId: "global", type: "ExpressionStatement" }
427 ]
428 },
429 {
430 code: "'use strict'; function foo() { 'use strict'; return; }",
431 output: " function foo() { return; }",
432 options: ["safe"],
433 parserOptions: { ecmaFeatures: { impliedStrict: true } },
434 errors: [
435 { messageId: "implied", type: "ExpressionStatement" },
436 { messageId: "implied", type: "ExpressionStatement" }
437 ]
438 },
439 {
440 code: "'use strict'; function foo() { 'use strict'; return; }",
441 output: " function foo() { return; }",
442 options: ["safe"],
443 parserOptions: { ecmaVersion: 6, sourceType: "module", ecmaFeatures: { impliedStrict: true } },
444 errors: [
445 { messageId: "module", type: "ExpressionStatement" },
446 { messageId: "module", type: "ExpressionStatement" }
447 ]
448 },
449
450 // Default to "safe" mode
451 {
452 code: "'use strict'; function foo() { return; }",
453 output: null,
454 errors: [
455 { messageId: "function", type: "ExpressionStatement" },
456 { messageId: "function", type: "FunctionDeclaration" }
457 ]
458 },
459 {
460 code: "function foo() { return; }",
461 output: null,
462 errors: [{ messageId: "function", type: "FunctionDeclaration" }]
463 },
464 {
465 code: "function foo() { 'use strict'; return; }",
466 output: null,
467 parserOptions: { ecmaFeatures: { globalReturn: true } },
468 errors: [
469 { messageId: "global", type: "Program" },
470 { messageId: "global", type: "ExpressionStatement" }
471 ]
472 },
473 {
474 code: "'use strict'; function foo() { 'use strict'; return; }",
475 output: " function foo() { return; }",
476 parserOptions: { ecmaFeatures: { impliedStrict: true } },
477 errors: [
478 { messageId: "implied", type: "ExpressionStatement" },
479 { messageId: "implied", type: "ExpressionStatement" }
480 ]
481 },
482 {
483 code: "'use strict'; function foo() { 'use strict'; return; }",
484 output: " function foo() { return; }",
485 parserOptions: { ecmaVersion: 6, sourceType: "module", ecmaFeatures: { impliedStrict: true } },
486 errors: [
487 { messageId: "module", type: "ExpressionStatement" },
488 { messageId: "module", type: "ExpressionStatement" }
489 ]
490 },
491
492 // Reports deprecated syntax: https://github.com/eslint/eslint/issues/6405
493 {
494 code: "function foo(a = 0) { 'use strict' }",
495 output: null,
496 options: [],
497 parserOptions: { ecmaVersion: 6 },
498 errors: [{ messageId: "nonSimpleParameterList" }]
499 },
500 {
501 code: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
502 output: null,
503 options: [],
504 parserOptions: { ecmaVersion: 6 },
505 errors: [{ messageId: "nonSimpleParameterList" }]
506 },
507 {
508 code: "function foo(a = 0) { 'use strict' }",
509 output: null,
510 options: [],
511 parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } },
512 errors: [
513 "Use the global form of 'use strict'.",
514 { messageId: "nonSimpleParameterList" }
515 ]
516 },
517 {
518 code: "'use strict'; function foo(a = 0) { 'use strict' }",
519 output: null,
520 options: [],
521 parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } },
522 errors: [{ messageId: "nonSimpleParameterList" }]
523 },
524 {
525 code: "function foo(a = 0) { 'use strict' }",
526 output: null,
527 options: ["never"],
528 parserOptions: { ecmaVersion: 6 },
529 errors: [{ messageId: "nonSimpleParameterList" }]
530 },
531 {
532 code: "function foo(a = 0) { 'use strict' }",
533 output: null,
534 options: ["global"],
535 parserOptions: { ecmaVersion: 6 },
536 errors: [
537 "Use the global form of 'use strict'.",
538 { messageId: "nonSimpleParameterList" }
539 ]
540 },
541 {
542 code: "'use strict'; function foo(a = 0) { 'use strict' }",
543 output: null,
544 options: ["global"],
545 parserOptions: { ecmaVersion: 6 },
546 errors: [{ messageId: "nonSimpleParameterList" }]
547 },
548 {
549 code: "function foo(a = 0) { 'use strict' }",
550 output: null,
551 options: ["function"],
552 parserOptions: { ecmaVersion: 6 },
553 errors: [{ messageId: "nonSimpleParameterList" }]
554 },
555 {
556 code: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
557 output: null,
558 options: ["function"],
559 parserOptions: { ecmaVersion: 6 },
560 errors: [{ messageId: "nonSimpleParameterList" }]
561 },
562 {
563 code: "function foo(a = 0) { }",
564 output: null,
565 options: ["function"],
566 parserOptions: { ecmaVersion: 6 },
567 errors: [{ messageId: "wrap", data: { name: "function 'foo'" } }]
568 },
569 {
570 code: "(function() { function foo(a = 0) { } }())",
571 output: null,
572 options: ["function"],
573 parserOptions: { ecmaVersion: 6 },
574 errors: ["Use the function form of 'use strict'."]
575 }
576
577 ]
578 });