]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/strict.js
92abffda16ccc9b41152b564ebb4b752de05e2a3
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 const ruleTester
= new RuleTester();
17 ruleTester
.run("strict", rule
, {
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 } } },
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 } },
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" } },
56 code
: "class A { constructor() { } }",
57 options
: ["function"],
58 parserOptions
: { ecmaVersion
: 6 }
61 code
: "class A { foo() { } }",
62 options
: ["function"],
63 parserOptions
: { ecmaVersion
: 6 }
66 code
: "class A { foo() { function bar() { } } }",
67 options
: ["function"],
68 parserOptions
: { ecmaVersion
: 6 }
71 code
: "(function() { 'use strict'; function foo(a = 0) { } }())",
72 options
: ["function"],
73 parserOptions
: { ecmaVersion
: 6 }
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 } } },
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 } } }
94 code
: "\"use strict\"; foo();",
98 { messageId
: "never", type
: "ExpressionStatement" }
101 code
: "function foo() { 'use strict'; return; }",
105 { messageId
: "never", type
: "ExpressionStatement" }
108 code
: "var foo = function() { 'use strict'; return; };",
112 { messageId
: "never", type
: "ExpressionStatement" }
115 code
: "function foo() { return function() { 'use strict'; return; }; }",
119 { messageId
: "never", type
: "ExpressionStatement" }
122 code
: "'use strict'; function foo() { \"use strict\"; return; }",
126 { messageId
: "never", type
: "ExpressionStatement" },
127 { messageId
: "never", type
: "ExpressionStatement" }
130 code
: "\"use strict\"; foo();",
133 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
135 { messageId
: "module", type
: "ExpressionStatement" }
138 code
: "'use strict'; function foo() { 'use strict'; return; }",
139 output
: " function foo() { return; }",
141 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
143 { messageId
: "implied", type
: "ExpressionStatement" },
144 { messageId
: "implied", type
: "ExpressionStatement" }
147 code
: "'use strict'; function foo() { 'use strict'; return; }",
148 output
: " function foo() { return; }",
150 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
152 { messageId
: "module", type
: "ExpressionStatement" },
153 { messageId
: "module", type
: "ExpressionStatement" }
163 { messageId
: "global", type
: "Program" }
166 code
: "function foo() { 'use strict'; return; }",
170 { messageId
: "global", type
: "Program" },
171 { messageId
: "global", type
: "ExpressionStatement" }
174 code
: "var foo = function() { 'use strict'; return; }",
178 { messageId
: "global", type
: "Program" },
179 { messageId
: "global", type
: "ExpressionStatement" }
182 code
: "var foo = () => { 'use strict'; return () => 1; }",
185 parserOptions
: { ecmaVersion
: 6 },
187 { messageId
: "global", type
: "Program" },
188 { messageId
: "global", type
: "ExpressionStatement" }
191 code
: "'use strict'; function foo() { 'use strict'; return; }",
195 { messageId
: "global", type
: "ExpressionStatement" }
198 code
: "'use strict'; var foo = function() { 'use strict'; return; };",
202 { messageId
: "global", type
: "ExpressionStatement" }
205 code
: "'use strict'; 'use strict'; foo();",
206 output
: "'use strict'; foo();",
209 { messageId
: "multiple", type
: "ExpressionStatement" }
212 code
: "'use strict'; foo();",
215 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
217 { messageId
: "module", type
: "ExpressionStatement" }
220 code
: "'use strict'; function foo() { 'use strict'; return; }",
221 output
: " function foo() { return; }",
223 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
225 { messageId
: "implied", type
: "ExpressionStatement" },
226 { messageId
: "implied", type
: "ExpressionStatement" }
229 code
: "'use strict'; function foo() { 'use strict'; return; }",
230 output
: " function foo() { return; }",
232 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
234 { messageId
: "module", type
: "ExpressionStatement" },
235 { messageId
: "module", type
: "ExpressionStatement" }
241 code
: "'use strict'; foo();",
243 options
: ["function"],
245 { messageId
: "function", type
: "ExpressionStatement" }
248 code
: "'use strict'; (function() { 'use strict'; return true; }());",
250 options
: ["function"],
252 { messageId
: "function", type
: "ExpressionStatement" }
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"],
259 { messageId
: "unnecessary", type
: "ExpressionStatement" }
262 code
: "(function() { return true; }());",
264 options
: ["function"],
266 { messageId
: "function", type
: "FunctionExpression" }
269 code
: "(() => { return true; })();",
271 options
: ["function"],
272 parserOptions
: { ecmaVersion
: 6 },
274 { messageId
: "function", type
: "ArrowFunctionExpression" }
277 code
: "(() => true)();",
279 options
: ["function"],
280 parserOptions
: { ecmaVersion
: 6 },
282 { messageId
: "function", type
: "ArrowFunctionExpression" }
285 code
: "var foo = function() { foo(); 'use strict'; return; }; function bar() { foo(); 'use strict'; }",
287 options
: ["function"],
289 { messageId
: "function", type
: "FunctionExpression" },
290 { messageId
: "function", type
: "FunctionDeclaration" }
293 code
: "function foo() { 'use strict'; 'use strict'; return; }",
294 output
: "function foo() { 'use strict'; return; }",
295 options
: ["function"],
297 { messageId
: "multiple", type
: "ExpressionStatement" }
300 code
: "var foo = function() { 'use strict'; 'use strict'; return; }",
301 output
: "var foo = function() { 'use strict'; return; }",
302 options
: ["function"],
304 { messageId
: "multiple", type
: "ExpressionStatement" }
307 code
: "var foo = function() { 'use strict'; return; }",
308 output
: "var foo = function() { return; }",
309 options
: ["function"],
310 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
312 { messageId
: "module", type
: "ExpressionStatement" }
315 code
: "'use strict'; function foo() { 'use strict'; return; }",
316 output
: " function foo() { return; }",
317 options
: ["function"],
318 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
320 { messageId
: "implied", type
: "ExpressionStatement" },
321 { messageId
: "implied", type
: "ExpressionStatement" }
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 } },
329 { messageId
: "module", type
: "ExpressionStatement" },
330 { messageId
: "module", type
: "ExpressionStatement" }
333 code
: "function foo() { return function() { 'use strict'; return; }; }",
335 options
: ["function"],
337 { messageId
: "function", type
: "FunctionDeclaration" }
340 code
: "var foo = function() { function bar() { 'use strict'; return; } return; }",
342 options
: ["function"],
344 { messageId
: "function", type
: "FunctionExpression" }
347 code
: "function foo() { 'use strict'; return; } var bar = function() { return; };",
349 options
: ["function"],
351 { messageId
: "function", type
: "FunctionExpression" }
354 code
: "var foo = function() { 'use strict'; return; }; function bar() { return; };",
356 options
: ["function"],
358 { messageId
: "function", type
: "FunctionDeclaration" }
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"],
365 { messageId
: "unnecessary", type
: "ExpressionStatement" },
366 { messageId
: "multiple", type
: "ExpressionStatement" }
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"],
373 { messageId
: "unnecessary", type
: "ExpressionStatement" },
374 { messageId
: "multiple", type
: "ExpressionStatement" }
378 code
: "var foo = () => { return; };",
380 options
: ["function"],
381 parserOptions
: { ecmaVersion
: 6 },
382 errors
: [{ messageId
: "function", type
: "ArrowFunctionExpression" }]
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" }]
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" }]
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" }]
409 // "safe" mode corresponds to "global" if ecmaFeatures.globalReturn is true, otherwise "function"
411 code
: "'use strict'; function foo() { return; }",
415 { messageId
: "function", type
: "ExpressionStatement" },
416 { messageId
: "function", type
: "FunctionDeclaration" }
420 code
: "function foo() { 'use strict'; return; }",
423 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
425 { messageId
: "global", type
: "Program" },
426 { messageId
: "global", type
: "ExpressionStatement" }
430 code
: "'use strict'; function foo() { 'use strict'; return; }",
431 output
: " function foo() { return; }",
433 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
435 { messageId
: "implied", type
: "ExpressionStatement" },
436 { messageId
: "implied", type
: "ExpressionStatement" }
440 code
: "'use strict'; function foo() { 'use strict'; return; }",
441 output
: " function foo() { return; }",
443 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
445 { messageId
: "module", type
: "ExpressionStatement" },
446 { messageId
: "module", type
: "ExpressionStatement" }
450 // Default to "safe" mode
452 code
: "'use strict'; function foo() { return; }",
455 { messageId
: "function", type
: "ExpressionStatement" },
456 { messageId
: "function", type
: "FunctionDeclaration" }
460 code
: "function foo() { return; }",
462 errors
: [{ messageId
: "function", type
: "FunctionDeclaration" }]
465 code
: "function foo() { 'use strict'; return; }",
467 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
469 { messageId
: "global", type
: "Program" },
470 { messageId
: "global", type
: "ExpressionStatement" }
474 code
: "'use strict'; function foo() { 'use strict'; return; }",
475 output
: " function foo() { return; }",
476 parserOptions
: { ecmaFeatures
: { impliedStrict
: true } },
478 { messageId
: "implied", type
: "ExpressionStatement" },
479 { messageId
: "implied", type
: "ExpressionStatement" }
483 code
: "'use strict'; function foo() { 'use strict'; return; }",
484 output
: " function foo() { return; }",
485 parserOptions
: { ecmaVersion
: 6, sourceType
: "module", ecmaFeatures
: { impliedStrict
: true } },
487 { messageId
: "module", type
: "ExpressionStatement" },
488 { messageId
: "module", type
: "ExpressionStatement" }
492 // Reports deprecated syntax: https://github.com/eslint/eslint/issues/6405
494 code
: "function foo(a = 0) { 'use strict' }",
497 parserOptions
: { ecmaVersion
: 6 },
498 errors
: [{ messageId
: "nonSimpleParameterList" }]
501 code
: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
504 parserOptions
: { ecmaVersion
: 6 },
505 errors
: [{ messageId
: "nonSimpleParameterList" }]
508 code
: "function foo(a = 0) { 'use strict' }",
511 parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { globalReturn
: true } },
513 "Use the global form of 'use strict'.",
514 { messageId
: "nonSimpleParameterList" }
518 code
: "'use strict'; function foo(a = 0) { 'use strict' }",
521 parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { globalReturn
: true } },
522 errors
: [{ messageId
: "nonSimpleParameterList" }]
525 code
: "function foo(a = 0) { 'use strict' }",
528 parserOptions
: { ecmaVersion
: 6 },
529 errors
: [{ messageId
: "nonSimpleParameterList" }]
532 code
: "function foo(a = 0) { 'use strict' }",
535 parserOptions
: { ecmaVersion
: 6 },
537 "Use the global form of 'use strict'.",
538 { messageId
: "nonSimpleParameterList" }
542 code
: "'use strict'; function foo(a = 0) { 'use strict' }",
545 parserOptions
: { ecmaVersion
: 6 },
546 errors
: [{ messageId
: "nonSimpleParameterList" }]
549 code
: "function foo(a = 0) { 'use strict' }",
551 options
: ["function"],
552 parserOptions
: { ecmaVersion
: 6 },
553 errors
: [{ messageId
: "nonSimpleParameterList" }]
556 code
: "(function() { 'use strict'; function foo(a = 0) { 'use strict' } }())",
558 options
: ["function"],
559 parserOptions
: { ecmaVersion
: 6 },
560 errors
: [{ messageId
: "nonSimpleParameterList" }]
563 code
: "function foo(a = 0) { }",
565 options
: ["function"],
566 parserOptions
: { ecmaVersion
: 6 },
567 errors
: [{ messageId
: "wrap", data
: { name
: "function 'foo'" } }]
570 code
: "(function() { function foo(a = 0) { } }())",
572 options
: ["function"],
573 parserOptions
: { ecmaVersion
: 6 },
574 errors
: ["Use the function form of 'use strict'."]