feat(45210): add inlay hints for getters and setters (#45214)

This commit is contained in:
Oleksandr T 2021-07-29 00:52:37 +03:00 committed by GitHub
parent d1d65cb6b1
commit 7c197becb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 10 deletions

View File

@ -67,7 +67,7 @@ namespace ts.InlayHints {
if (preferences.includeInlayFunctionParameterTypeHints && isFunctionExpressionLike(node)) {
visitFunctionExpressionLikeForParameterType(node);
}
if (preferences.includeInlayFunctionLikeReturnTypeHints && isFunctionDeclarationLike(node)) {
if (preferences.includeInlayFunctionLikeReturnTypeHints && isFunctionLikeDeclaration(node)) {
visitFunctionDeclarationLikeForReturnType(node);
}
}
@ -78,10 +78,6 @@ namespace ts.InlayHints {
return isArrowFunction(node) || isFunctionExpression(node);
}
function isFunctionDeclarationLike(node: Node): node is FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration {
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node);
}
function addParameterHints(text: string, position: number, isFirstVariadicArgument: boolean) {
result.push({
text: `${isFirstVariadicArgument ? "..." : ""}${truncation(text, maxHintsLength)}:`,
@ -206,7 +202,7 @@ namespace ts.InlayHints {
return isLiteralExpression(node) || isBooleanLiteral(node) || isFunctionExpressionLike(node) || isObjectLiteralExpression(node) || isArrayLiteralExpression(node);
}
function visitFunctionDeclarationLikeForReturnType(decl: ArrowFunction | FunctionExpression | MethodDeclaration | FunctionDeclaration) {
function visitFunctionDeclarationLikeForReturnType(decl: FunctionLikeDeclaration) {
if (isArrowFunction(decl)) {
if (!findChildOfKind(decl, SyntaxKind.OpenParenToken, file)) {
return;
@ -218,9 +214,7 @@ namespace ts.InlayHints {
return;
}
const type = checker.getTypeAtLocation(decl);
const signatures = checker.getSignaturesOfType(type, SignatureKind.Call);
const signature = firstOrUndefined(signatures);
const signature = checker.getSignatureFromDeclaration(decl);
if (!signature) {
return;
}
@ -238,7 +232,7 @@ namespace ts.InlayHints {
addTypeHints(typeDisplayString, getTypeAnnotationPosition(decl));
}
function getTypeAnnotationPosition(decl: ArrowFunction | FunctionExpression | MethodDeclaration | FunctionDeclaration) {
function getTypeAnnotationPosition(decl: FunctionLikeDeclaration) {
const closeParenToken = findChildOfKind(decl, SyntaxKind.CloseParenToken, file);
if (closeParenToken) {
return closeParenToken.end;

View File

@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />
////class Foo {
//// get foo()/*a*/ { return 1; }
//// set foo(value: number)/*b*/ {}
////}
const [a, b] = test.markers();
verify.getInlayHints([
{
text: ': number',
position: a.position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': void',
position: b.position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
includeInlayFunctionLikeReturnTypeHints: true
});