Skip to content

Commit 158f0b0

Browse files
author
Benjamin Lichtman
committed
Allow codefix to apply to function expression in variable declaration
1 parent cc4e1f8 commit 158f0b0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ namespace ts.codefix {
4242

4343
function convertToAsyncFunction(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker, context: CodeFixContextBase): void {
4444
// get the function declaration - returns a promise
45-
const functionToConvert: FunctionLikeDeclaration = getContainingFunction(getTokenAtPosition(sourceFile, position)) as FunctionLikeDeclaration;
45+
const tokenAtPosition = getTokenAtPosition(sourceFile, position);
46+
let functionToConvert: FunctionLikeDeclaration;
47+
if (isIdentifier(tokenAtPosition) && isVariableDeclaration(tokenAtPosition.parent) &&
48+
tokenAtPosition.parent.initializer && isFunctionLikeDeclaration(tokenAtPosition.parent.initializer)) {
49+
functionToConvert = tokenAtPosition.parent.initializer;
50+
}
51+
else {
52+
functionToConvert = getContainingFunction(getTokenAtPosition(sourceFile, position)) as FunctionLikeDeclaration;
53+
}
54+
4655
if (!functionToConvert) {
4756
return;
4857
}

0 commit comments

Comments
 (0)