Skip to content

Commit e0d7703

Browse files
authored
fix(43939): forbid converting getters to async/await (microsoft#43944)
1 parent 97f5c62 commit e0d7703

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-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)), canBeConvertedToAsync);
5858
}
5959

6060
if (!functionToConvert) {

src/services/suggestionDiagnostics.ts

Lines changed: 13 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 (canBeConvertedToAsync(node)) {
6161
addConvertToAsyncFunctionDiagnostics(node, checker, diags);
6262
}
6363
node.forEachChild(check);
@@ -213,4 +213,16 @@ namespace ts {
213213

214214
return false;
215215
}
216+
217+
export function canBeConvertedToAsync(node: Node): node is FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction {
218+
switch (node.kind) {
219+
case SyntaxKind.FunctionDeclaration:
220+
case SyntaxKind.MethodDeclaration:
221+
case SyntaxKind.FunctionExpression:
222+
case SyntaxKind.ArrowFunction:
223+
return true;
224+
default:
225+
return false;
226+
}
227+
}
216228
}

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)