mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
feat(45210): add inlay hints for getters and setters (#45214)
This commit is contained in:
parent
d1d65cb6b1
commit
7c197becb6
@ -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;
|
||||
|
||||
25
tests/cases/fourslash/inlayHintsShouldWork55.ts
Normal file
25
tests/cases/fourslash/inlayHintsShouldWork55.ts
Normal 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
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user