diff --git a/src/services/refactors/inferFunctionReturnType.ts b/src/services/refactors/inferFunctionReturnType.ts index d4f4a5c871cfc..dc3a9adbd0d7f 100644 --- a/src/services/refactors/inferFunctionReturnType.ts +++ b/src/services/refactors/inferFunctionReturnType.ts @@ -10,7 +10,7 @@ import { FunctionDeclaration, FunctionExpression, getLocaleSpecificMessage, - getTokenAtPosition, + getTouchingPropertyName, isArrowFunction, isBlock, isInJSFile, @@ -104,7 +104,7 @@ function doChange(sourceFile: SourceFile, changes: textChanges.ChangeTracker, de function getInfo(context: RefactorContext): FunctionInfo | RefactorErrorInfo | undefined { if (isInJSFile(context.file) || !refactorKindBeginsWith(inferReturnTypeAction.kind, context.kind)) return; - const token = getTokenAtPosition(context.file, context.startPosition); + const token = getTouchingPropertyName(context.file, context.startPosition); const declaration = findAncestor(token, n => isBlock(n) || n.parent && isArrowFunction(n.parent) && (n.kind === SyntaxKind.EqualsGreaterThanToken || n.parent.body === n) ? "quit" : isConvertibleDeclaration(n)) as ConvertibleDeclaration | undefined; diff --git a/tests/cases/fourslash/refactorInferFunctionReturnType24.ts b/tests/cases/fourslash/refactorInferFunctionReturnType24.ts new file mode 100644 index 0000000000000..69c58e14cbcb6 --- /dev/null +++ b/tests/cases/fourslash/refactorInferFunctionReturnType24.ts @@ -0,0 +1,10 @@ +/// + +////let x = 10;/*a*//*b*/ +//// +////function func() { +//// return 10; +////} + +goTo.select("a", "b"); +verify.not.refactorAvailable("Infer function return type");