]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-useless-return.js
2 * @fileoverview Disallow redundant return statements
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require("../../../lib/rules/no-useless-return"),
12 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("no-useless-return", rule
, {
24 "function foo() { return 5; }",
25 "function foo() { return null; }",
26 "function foo() { return doSomething(); }",
78 for (var foo = 0; foo < 10; foo++) {
85 for (var foo in bar) {
95 return; // This is allowed because it can override the returned value of 5
108 for (var foo of bar) return;
111 parserOptions
: { ecmaVersion
: 6 }
114 code
: "() => { if (foo) return; bar(); }",
115 parserOptions
: { ecmaVersion
: 6 }
119 parserOptions
: { ecmaVersion
: 6 }
122 code
: "() => { return; doSomething(); }",
123 parserOptions
: { ecmaVersion
: 6 }
126 code
: "if (foo) { return; } doSomething();",
127 parserOptions
: { ecmaFeatures
: { globalReturn
: true } }
130 // https://github.com/eslint/eslint/issues/7477
153 // https://github.com/eslint/eslint/issues/7583
162 // https://github.com/eslint/eslint/issues/7855
165 throw new Error('foo');
170 // https://github.com/eslint/eslint/issues/11647
173 throw new Error("Debugging...");
184 code
: "function foo() { return; }",
185 output
: "function foo() { }"
188 code
: "function foo() { doSomething(); return; }",
189 output
: "function foo() { doSomething(); }"
192 code
: "function foo() { if (condition) { bar(); return; } else { baz(); } }",
193 output
: "function foo() { if (condition) { bar(); } else { baz(); } }"
196 code
: "function foo() { if (foo) return; }",
197 output
: "function foo() { if (foo) return; }"
200 code
: "function foo() { bar(); return/**/; }",
204 code
: "function foo() { bar(); return//\n; }",
208 code
: "foo(); return;",
210 parserOptions
: { ecmaFeatures
: { globalReturn
: true } }
213 code
: "if (foo) { bar(); return; } else { baz(); }",
214 output
: "if (foo) { bar(); } else { baz(); }",
215 parserOptions
: { ecmaFeatures
: { globalReturn
: true } }
233 `, // Other case is fixed in the second pass.
235 { messageId
: "unnecessaryReturn", type
: "ReturnStatement" },
236 { messageId
: "unnecessaryReturn", type
: "ReturnStatement" }
380 try {} catch (err) { return; }
385 try {} catch (err) { }
391 * FIXME: Re-add this case (removed due to https://github.com/eslint/eslint/issues/7481):
392 * https://github.com/eslint/eslint/blob/261d7287820253408ec87c344beccdba2fe829a4/tests/lib/rules/no-useless-return.js#L308-L329
434 code
: "() => { return; }",
436 parserOptions
: { ecmaVersion
: 6 }
439 code
: "function foo() { return; return; }",
440 output
: "function foo() { return; }",
442 messageId
: "unnecessaryReturn",
443 type
: "ReturnStatement",
447 ].map(invalidCase
=> Object
.assign({ errors
: [{ messageId
: "unnecessaryReturn", type
: "ReturnStatement" }] }, invalidCase
))