Skip to content

Commit

Permalink
fix: check all try block statements in the list
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Mar 18, 2023
1 parent d02ad37 commit 66b6181
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
14 changes: 11 additions & 3 deletions lib/rules/no-useless-return.js
Expand Up @@ -168,12 +168,20 @@ module.exports = {

for (const node of info.uselessReturns) {
if (scopeInfo.traversedTryBlockStatements && scopeInfo.traversedTryBlockStatements.length > 0) {
const blockInitialRange = scopeInfo.traversedTryBlockStatements[0].range[0];
const blockFinalRange = scopeInfo.traversedTryBlockStatements[0].range[1];
const returnInitialRange = node.range[0];
const returnFinalRange = node.range[1];

if (returnInitialRange >= blockInitialRange && returnFinalRange <= blockFinalRange) {
const areBlocksInRange = scopeInfo.traversedTryBlockStatements.some(tryBlockStatement => {
const blockInitialRange = tryBlockStatement.range[0];
const blockFinalRange = tryBlockStatement.range[1];

return (
returnInitialRange >= blockInitialRange &&
returnFinalRange <= blockFinalRange
);
});

if (areBlocksInRange) {
continue;
}
}
Expand Down
53 changes: 46 additions & 7 deletions tests/lib/rules/no-useless-return.js
Expand Up @@ -19,7 +19,6 @@ const rule = require("../../../lib/rules/no-useless-return"),
const ruleTester = new RuleTester();

ruleTester.run("no-useless-return", rule, {

valid: [
"function foo() { return 5; }",
"function foo() { return null; }",
Expand Down Expand Up @@ -490,6 +489,36 @@ ruleTester.run("no-useless-return", rule, {
}
`
},
{
code: `
function foo() {
try {
bar();
} catch (e) {
try {
baz();
return;
} catch (e) {
qux();
}
}
}
`,
output: `
function foo() {
try {
bar();
} catch (e) {
try {
baz();
} catch (e) {
qux();
}
}
}
`
},
{
code: `
function foo() {
Expand Down Expand Up @@ -536,11 +565,21 @@ ruleTester.run("no-useless-return", rule, {
{
code: "function foo() { return; return; }",
output: "function foo() { return; }",
errors: [{
messageId: "unnecessaryReturn",
type: "ReturnStatement",
column: 18
}]
errors: [
{
messageId: "unnecessaryReturn",
type: "ReturnStatement",
column: 18
}
]
}
].map(invalidCase => Object.assign({ errors: [{ messageId: "unnecessaryReturn", type: "ReturnStatement" }] }, invalidCase))
].map(invalidCase =>
Object.assign(
{
errors: [
{ messageId: "unnecessaryReturn", type: "ReturnStatement" }
]
},
invalidCase
))
});

0 comments on commit 66b6181

Please sign in to comment.