Skip to content

Commit 00a4369

Browse files
authored
fix(eslint-plugin): [return-await] properly handle fixes for TSAsExpression (typescript-eslint#3631)
1 parent da3511d commit 00a4369

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/eslint-plugin/src/rules/return-await.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,15 @@ export default util.createRule({
153153
function insertAwait(
154154
fixer: TSESLint.RuleFixer,
155155
node: TSESTree.Expression,
156-
): TSESLint.RuleFix | null {
157-
return fixer.insertTextBefore(node, 'await ');
156+
): TSESLint.RuleFix | TSESLint.RuleFix[] {
157+
if (node.type !== AST_NODE_TYPES.TSAsExpression) {
158+
return fixer.insertTextBefore(node, 'await ');
159+
}
160+
161+
return [
162+
fixer.insertTextBefore(node, 'await ('),
163+
fixer.insertTextAfter(node, ')'),
164+
];
158165
}
159166

160167
function test(node: TSESTree.Expression, expression: ts.Node): void {

packages/eslint-plugin/tests/rules/return-await.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,35 @@ const buzz = async () => ((await foo()) ? 1 : await bar());
842842
},
843843
],
844844
},
845+
{
846+
// https://github.com/typescript-eslint/typescript-eslint/issues/2109
847+
code: `
848+
async function test<T>(): Promise<T> {
849+
const res = await fetch('...');
850+
try {
851+
return res.json() as Promise<T>;
852+
} catch (err) {
853+
throw Error('Request Failed.');
854+
}
855+
}
856+
`,
857+
output: `
858+
async function test<T>(): Promise<T> {
859+
const res = await fetch('...');
860+
try {
861+
return await (res.json() as Promise<T>);
862+
} catch (err) {
863+
throw Error('Request Failed.');
864+
}
865+
}
866+
`,
867+
errors: [
868+
{
869+
line: 5,
870+
messageId: 'requiredPromiseAwait',
871+
},
872+
],
873+
},
845874
{
846875
code: `
847876
async function test() {

0 commit comments

Comments
 (0)