Skip to content

Commit 0ff12ab

Browse files
committed
fix(43939): forbid converting getters to async/await
1 parent f7ef154 commit 0ff12ab

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace ts.codefix {
5454
functionToConvert = tokenAtPosition.parent.initializer;
5555
}
5656
else {
57-
functionToConvert = tryCast(getContainingFunction(getTokenAtPosition(sourceFile, position)), isFunctionLikeDeclaration);
57+
functionToConvert = tryCast(getContainingFunction(getTokenAtPosition(sourceFile, position)), canBeConvertedToAsyncFunction);
5858
}
5959

6060
if (!functionToConvert) {

src/services/suggestionDiagnostics.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts {
5757
}
5858
}
5959

60-
if (isFunctionLikeDeclaration(node)) {
60+
if (canBeConvertedToAsyncFunction(node)) {
6161
addConvertToAsyncFunctionDiagnostics(node, checker, diags);
6262
}
6363
node.forEachChild(check);
@@ -213,4 +213,8 @@ namespace ts {
213213

214214
return false;
215215
}
216+
217+
export function canBeConvertedToAsyncFunction(node: Node): node is Exclude<FunctionLikeDeclaration, GetAccessorDeclaration> {
218+
return isFunctionLikeDeclaration(node) && !isGetAccessorDeclaration(node);
219+
}
216220
}

src/testRunner/unittests/services/convertToAsyncFunction.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,14 @@ function [#|f|](x?: number): Promise<void> | number {
16981698
if (x) return x;
16991699
return fetch('').then(() => {});
17001700
}
1701+
`);
1702+
1703+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction__NoSuggestionInGetters", `
1704+
class Foo {
1705+
get [#|m|](): Promise<number> {
1706+
return Promise.resolve(1).then(n => n);
1707+
}
1708+
}
17011709
`);
17021710

17031711
});

0 commit comments

Comments
 (0)