Skip to content

fix(43939): Quick fix shouldn't be available for converting getters to async await #43944

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

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/services/codefixes/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace ts.codefix {
functionToConvert = tokenAtPosition.parent.initializer;
}
else {
functionToConvert = tryCast(getContainingFunction(getTokenAtPosition(sourceFile, position)), isFunctionLikeDeclaration);
functionToConvert = tryCast(getContainingFunction(getTokenAtPosition(sourceFile, position)), canBeConvertedToAsync);
}

if (!functionToConvert) {
Expand Down
14 changes: 13 additions & 1 deletion src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace ts {
}
}

if (isFunctionLikeDeclaration(node)) {
if (canBeConvertedToAsync(node)) {
addConvertToAsyncFunctionDiagnostics(node, checker, diags);
}
node.forEachChild(check);
Expand Down Expand Up @@ -213,4 +213,16 @@ namespace ts {

return false;
}

export function canBeConvertedToAsync(node: Node): node is FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction {
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return true;
default:
return false;
}
}
}
8 changes: 8 additions & 0 deletions src/testRunner/unittests/services/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,14 @@ function [#|f|](x?: number): Promise<void> | number {
if (x) return x;
return fetch('').then(() => {});
}
`);

_testConvertToAsyncFunctionFailed("convertToAsyncFunction__NoSuggestionInGetters", `
class Foo {
get [#|m|](): Promise<number> {
return Promise.resolve(1).then(n => n);
}
}
`);

});
Expand Down