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] properly handle fixes for TSAsExpression #3631

Merged
merged 2 commits into from Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions packages/eslint-plugin/src/rules/return-await.ts
Expand Up @@ -147,8 +147,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 @@ -827,5 +827,34 @@ 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',
},
],
},
],
});