Skip to content

Commit 1fb2b2d

Browse files
authored
fix(47562): Add option to suppress type hint if variable name matches type name (#48529)
* fix(47562): Add option to suppress type hint if variable name matches type * Remove the unnecessary debug code * Re-run gulp runtests * Use equateStringsCaseInsensitive to compare strings
1 parent b57d6e1 commit 1fb2b2d

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8813,6 +8813,7 @@ namespace ts {
88138813
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
88148814
readonly includeInlayFunctionParameterTypeHints?: boolean,
88158815
readonly includeInlayVariableTypeHints?: boolean;
8816+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
88168817
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
88178818
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
88188819
readonly includeInlayEnumMemberValueHints?: boolean;

src/server/protocol.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ namespace ts.server.protocol {
728728
}
729729

730730
// All we need is the `success` and `message` fields of Response.
731-
export interface ApplyCodeActionCommandResponse extends Response {}
731+
export interface ApplyCodeActionCommandResponse extends Response { }
732732

733733
export interface FileRangeRequestArgs extends FileRequestArgs {
734734
/**
@@ -1067,7 +1067,7 @@ namespace ts.server.protocol {
10671067
readonly arguments: JsxClosingTagRequestArgs;
10681068
}
10691069

1070-
export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs {}
1070+
export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs { }
10711071

10721072
export interface JsxClosingTagResponse extends Response {
10731073
readonly body: TextInsertion;
@@ -2390,7 +2390,7 @@ namespace ts.server.protocol {
23902390
/**
23912391
* Human-readable description of the `source` from the CompletionEntry.
23922392
*/
2393-
sourceDisplay?: SymbolDisplayPart[];
2393+
sourceDisplay?: SymbolDisplayPart[];
23942394
}
23952395

23962396
/** @deprecated Prefer CompletionInfoResponse, which supports several top-level fields in addition to the array of entries. */
@@ -3415,7 +3415,7 @@ namespace ts.server.protocol {
34153415
/**
34163416
* Allows completions to be formatted with snippet text, indicated by `CompletionItem["isSnippet"]`.
34173417
*/
3418-
readonly includeCompletionsWithSnippetText?: boolean;
3418+
readonly includeCompletionsWithSnippetText?: boolean;
34193419
/**
34203420
* If enabled, the completion list will include completions with invalid identifier names.
34213421
* For those entries, The `insertText` and `replacementSpan` properties will be set to change from `.x` property access to `["x"]`.
@@ -3465,6 +3465,7 @@ namespace ts.server.protocol {
34653465
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
34663466
readonly includeInlayFunctionParameterTypeHints?: boolean,
34673467
readonly includeInlayVariableTypeHints?: boolean;
3468+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
34683469
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
34693470
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
34703471
readonly includeInlayEnumMemberValueHints?: boolean;

src/services/inlayHints.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ namespace ts.InlayHints {
137137

138138
const typeDisplayString = printTypeInSingleLine(declarationType);
139139
if (typeDisplayString) {
140+
const isVariableNameMatchesType = preferences.includeInlayVariableTypeHintsWhenTypeMatchesName === false && equateStringsCaseInsensitive(decl.name.getText(), typeDisplayString);
141+
if (isVariableNameMatchesType) {
142+
return;
143+
}
140144
addTypeHints(typeDisplayString, decl.name.end);
141145
}
142146
}

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,6 +4120,7 @@ declare namespace ts {
41204120
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
41214121
readonly includeInlayFunctionParameterTypeHints?: boolean;
41224122
readonly includeInlayVariableTypeHints?: boolean;
4123+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
41234124
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
41244125
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
41254126
readonly includeInlayEnumMemberValueHints?: boolean;
@@ -9711,6 +9712,7 @@ declare namespace ts.server.protocol {
97119712
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
97129713
readonly includeInlayFunctionParameterTypeHints?: boolean;
97139714
readonly includeInlayVariableTypeHints?: boolean;
9715+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
97149716
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
97159717
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
97169718
readonly includeInlayEnumMemberValueHints?: boolean;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,6 +4120,7 @@ declare namespace ts {
41204120
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
41214121
readonly includeInlayFunctionParameterTypeHints?: boolean;
41224122
readonly includeInlayVariableTypeHints?: boolean;
4123+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
41234124
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
41244125
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
41254126
readonly includeInlayEnumMemberValueHints?: boolean;

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ declare namespace FourSlashInterface {
666666
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
667667
readonly includeInlayFunctionParameterTypeHints?: boolean;
668668
readonly includeInlayVariableTypeHints?: boolean;
669+
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
669670
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
670671
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
671672
readonly includeInlayEnumMemberValueHints?: boolean;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// type Client = {};
4+
//// function getClient(): Client { return {}; };
5+
//// const client/**/ = getClient();
6+
7+
const markers = test.markers();
8+
9+
verify.getInlayHints([
10+
{
11+
text: ': Client',
12+
position: markers[0].position,
13+
kind: ts.InlayHintKind.Type,
14+
whitespaceBefore: true
15+
}
16+
], undefined, {
17+
includeInlayVariableTypeHints: true,
18+
includeInlayVariableTypeHintsWhenTypeMatchesName: true
19+
});
20+
21+
verify.getInlayHints([], undefined, {
22+
includeInlayVariableTypeHints: true,
23+
includeInlayVariableTypeHintsWhenTypeMatchesName: false
24+
});

0 commit comments

Comments
 (0)