Skip to content

Commit

Permalink
add try catch finally pattern to return statement (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipopotamasu authored Feb 23, 2024
1 parent dfea636 commit 4f4b274
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions rules/no-implicit-any/return-statement/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ ruleTester.run('return-statement', rule, {
}
`,
},
{
code: `
function foo () {
try {
return doSomethingMightHaveError();
} catch {
throw new Error('error');
} finally {
return null;
}
}
`,
},
{
code: `
function foo () {
try {
doSomethingMightHaveError();
return null;
} catch {
return 'error'
}
}
`,
},
],
invalid: [
{
Expand Down
6 changes: 5 additions & 1 deletion rules/no-implicit-any/return-statement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function getReturnStatementNodes(nodes: TSESTree.Statement[]) {
returnStatementNodes.push(getReturnStatementNodes(caseNode.consequent));
});
break;
case AST_NODE_TYPES.TryStatement:
returnStatementNodes.push(getReturnStatementNodes(node.block.body));
if (node.handler) returnStatementNodes.push(getReturnStatementNode(node.handler.body));
if (node.finalizer) returnStatementNodes.push(getReturnStatementNodes(node.finalizer.body));
break;
case AST_NODE_TYPES.IfStatement:
returnStatementNodes.push(getReturnStatementNode(node.consequent));
if (node.alternate) returnStatementNodes.push(getReturnStatementNode(node.alternate));
Expand Down Expand Up @@ -91,7 +96,6 @@ export const lintReturnStatement = (
const functionNode = getFunctionNode(node.parent);
if (functionNode.body.type !== AST_NODE_TYPES.BlockStatement) return;
const returnStatementNodes = getReturnStatementNodes(functionNode.body.body);

let shouldReport = true;

for (const returnStatementNode of returnStatementNodes) {
Expand Down

0 comments on commit 4f4b274

Please sign in to comment.