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

fix(eslint-plugin): [return-await] don't error for in-try-catch if the return is in a catch without a finally #2356

Merged
merged 16 commits into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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
58 changes: 53 additions & 5 deletions packages/eslint-plugin/src/rules/return-await.ts
Expand Up @@ -57,23 +57,63 @@ export default util.createRule({
};
}

function inTryCatch(node: ts.Node): boolean {
function inTry(node: ts.Node): boolean {
let ancestor = node.parent;

while (ancestor && !ts.isFunctionLike(ancestor)) {
if (tsutils.isTryStatement(ancestor)) {
return true;
}

ancestor = ancestor.parent;
}

return false;
}

function inCatch(node: ts.Node): boolean {
let ancestor = node.parent;

while (ancestor && !ts.isFunctionLike(ancestor)) {
if (tsutils.isCatchClause(ancestor)) {
return true;
}

ancestor = ancestor.parent;
}

return false;
}

function isReturnPromiseInFinally(node: ts.Node): boolean {
let ancestor = node.parent;

while (ancestor && !ts.isFunctionLike(ancestor)) {
if (
tsutils.isTryStatement(ancestor) ||
tsutils.isCatchClause(ancestor)
tsutils.isTryStatement(ancestor.parent) &&
tsutils.isBlock(ancestor) &&
ancestor.parent.end === ancestor.end
) {
return true;
}

ancestor = ancestor.parent;
}

return false;
}

function hasFinallyBlock(node: ts.Node): boolean {
let ancestor = node.parent;

while (ancestor && !ts.isFunctionLike(ancestor)) {
if (tsutils.isTryStatement(ancestor)) {
return !!ancestor.finallyBlock;
}
ancestor = ancestor.parent;
}
return false;
}

// function findTokensToRemove()

function removeAwait(
Expand Down Expand Up @@ -163,14 +203,22 @@ export default util.createRule({
}

if (option === 'in-try-catch') {
const isInTryCatch = inTryCatch(expression);
const isInTryCatch = inTry(expression) || inCatch(expression);
if (isAwait && !isInTryCatch) {
context.report({
messageId: 'disallowedPromiseAwait',
node,
fix: fixer => removeAwait(fixer, node),
});
} else if (!isAwait && isInTryCatch) {
if (inCatch(expression) && !hasFinallyBlock(expression)) {
return;
}

if (isReturnPromiseInFinally(expression)) {
return;
}

context.report({
messageId: 'requiredPromiseAwait',
node,
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/rules/return-await.test.ts
Expand Up @@ -112,6 +112,32 @@ ruleTester.run('return-await', rule, {
}
`,
},
{
options: ['in-try-catch'],
code: `
async function test() {
try {
throw 'foo';
} catch (e) {
return Promise.resolve(1);
}
}
`,
},
{
options: ['in-try-catch'],
code: `
async function test() {
try {
throw 'foo';
} catch (e) {
throw 'foo2';
} finally {
return Promise.resolve(1);
}
}
`,
},
{
options: ['in-try-catch'],
code: `
Expand Down