Skip to content

Commit

Permalink
fix(eslint-plugin): return-await in binary expression
Browse files Browse the repository at this point in the history
  • Loading branch information
islandryu committed Jan 7, 2022
1 parent 8d710a0 commit 4477fb7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/eslint-plugin/src/rules/return-await.ts
Expand Up @@ -4,6 +4,7 @@ import {
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import { isBinaryExpression } from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';

Expand Down Expand Up @@ -154,6 +155,12 @@ export default util.createRule({
fixer: TSESLint.RuleFixer,
node: TSESTree.Expression,
): TSESLint.RuleFix | TSESLint.RuleFix[] {
if (node.parent?.type === AST_NODE_TYPES.LogicalExpression) {
return [
fixer.insertTextBefore(node, '(await '),
fixer.insertTextAfter(node, ')'),
];
}
if (node.type !== AST_NODE_TYPES.TSAsExpression) {
return fixer.insertTextBefore(node, 'await ');
}
Expand Down Expand Up @@ -259,6 +266,22 @@ export default util.createRule({
}
}

function testLogicalExpression(
node: TSESTree.LogicalExpression,
tsNode: ts.BinaryExpression,
): void {
const left = node.left;
const tsLeft = tsNode.left;
const right = node.right;
const tsRight = tsNode.right;
test(right, tsRight);
if (isLogicalExpression(left) && isBinaryExpression(tsLeft)) {
testLogicalExpression(left, tsLeft);
} else {
test(left, tsLeft);
}
}

function findPossiblyReturnedNodes(
node: TSESTree.Expression,
): TSESTree.Expression[] {
Expand Down Expand Up @@ -298,9 +321,19 @@ export default util.createRule({
}
findPossiblyReturnedNodes(node.argument).forEach(node => {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
test(node, tsNode);
if (isLogicalExpression(node) && isBinaryExpression(tsNode)) {
testLogicalExpression(node, tsNode);
} else {
test(node, tsNode);
}
});
},
};
},
});

function isLogicalExpression(
node: TSESTree.Node,
): node is TSESTree.LogicalExpression {
return node.type === AST_NODE_TYPES.LogicalExpression;
}
76 changes: 76 additions & 0 deletions packages/eslint-plugin/tests/rules/return-await.test.ts
Expand Up @@ -909,5 +909,81 @@ async function test<T>(): Promise<T> {
},
],
},
{
code: `
async function bar() {}
async function foo() {
try {
return undefined || bar();
} catch {}
}
`,
output: `
async function bar() {}
async function foo() {
try {
return undefined || (await bar());
} catch {}
}
`,
errors: [
{
line: 5,
messageId: 'requiredPromiseAwait',
},
],
},
{
code: `
async function bar() {}
async function foo() {
try {
return bar() || undefined || bar();
} catch {}
}
`,
output: `
async function bar() {}
async function foo() {
try {
return (await bar()) || undefined || (await bar());
} catch {}
}
`,
errors: [
{
line: 5,
messageId: 'requiredPromiseAwait',
},
{
line: 5,
messageId: 'requiredPromiseAwait',
},
],
},
{
code: `
async function bar() {}
async function foo() {
try {
return window.foo ?? bar();
} catch {}
}
`,
output: `
async function bar() {}
async function foo() {
try {
return window.foo ?? (await bar());
} catch {}
}
`,
errors: [
{
line: 5,
messageId: 'requiredPromiseAwait',
},
],
},
],
});

0 comments on commit 4477fb7

Please sign in to comment.