]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-unreachable.js
2 * @fileoverview Tests for no-unreachable rule.
3 * @author Joel Feenstra
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require ( "../../../lib/rules/no-unreachable" ),
13 { RuleTester
} = require ( "../../../lib/rule-tester" );
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester ();
21 ruleTester
. run ( "no-unreachable" , rule
, {
23 "function foo() { function bar() { return 1; } return bar(); }" ,
24 "function foo() { return bar(); function bar() { return 1; } }" ,
25 "function foo() { return x; var x; }" ,
26 "function foo() { var x = 1; var y = 2; }" ,
27 "function foo() { var x = 1; var y = 2; return; }" ,
28 "while (true) { switch (foo) { case 1: x = 1; x = 2;} }" ,
29 "while (true) { break; var x; }" ,
30 "while (true) { continue; var x, y; }" ,
31 "while (true) { throw 'message'; var x; }" ,
32 "while (true) { if (true) break; var x = 1; }" ,
33 "while (true) continue;" ,
34 "switch (foo) { case 1: break; var x; }" ,
35 "switch (foo) { case 1: break; var x; default: throw true; };" ,
37 code
: "const arrow_direction = arrow => { switch (arrow) { default: throw new Error(); };}" ,
42 "var x = 1; y = 2; throw 'uh oh'; var y;" ,
43 "function foo() { var x = 1; if (x) { return; } x = 2; }" ,
44 "function foo() { var x = 1; if (x) { } else { return; } x = 2; }" ,
45 "function foo() { var x = 1; switch (x) { case 0: break; default: return; } x = 2; }" ,
46 "function foo() { var x = 1; while (x) { return; } x = 2; }" ,
47 "function foo() { var x = 1; for (x in {}) { return; } x = 2; }" ,
48 "function foo() { var x = 1; try { return; } finally { x = 2; } }" ,
49 "function foo() { var x = 1; for (;;) { if (x) break; } x = 2; }" ,
50 "A: { break A; } foo()" ,
52 code
: "function* foo() { try { yield 1; return; } catch (err) { return err; } }" ,
58 code
: "function foo() { try { bar(); return; } catch (err) { return err; } }" ,
64 code
: "function foo() { try { a.b.c = 1; return; } catch (err) { return err; } }" ,
70 code
: "class C { foo = reachable; }" ,
71 parserOptions
: { ecmaVersion
: 2022 }
74 code
: "class C { foo = reachable; constructor() {} }" ,
75 parserOptions
: { ecmaVersion
: 2022 }
78 code
: "class C extends B { foo = reachable; }" ,
79 parserOptions
: { ecmaVersion
: 2022 }
82 code
: "class C extends B { foo = reachable; constructor() { super(); } }" ,
83 parserOptions
: { ecmaVersion
: 2022 }
86 code
: "class C extends B { static foo = reachable; constructor() {} }" ,
87 parserOptions
: { ecmaVersion
: 2022 }
91 { code
: "function foo() { return x; var x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "VariableDeclaration" }] },
92 { code
: "function foo() { return x; var x, y = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "VariableDeclaration" }] },
93 { code
: "while (true) { continue; var x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "VariableDeclaration" }] },
94 { code
: "function foo() { return; x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
95 { code
: "function foo() { throw error; x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
96 { code
: "while (true) { break; x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
97 { code
: "while (true) { continue; x = 1; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
98 { code
: "function foo() { switch (foo) { case 1: return; x = 1; } }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
99 { code
: "function foo() { switch (foo) { case 1: throw e; x = 1; } }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
100 { code
: "while (true) { switch (foo) { case 1: break; x = 1; } }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
101 { code
: "while (true) { switch (foo) { case 1: continue; x = 1; } }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
102 { code
: "var x = 1; throw 'uh oh'; var y = 2;" , errors
: [{ messageId
: "unreachableCode" , type
: "VariableDeclaration" }] },
103 { code
: "function foo() { var x = 1; if (x) { return; } else { throw e; } x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
104 { code
: "function foo() { var x = 1; if (x) return; else throw -1; x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
105 { code
: "function foo() { var x = 1; try { return; } finally {} x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
106 { code
: "function foo() { var x = 1; try { } finally { return; } x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
107 { code
: "function foo() { var x = 1; do { return; } while (x); x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
108 { code
: "function foo() { var x = 1; while (x) { if (x) break; else continue; x = 2; } }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
109 { code
: "function foo() { var x = 1; for (;;) { if (x) continue; } x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
110 { code
: "function foo() { var x = 1; while (true) { } x = 2; }" , errors
: [{ messageId
: "unreachableCode" , type
: "ExpressionStatement" }] },
112 code
: "const arrow_direction = arrow => { switch (arrow) { default: throw new Error(); }; g() }" ,
113 parserOptions
: { ecmaVersion
: 6 },
116 messageId
: "unreachableCode" ,
117 type
: "ExpressionStatement" ,
126 // Merge the warnings of continuous unreachable nodes.
132 a(); // ← ERROR: Unreachable code. (no-unreachable)
134 b() // ↑ ';' token is included in the unreachable code, so this statement will be merged.
136 c(); // ↑ ')' token is included in the unreachable code, so this statement will be merged.
141 messageId
: "unreachableCode" ,
142 type
: "ExpressionStatement" ,
166 messageId
: "unreachableCode" ,
167 type
: "ExpressionStatement" ,
190 messageId
: "unreachableCode" ,
191 type
: "ExpressionStatement" ,
198 messageId
: "unreachableCode" ,
199 type
: "ExpressionStatement" ,
223 messageId
: "unreachableCode" ,
224 type
: "ExpressionStatement" ,
231 messageId
: "unreachableCode" ,
232 type
: "ExpressionStatement" ,
239 messageId
: "unreachableCode" ,
240 type
: "ExpressionStatement" ,
262 messageId
: "unreachableCode" ,
263 type
: "BlockStatement" ,
285 messageId
: "unreachableCode" ,
286 type
: "BlockStatement" ,
309 messageId
: "unreachableCode" ,
310 type
: "VariableDeclaration" ,
317 messageId
: "unreachableCode" ,
318 type
: "BlockStatement" ,
328 * If `extends` exists, constructor exists, and the constructor doesn't
329 * contain `super()`, then the fields are unreachable because the
330 * evaluation of `super()` initializes fields in that case.
331 * In most cases, such an instantiation throws runtime errors, but
332 * doesn't throw if the constructor returns a value.
335 code
: "class C extends B { foo; constructor() {} }" ,
336 parserOptions
: { ecmaVersion
: 2022 },
337 errors
: [{ messageId
: "unreachableCode" , column
: 21 , endColumn
: 25 }]
340 code
: "class C extends B { foo = unreachable + code; constructor() {} }" ,
341 parserOptions
: { ecmaVersion
: 2022 },
342 errors
: [{ messageId
: "unreachableCode" , column
: 21 , endColumn
: 46 }]
345 code
: "class C extends B { foo; bar; constructor() {} }" ,
346 parserOptions
: { ecmaVersion
: 2022 },
347 errors
: [{ messageId
: "unreachableCode" , column
: 21 , endColumn
: 30 }]
350 code
: "class C extends B { foo; constructor() {} bar; }" ,
351 parserOptions
: { ecmaVersion
: 2022 },
353 { messageId
: "unreachableCode" , column
: 21 , endColumn
: 25 },
354 { messageId
: "unreachableCode" , column
: 43 , endColumn
: 47 }
358 code
: "(class extends B { foo; constructor() {} bar; })" ,
359 parserOptions
: { ecmaVersion
: 2022 },
361 { messageId
: "unreachableCode" , column
: 20 , endColumn
: 24 },
362 { messageId
: "unreachableCode" , column
: 42 , endColumn
: 46 }
366 code
: "class B extends A { x; constructor() { class C extends D { [super().x]; constructor() {} } } }" ,
367 parserOptions
: { ecmaVersion
: 2022 },
369 { messageId
: "unreachableCode" , column
: 60 , endColumn
: 72 }
373 code
: "class B extends A { x; constructor() { class C extends super().x { y; constructor() {} } } }" ,
374 parserOptions
: { ecmaVersion
: 2022 },
376 { messageId
: "unreachableCode" , column
: 68 , endColumn
: 70 }
380 code
: "class B extends A { x; static y; z; static q; constructor() {} }" ,
381 parserOptions
: { ecmaVersion
: 2022 },
383 { messageId
: "unreachableCode" , column
: 21 , endColumn
: 23 },
384 { messageId
: "unreachableCode" , column
: 34 , endColumn
: 36 }