Skip to content

Commit

Permalink
fix(eslint-plugin): [return-await] properly handle fixes for `TSAsExp…
Browse files Browse the repository at this point in the history
…ression` (#3631)
  • Loading branch information
rafaelss95 committed Aug 1, 2021
1 parent da3511d commit 00a4369
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/eslint-plugin/src/rules/return-await.ts
Expand Up @@ -153,8 +153,15 @@ export default util.createRule({
function insertAwait(
fixer: TSESLint.RuleFixer,
node: TSESTree.Expression,
): TSESLint.RuleFix | null {
return fixer.insertTextBefore(node, 'await ');
): TSESLint.RuleFix | TSESLint.RuleFix[] {
if (node.type !== AST_NODE_TYPES.TSAsExpression) {
return fixer.insertTextBefore(node, 'await ');
}

return [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfter(node, ')'),
];
}

function test(node: TSESTree.Expression, expression: ts.Node): void {
Expand Down
29 changes: 29 additions & 0 deletions packages/eslint-plugin/tests/rules/return-await.test.ts
Expand Up @@ -842,6 +842,35 @@ const buzz = async () => ((await foo()) ? 1 : await bar());
},
],
},
{
// https://github.com/typescript-eslint/typescript-eslint/issues/2109
code: `
async function test<T>(): Promise<T> {
const res = await fetch('...');
try {
return res.json() as Promise<T>;
} catch (err) {
throw Error('Request Failed.');
}
}
`,
output: `
async function test<T>(): Promise<T> {
const res = await fetch('...');
try {
return await (res.json() as Promise<T>);
} catch (err) {
throw Error('Request Failed.');
}
}
`,
errors: [
{
line: 5,
messageId: 'requiredPromiseAwait',
},
],
},
{
code: `
async function test() {
Expand Down

0 comments on commit 00a4369

Please sign in to comment.