Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix no-useless-return false negative in try statement #16593

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 47 additions & 1 deletion lib/rules/no-useless-return.js
Expand Up @@ -38,6 +38,15 @@ function isRemovable(node) {
return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type);
}

/**
* Checks if given node is a try statement or not.
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the node is a try statement.
*/
function isTryStatement(node) {
return node.type === "TryStatement";
}

/**
* Checks whether the given return statement is in a `finally` block or not.
* @param {ASTNode} node The return statement node to check.
Expand All @@ -49,14 +58,33 @@ function isInFinally(node) {
currentNode && currentNode.parent && !astUtils.isFunction(currentNode);
currentNode = currentNode.parent
) {
if (currentNode.parent.type === "TryStatement" && currentNode.parent.finalizer === currentNode) {
if (isTryStatement(currentNode.parent) && currentNode.parent.finalizer === currentNode) {
return true;
}
}

return false;
}

/**
* Checks whether the given node is the last one in the parentNode.
* @param {ASTNode} node The node to check.
* @param {ASTNode} parentNode The node that possibly contains the given `node`.
* @param {any} sourceCode The source code
* @returns {boolean} `true` if the node is the last one in the `parentNode` children.
*/
function isLastNode(node, parentNode, sourceCode) {
if (!parentNode.body) {
return false;
}

const lastNodeInParent = Array.isArray(parentNode.body)
? parentNode.body[parentNode.body.length - 1]
: parentNode.body;

return astUtils.equalTokens(node, lastNodeInParent, sourceCode);
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -274,6 +302,24 @@ module.exports = {
}
scopeInfo.uselessReturns.push(node);
},
"BlockStatement:exit"(node) {
const parentNode = node.parent;

if (
!(isTryStatement(parentNode) && isLastNode(parentNode, parentNode.parent, sourceCode)) ||
!scopeInfo.upper ||
!scopeInfo.uselessReturns
) {
return;
}

const lastUselessReturn = scopeInfo.uselessReturns[scopeInfo.uselessReturns.length - 1];


if (lastUselessReturn && isLastNode(lastUselessReturn, node, sourceCode)) {
scopeInfo.upper.uselessReturns.push(lastUselessReturn);
}
},

/*
* Registers for all statement nodes except FunctionDeclaration, BlockStatement, BreakStatement.
Expand Down
57 changes: 51 additions & 6 deletions tests/lib/rules/no-useless-return.js
Expand Up @@ -96,6 +96,15 @@ ruleTester.run("no-useless-return", rule, {
}
}
`,
`
function foo() {
try {
bar();
return;
} catch (err) {}
baz();
}
`,
`
function foo() {
return;
Expand Down Expand Up @@ -386,12 +395,48 @@ ruleTester.run("no-useless-return", rule, {
}
`
},

/*
* FIXME: Re-add this case (removed due to https://github.com/eslint/eslint/issues/7481):
* https://github.com/eslint/eslint/blob/261d7287820253408ec87c344beccdba2fe829a4/tests/lib/rules/no-useless-return.js#L308-L329
*/

{
code: `
function foo() {
try {
foo();
return;
} catch (err) {
return 5;
}
}
`,
output: `
function foo() {
try {
foo();

} catch (err) {
return 5;
}
}
`
},
{
code: `
function foo() {
try {
return;
} catch (err) {
foo();
}
}
`,
output: `
function foo() {
try {

} catch (err) {
foo();
}
}
`
},
{
code: `
function foo() {
Expand Down