Skip to content

Commit 7c197be

Browse files
authored
feat(45210): add inlay hints for getters and setters (microsoft#45214)
1 parent d1d65cb commit 7c197be

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/services/inlayHints.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace ts.InlayHints {
6767
if (preferences.includeInlayFunctionParameterTypeHints && isFunctionExpressionLike(node)) {
6868
visitFunctionExpressionLikeForParameterType(node);
6969
}
70-
if (preferences.includeInlayFunctionLikeReturnTypeHints && isFunctionDeclarationLike(node)) {
70+
if (preferences.includeInlayFunctionLikeReturnTypeHints && isFunctionLikeDeclaration(node)) {
7171
visitFunctionDeclarationLikeForReturnType(node);
7272
}
7373
}
@@ -78,10 +78,6 @@ namespace ts.InlayHints {
7878
return isArrowFunction(node) || isFunctionExpression(node);
7979
}
8080

81-
function isFunctionDeclarationLike(node: Node): node is FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration {
82-
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node);
83-
}
84-
8581
function addParameterHints(text: string, position: number, isFirstVariadicArgument: boolean) {
8682
result.push({
8783
text: `${isFirstVariadicArgument ? "..." : ""}${truncation(text, maxHintsLength)}:`,
@@ -206,7 +202,7 @@ namespace ts.InlayHints {
206202
return isLiteralExpression(node) || isBooleanLiteral(node) || isFunctionExpressionLike(node) || isObjectLiteralExpression(node) || isArrayLiteralExpression(node);
207203
}
208204

209-
function visitFunctionDeclarationLikeForReturnType(decl: ArrowFunction | FunctionExpression | MethodDeclaration | FunctionDeclaration) {
205+
function visitFunctionDeclarationLikeForReturnType(decl: FunctionLikeDeclaration) {
210206
if (isArrowFunction(decl)) {
211207
if (!findChildOfKind(decl, SyntaxKind.OpenParenToken, file)) {
212208
return;
@@ -218,9 +214,7 @@ namespace ts.InlayHints {
218214
return;
219215
}
220216

221-
const type = checker.getTypeAtLocation(decl);
222-
const signatures = checker.getSignaturesOfType(type, SignatureKind.Call);
223-
const signature = firstOrUndefined(signatures);
217+
const signature = checker.getSignatureFromDeclaration(decl);
224218
if (!signature) {
225219
return;
226220
}
@@ -238,7 +232,7 @@ namespace ts.InlayHints {
238232
addTypeHints(typeDisplayString, getTypeAnnotationPosition(decl));
239233
}
240234

241-
function getTypeAnnotationPosition(decl: ArrowFunction | FunctionExpression | MethodDeclaration | FunctionDeclaration) {
235+
function getTypeAnnotationPosition(decl: FunctionLikeDeclaration) {
242236
const closeParenToken = findChildOfKind(decl, SyntaxKind.CloseParenToken, file);
243237
if (closeParenToken) {
244238
return closeParenToken.end;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class Foo {
4+
//// get foo()/*a*/ { return 1; }
5+
//// set foo(value: number)/*b*/ {}
6+
////}
7+
8+
const [a, b] = test.markers();
9+
10+
verify.getInlayHints([
11+
{
12+
text: ': number',
13+
position: a.position,
14+
kind: ts.InlayHintKind.Type,
15+
whitespaceBefore: true
16+
},
17+
{
18+
text: ': void',
19+
position: b.position,
20+
kind: ts.InlayHintKind.Type,
21+
whitespaceBefore: true
22+
},
23+
], undefined, {
24+
includeInlayFunctionLikeReturnTypeHints: true
25+
});

0 commit comments

Comments
 (0)