Skip to content

Commit 39c653c

Browse files
authored
fix: omit inlay hints for setters (#45228)
1 parent 975aabe commit 39c653c

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/services/inlayHints.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ namespace ts.InlayHints {
6767
if (preferences.includeInlayFunctionParameterTypeHints && isFunctionExpressionLike(node)) {
6868
visitFunctionExpressionLikeForParameterType(node);
6969
}
70-
if (preferences.includeInlayFunctionLikeReturnTypeHints && isFunctionLikeDeclaration(node)) {
70+
if (preferences.includeInlayFunctionLikeReturnTypeHints && isSignatureSupportingReturnAnnotation(node)) {
7171
visitFunctionDeclarationLikeForReturnType(node);
7272
}
7373
}
7474
return forEachChild(node, visitor);
7575
}
7676

77+
function isSignatureSupportingReturnAnnotation(node: Node): node is FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration | GetAccessorDeclaration {
78+
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
79+
}
80+
7781
function isFunctionExpressionLike(node: Node): node is ArrowFunction | FunctionExpression {
7882
return isArrowFunction(node) || isFunctionExpression(node);
7983
}
@@ -206,7 +210,7 @@ namespace ts.InlayHints {
206210
return isLiteralExpression(node) || isBooleanLiteral(node) || isFunctionExpressionLike(node) || isObjectLiteralExpression(node) || isArrayLiteralExpression(node);
207211
}
208212

209-
function visitFunctionDeclarationLikeForReturnType(decl: FunctionLikeDeclaration) {
213+
function visitFunctionDeclarationLikeForReturnType(decl: FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration | GetAccessorDeclaration) {
210214
if (isArrowFunction(decl)) {
211215
if (!findChildOfKind(decl, SyntaxKind.OpenParenToken, file)) {
212216
return;
@@ -236,7 +240,7 @@ namespace ts.InlayHints {
236240
addTypeHints(typeDisplayString, getTypeAnnotationPosition(decl));
237241
}
238242

239-
function getTypeAnnotationPosition(decl: FunctionLikeDeclaration) {
243+
function getTypeAnnotationPosition(decl: FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration | GetAccessorDeclaration) {
240244
const closeParenToken = findChildOfKind(decl, SyntaxKind.CloseParenToken, file);
241245
if (closeParenToken) {
242246
return closeParenToken.end;

tests/cases/fourslash/inlayHintsShouldWork55.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
/// <reference path="fourslash.ts" />
22

33
////class Foo {
4-
//// get foo()/*a*/ { return 1; }
5-
//// set foo(value: number)/*b*/ {}
4+
//// get foo()/**/ { return 1; }
65
////}
76

8-
const [a, b] = test.markers();
7+
const [marker] = test.markers();
98

109
verify.getInlayHints([
1110
{
1211
text: ': number',
13-
position: a.position,
14-
kind: ts.InlayHintKind.Type,
15-
whitespaceBefore: true
16-
},
17-
{
18-
text: ': void',
19-
position: b.position,
12+
position: marker.position,
2013
kind: ts.InlayHintKind.Type,
2114
whitespaceBefore: true
2215
},
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class Foo {
4+
//// set foo(value: number)/**/ {}
5+
////}
6+
7+
verify.getInlayHints([], undefined, {
8+
includeInlayFunctionLikeReturnTypeHints: true
9+
});

0 commit comments

Comments
 (0)