]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/strict.js
e3ea705fe7ef60df0019068d2b48081cd6f1d02b
2 * @fileoverview Tests for strict rule.
3 * @author Nicholas C. Zakas
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/strict"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("strict", rule
, {
25 { code
: "foo();", options
: ["never"] },
26 { code
: "function foo() { return; }", options
: ["never"] },
27 { code
: "var foo = function() { return; };", options
: ["never"] },
28 { code
: "foo(); 'use strict';", options
: ["never"] },
29 { code
: "function foo() { bar(); 'use strict'; return; }", options
: ["never"] },
30 { code
: "var foo = function() { { 'use strict'; } return; };", options
: ["never"] },
31 { code
: "(function() { bar('use strict'); return; }());", options
: ["never"] },
32 { code
: "var fn = x => 1;", options
: ["never"], parserOptions
: { ecmaVersion
: 6 } },
33 { code
: "var fn = x => { return; };", options
: ["never"], parserOptions
: { ecmaVersion
: 6 } },
34 { code
: "foo();", options
: ["never"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
35 { code
: "function foo() { return; }", options
: ["never"], parserOptions
: { ecmaFeatures
: { impliedStrict
: true } } },
38 { code
: "// Intentionally empty", options
: ["global"] },
39 { code
: "\"use strict\"; foo();", options
: ["global"] },
40 { code
: "foo();", options
: ["global"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
41 { code
: "function foo() { return; }", options
: ["global"], parserOptions
: { ecmaFeatures
: { impliedStrict
: true } } },
42 { code
: "'use strict'; function foo() { return; }", options
: ["global"] },
43 { code
: "'use strict'; var foo = function() { return; };", options
: ["global"] },
44 { code
: "'use strict'; function foo() { bar(); 'use strict'; return; }", options
: ["global"] },
45 { code
: "'use strict'; var foo = function() { bar(); 'use strict'; return; };", options
: ["global"] },
46 { code
: "'use strict'; function foo() { return function() { bar(); 'use strict'; return; }; }", options
: ["global"] },
47 { code
: "'use strict'; var foo = () => { return () => { bar(); 'use strict'; return; }; }", options
: ["global"], parserOptions
: { ecmaVersion
: 6 } },
50 { code
: "function foo() { 'use strict'; return; }", options
: ["function"] },
51 { code
: "function foo() { return; }", options
: ["function"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
52 { code
: "function foo() { return; }", options
: ["function"], parserOptions
: { ecmaFeatures
: { impliedStrict
: true } } },
53 { code
: "var foo = function() { return; }", options
: ["function"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
54 { code
: "var foo = function() { 'use strict'; return; }", options
: ["function"] },
55 { code
: "function foo() { 'use strict'; return; } var bar = function() { 'use strict'; bar(); };", options
: ["function"] },
56 { code
: "var foo = function() { 'use strict'; function bar() { return; } bar(); };", options
: ["function"] },
57 { code
: "var foo = () => { 'use strict'; var bar = () => 1; bar(); };", options
: ["function"], parserOptions
: { ecmaVersion
: 6 } },
58 { code
: "var foo = () => { var bar = () => 1; bar(); };", options
: ["function"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
60 code
: "class A { constructor() { } }",
61 options
: ["function"],
62 parserOptions
: { ecmaVersion
: 6 }
65 code
: "class A { foo() { } }",
66 options
: ["function"],
67 parserOptions
: { ecmaVersion
: 6 }
70 code
: "class A { foo() { function bar() { } } }",
71 options
: ["function"],
72 parserOptions
: { ecmaVersion
: 6 }
75 code
: "(function() { 'use strict'; function foo(a = 0) { } }())",
76 options
: ["function"],
77 parserOptions
: { ecmaVersion
: 6 }
81 // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
82 { code
: "function foo() { 'use strict'; return; }", options
: ["safe"] },
83 { code
: "'use strict'; function foo() { return; }", options
: ["safe"], parserOptions
: { ecmaFeatures
: { globalReturn
: true } } },
84 { code
: "function foo() { return; }", options
: ["safe"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
85 { code
: "function foo() { return; }", options
: ["safe"], parserOptions
: { ecmaFeatures
: { impliedStrict
: true } } },
87 // defaults to "safe" mode
88 "function foo() { 'use strict'; return; }",
89 { code
: "'use strict'; function foo() { return; }", parserOptions
: { ecmaFeatures
: { globalReturn
: true } } },
90 { code
: "function foo() { return; }", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
91 { code
: "function foo() { return; }", parserOptions
: { ecmaFeatures
: { impliedStrict
: true } } },
93 // class static blocks do not have directive prologues, therefore this rule should never require od disallow "use strict" statement in them.
94 { code
: "'use strict'; class C { static { foo; } }", options
: ["global"], parserOptions
: { ecmaVersion
: 2022 } },
95 { code
: "'use strict'; class C { static { 'use strict'; } }", options
: ["global"], parserOptions
: { ecmaVersion
: 2022 } },
96 { code
: "'use strict'; class C { static { 'use strict'; 'use strict'; } }", options
: ["global"], parserOptions
: { ecmaVersion
: 2022 } },
97 { code
: "class C { static { foo; } }", options
: ["function"], parserOptions
: { ecmaVersion
: 2022 } },
98 { code
: "class C { static { 'use strict'; } }", options
: ["function"], parserOptions
: { ecmaVersion
: 2022 } },
99 { code
: "class C { static { 'use strict'; 'use strict'; } }", options
: ["function"], parserOptions
: { ecmaVersion
: 2022 } },
100 { code
: "class C { static { foo; } }", options
: ["never"], parserOptions
: { ecmaVersion
: 2022 } },
101 { code
: "class C { static { 'use strict'; } }", options
: ["never"], parserOptions
: { ecmaVersion
: 2022 } },
102 { code
: "class C { static { 'use strict'; 'use strict'; } }", options
: ["never"], parserOptions
: { ecmaVersion
: 2022 } },
103 { code
: "class C { static { 'use strict'; } }", options
: ["safe"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
104 { code
: "class C { static { 'use strict'; } }", options
: ["safe"], parserOptions
: { ecmaVersion
: 2022, ecmaFeatures
: { impliedStrict
: true } } }
111 code
: "\"use strict\"; foo();",
115 { messageId
: "never", type
: "ExpressionStatement" }
118 code
: "function foo() { 'use strict'; return; }",
122 { messageId
: "never", type
: "ExpressionStatement" }
125 code
: "var foo = function() { 'use strict'; return; };",
129 { messageId
: "never", type
: "ExpressionStatement" }
132 code
: "function foo() { return function() { 'use strict'; return; }; }",
136 { messageId
: "never", type
: "ExpressionStatement" }
139 code
: "'use strict'; function foo() { \"use strict\"; return; }",
143 { messageId
: "never", type
: "ExpressionStatement" },
144 { messageId
: "never", type
: "ExpressionStatement" }
147 code
: "\"use strict\"; foo();",
150 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
152 { messageId
: "module", type
: "ExpressionStatement" }
155 code
: "'use strict'; function foo() { 'use strict'; return; }",
156 output
: " function foo() { return; }",
158 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
160 { messageId
: "implied", type
: "ExpressionStatement" },
161 { messageId
: "implied", type
: "ExpressionStatement" }
164 code
: "'use strict'; function foo() { 'use strict'; return; }",
165 output
: " function foo() { return; }",
167 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
169 { messageId
: "module", type
: "ExpressionStatement" },
170 { messageId
: "module", type
: "ExpressionStatement" }
180 { messageId
: "global", type
: "Program" }
183 code
: "function foo() { 'use strict'; return; }",
187 { messageId
: "global", type
: "Program" },
188 { messageId
: "global", type
: "ExpressionStatement" }
191 code
: "var foo = function() { 'use strict'; return; }",
195 { messageId
: "global", type
: "Program" },
196 { messageId
: "global", type
: "ExpressionStatement" }
199 code
: "var foo = () => { 'use strict'; return () => 1; }",
202 parserOptions
: { ecmaVersion
: 6 },
204 { messageId
: "global", type
: "Program" },
205 { messageId
: "global", type
: "ExpressionStatement" }
208 code
: "'use strict'; function foo() { 'use strict'; return; }",
212 { messageId
: "global", type
: "ExpressionStatement" }
215 code
: "'use strict'; var foo = function() { 'use strict'; return; };",
219 { messageId
: "global", type
: "ExpressionStatement" }
222 code
: "'use strict'; 'use strict'; foo();",
223 output
: "'use strict'; foo();",
226 { messageId
: "multiple", type
: "ExpressionStatement" }
229 code
: "'use strict'; foo();",
232 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
234 { messageId
: "module", type
: "ExpressionStatement" }
237 code
: "'use strict'; function foo() { 'use strict'; return; }",
238 output
: " function foo() { return; }",
240 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
242 { messageId
: "implied", type
: "ExpressionStatement" },
243 { messageId
: "implied", type
: "ExpressionStatement" }
246 code
: "'use strict'; function foo() { 'use strict'; return; }",
247 output
: " function foo() { return; }",
249 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
251 { messageId
: "module", type
: "ExpressionStatement" },
252 { messageId
: "module", type
: "ExpressionStatement" }
258 code
: "'use strict'; foo();",
260 options
: ["function"],
262 { messageId
: "function", type
: "ExpressionStatement" }
265 code
: "'use strict'; (function() { 'use strict'; return true; }());",
267 options
: ["function"],
269 { messageId
: "function", type
: "ExpressionStatement" }
272 code
: "(function() { 'use strict'; function f() { 'use strict'; return } return true; }());",
273 output
: "(function() { 'use strict'; function f() { return } return true; }());",
274 options
: ["function"],
276 { messageId
: "unnecessary", type
: "ExpressionStatement" }
279 code
: "(function() { return true; }());",
281 options
: ["function"],
283 { messageId
: "function", type
: "FunctionExpression" }
286 code
: "(() => { return true; })();",
288 options
: ["function"],
289 parserOptions
: { ecmaVersion
: 6 },
291 { messageId
: "function", type
: "ArrowFunctionExpression" }
294 code
: "(() => true)();",
296 options
: ["function"],
297 parserOptions
: { ecmaVersion
: 6 },
299 { messageId
: "function", type
: "ArrowFunctionExpression" }
302 code
: "var foo = function() { foo(); 'use strict'; return; }; function bar() { foo(); 'use strict'; }",
304 options
: ["function"],
306 { messageId
: "function", type
: "FunctionExpression" },
307 { messageId
: "function", type
: "FunctionDeclaration" }
310 code
: "function foo() { 'use strict'; 'use strict'; return; }",
311 output
: "function foo() { 'use strict'; return; }",
312 options
: ["function"],
314 { messageId
: "multiple", type
: "ExpressionStatement" }
317 code
: "var foo = function() { 'use strict'; 'use strict'; return; }",
318 output
: "var foo = function() { 'use strict'; return; }",
319 options
: ["function"],
321 { messageId
: "multiple", type
: "ExpressionStatement" }
324 code
: "var foo = function() { 'use strict'; return; }",
325 output
: "var foo = function() { return; }",
326 options
: ["function"],
327 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
329 { messageId
: "module", type
: "ExpressionStatement" }
332 code
: "'use strict'; function foo() { 'use strict'; return; }",
333 output
: " function foo() { return; }",
334 options
: ["function"],
335 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
337 { messageId
: "implied", type
: "ExpressionStatement" },
338 { messageId
: "implied", type
: "ExpressionStatement" }
341 code
: "'use strict'; function foo() { 'use strict'; return; }",
342 output
: " function foo() { return; }",
343 options
: ["function"],
344 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
346 { messageId
: "module", type
: "ExpressionStatement" },
347 { messageId
: "module", type
: "ExpressionStatement" }
350 code
: "function foo() { return function() { 'use strict'; return; }; }",
352 options
: ["function"],
354 { messageId
: "function", type
: "FunctionDeclaration" }
357 code
: "var foo = function() { function bar() { 'use strict'; return; } return; }",
359 options
: ["function"],
361 { messageId
: "function", type
: "FunctionExpression" }
364 code
: "function foo() { 'use strict'; return; } var bar = function() { return; };",
366 options
: ["function"],
368 { messageId
: "function", type
: "FunctionExpression" }
371 code
: "var foo = function() { 'use strict'; return; }; function bar() { return; };",
373 options
: ["function"],
375 { messageId
: "function", type
: "FunctionDeclaration" }
378 code
: "function foo() { 'use strict'; return function() { 'use strict'; 'use strict'; return; }; }",
379 output
: "function foo() { 'use strict'; return function() { return; }; }",
380 options
: ["function"],
382 { messageId
: "unnecessary", type
: "ExpressionStatement" },
383 { messageId
: "multiple", type
: "ExpressionStatement" }
386 code
: "var foo = function() { 'use strict'; function bar() { 'use strict'; 'use strict'; return; } }",
387 output
: "var foo = function() { 'use strict'; function bar() { return; } }",
388 options
: ["function"],
390 { messageId
: "unnecessary", type
: "ExpressionStatement" },
391 { messageId
: "multiple", type
: "ExpressionStatement" }
395 code
: "var foo = () => { return; };",
397 options
: ["function"],
398 parserOptions
: { ecmaVersion
: 6 },
399 errors
: [{ messageId
: "function", type
: "ArrowFunctionExpression" }]
404 code
: "class A { constructor() { \"use strict\"; } }",
405 output
: "class A { constructor() { } }",
406 options
: ["function"],
407 parserOptions
: { ecmaVersion
: 6 },
408 errors
: [{ messageId
: "unnecessaryInClasses", type
: "ExpressionStatement" }]
411 code
: "class A { foo() { \"use strict\"; } }",
412 output
: "class A { foo() { } }",
413 options
: ["function"],
414 parserOptions
: { ecmaVersion
: 6 },
415 errors
: [{ messageId
: "unnecessaryInClasses", type
: "ExpressionStatement" }]
418 code
: "class A { foo() { function bar() { \"use strict\"; } } }",
419 output
: "class A { foo() { function bar() { } } }",
420 options
: ["function"],
421 parserOptions
: { ecmaVersion
: 6 },
422 errors
: [{ messageId
: "unnecessaryInClasses", type
: "ExpressionStatement" }]
425 code
: "class A { field = () => { \"use strict\"; } }",
426 output
: "class A { field = () => { } }",
427 options
: ["function"],
428 parserOptions
: { ecmaVersion
: 2022 },
429 errors
: [{ messageId
: "unnecessaryInClasses", type
: "ExpressionStatement" }]
432 code
: "class A { field = function() { \"use strict\"; } }",
433 output
: "class A { field = function() { } }",
434 options
: ["function"],
435 parserOptions
: { ecmaVersion
: 2022 },
436 errors
: [{ messageId
: "unnecessaryInClasses", type
: "ExpressionStatement" }]
439 // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
441 code
: "'use strict'; function foo() { return; }",
445 { messageId
: "function", type
: "ExpressionStatement" },
446 { messageId
: "function", type
: "FunctionDeclaration" }
450 code
: "function foo() { 'use strict'; return; }",
453 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
455 { messageId
: "global", type
: "Program" },
456 { messageId
: "global", type
: "ExpressionStatement" }
460 code
: "'use strict'; function foo() { 'use strict'; return; }",
461 output
: " function foo() { return; }",
463 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
465 { messageId
: "implied", type
: "ExpressionStatement" },
466 { messageId
: "implied", type
: "ExpressionStatement" }
470 code
: "'use strict'; function foo() { 'use strict'; return; }",
471 output
: " function foo() { return; }",
473 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
475 { messageId
: "module", type
: "ExpressionStatement" },
476 { messageId
: "module", type
: "ExpressionStatement" }
480 // Default to "safe" mode
482 code
: "'use strict'; function foo() { return; }",
485 { messageId
: "function", type
: "ExpressionStatement" },
486 { messageId
: "function", type
: "FunctionDeclaration" }
490 code
: "function foo() { return; }",
492 errors
: [{ messageId
: "function", type
: "FunctionDeclaration" }]
495 code
: "function foo() { 'use strict'; return; }",
497 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
499 { messageId
: "global", type
: "Program" },
500 { messageId
: "global", type
: "ExpressionStatement" }
504 code
: "'use strict'; function foo() { 'use strict'; return; }",
505 output
: " function foo() { return; }",
506 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
508 { messageId
: "implied", type
: "ExpressionStatement" },
509 { messageId
: "implied", type
: "ExpressionStatement" }
513 code
: "'use strict'; function foo() { 'use strict'; return; }",
514 output
: " function foo() { return; }",
515 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
517 { messageId
: "module", type
: "ExpressionStatement" },
518 { messageId
: "module", type
: "ExpressionStatement" }
522 // Reports deprecated syntax: https://github.com/eslint/eslint/issues/6405
524 code
: "function foo(a = 0) { 'use strict' }",
527 parserOptions
: { ecmaVersion
: 6 },
528 errors
: [{ messageId
: "nonSimpleParameterList" }]
531 code
: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
534 parserOptions
: { ecmaVersion
: 6 },
535 errors
: [{ messageId
: "nonSimpleParameterList" }]
538 code
: "function foo(a = 0) { 'use strict' }",
541 parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { globalReturn
: true } },
543 "Use the global form of 'use strict'.",
544 { messageId
: "nonSimpleParameterList" }
548 code
: "'use strict'; function foo(a = 0) { 'use strict' }",
551 parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { globalReturn
: true } },
552 errors
: [{ messageId
: "nonSimpleParameterList" }]
555 code
: "function foo(a = 0) { 'use strict' }",
558 parserOptions
: { ecmaVersion
: 6 },
559 errors
: [{ messageId
: "nonSimpleParameterList" }]
562 code
: "function foo(a = 0) { 'use strict' }",
565 parserOptions
: { ecmaVersion
: 6 },
567 "Use the global form of 'use strict'.",
568 { messageId
: "nonSimpleParameterList" }
572 code
: "'use strict'; function foo(a = 0) { 'use strict' }",
575 parserOptions
: { ecmaVersion
: 6 },
576 errors
: [{ messageId
: "nonSimpleParameterList" }]
579 code
: "function foo(a = 0) { 'use strict' }",
581 options
: ["function"],
582 parserOptions
: { ecmaVersion
: 6 },
583 errors
: [{ messageId
: "nonSimpleParameterList" }]
586 code
: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
588 options
: ["function"],
589 parserOptions
: { ecmaVersion
: 6 },
590 errors
: [{ messageId
: "nonSimpleParameterList" }]
593 code
: "function foo(a = 0) { }",
595 options
: ["function"],
596 parserOptions
: { ecmaVersion
: 6 },
597 errors
: [{ messageId
: "wrap", data
: { name
: "function 'foo'" } }]
600 code
: "(function() { function foo(a = 0) { } }())",
602 options
: ["function"],
603 parserOptions
: { ecmaVersion
: 6 },
604 errors
: ["Use the function form of 'use strict'."]
607 // functions inside class static blocks should be checked
609 code
: "'use strict'; class C { static { function foo() { \n'use strict'; } } }",
612 parserOptions
: { ecmaVersion
: 2022 },
613 errors
: [{ messageId
: "global", line
: 2 }]
616 code
: "class C { static { function foo() { \n'use strict'; } } }",
619 parserOptions
: { ecmaVersion
: 2022 },
620 errors
: [{ messageId
: "never", line
: 2 }]
623 code
: "class C { static { function foo() { \n'use strict'; } } }",
624 output
: "class C { static { function foo() { \n } } }",
626 parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" },
627 errors
: [{ messageId
: "module", line
: 2 }]
630 code
: "class C { static { function foo() { \n'use strict'; } } }",
631 output
: "class C { static { function foo() { \n } } }",
633 parserOptions
: { ecmaVersion
: 2022, ecmaFeatures
: { impliedStrict
: true } },
634 errors
: [{ messageId
: "implied", line
: 2 }]
637 code
: "function foo() {'use strict'; class C { static { function foo() { \n'use strict'; } } } }",
638 output
: "function foo() {'use strict'; class C { static { function foo() { \n } } } }",
639 options
: ["function"],
640 parserOptions
: { ecmaVersion
: 2022 },
641 errors
: [{ messageId
: "unnecessary", line
: 2 }]
644 code
: "class C { static { function foo() { \n'use strict'; } } }",
645 output
: "class C { static { function foo() { \n } } }",
646 options
: ["function"],
647 parserOptions
: { ecmaVersion
: 2022 },
648 errors
: [{ messageId
: "unnecessaryInClasses", line
: 2 }]
651 code
: "class C { static { function foo() { \n'use strict';\n'use strict'; } } }",
652 output
: "class C { static { function foo() { \n\n } } }",
653 options
: ["function"],
654 parserOptions
: { ecmaVersion
: 2022 },
656 { messageId
: "unnecessaryInClasses", line
: 2 },
657 { messageId
: "multiple", line
: 3 }