Skip to content

Commit

Permalink
fix(47562): Add option to suppress type hint if variable name matches…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
huytd committed May 25, 2022
1 parent b57d6e1 commit 1fb2b2d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -8813,6 +8813,7 @@ namespace ts {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean,
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down
9 changes: 5 additions & 4 deletions src/server/protocol.ts
Expand Up @@ -728,7 +728,7 @@ namespace ts.server.protocol {
}

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

export interface FileRangeRequestArgs extends FileRequestArgs {
/**
Expand Down Expand Up @@ -1067,7 +1067,7 @@ namespace ts.server.protocol {
readonly arguments: JsxClosingTagRequestArgs;
}

export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs {}
export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs { }

export interface JsxClosingTagResponse extends Response {
readonly body: TextInsertion;
Expand Down Expand Up @@ -2390,7 +2390,7 @@ namespace ts.server.protocol {
/**
* Human-readable description of the `source` from the CompletionEntry.
*/
sourceDisplay?: SymbolDisplayPart[];
sourceDisplay?: SymbolDisplayPart[];
}

/** @deprecated Prefer CompletionInfoResponse, which supports several top-level fields in addition to the array of entries. */
Expand Down Expand Up @@ -3415,7 +3415,7 @@ namespace ts.server.protocol {
/**
* Allows completions to be formatted with snippet text, indicated by `CompletionItem["isSnippet"]`.
*/
readonly includeCompletionsWithSnippetText?: boolean;
readonly includeCompletionsWithSnippetText?: boolean;
/**
* If enabled, the completion list will include completions with invalid identifier names.
* For those entries, The `insertText` and `replacementSpan` properties will be set to change from `.x` property access to `["x"]`.
Expand Down Expand Up @@ -3465,6 +3465,7 @@ namespace ts.server.protocol {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean,
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/services/inlayHints.ts
Expand Up @@ -137,6 +137,10 @@ namespace ts.InlayHints {

const typeDisplayString = printTypeInSingleLine(declarationType);
if (typeDisplayString) {
const isVariableNameMatchesType = preferences.includeInlayVariableTypeHintsWhenTypeMatchesName === false && equateStringsCaseInsensitive(decl.name.getText(), typeDisplayString);
if (isVariableNameMatchesType) {
return;
}
addTypeHints(typeDisplayString, decl.name.end);
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -4120,6 +4120,7 @@ declare namespace ts {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean;
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down Expand Up @@ -9711,6 +9712,7 @@ declare namespace ts.server.protocol {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean;
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -4120,6 +4120,7 @@ declare namespace ts {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean;
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Expand Up @@ -666,6 +666,7 @@ declare namespace FourSlashInterface {
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean;
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/inlayHintsShouldWork67.ts
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />

//// type Client = {};
//// function getClient(): Client { return {}; };
//// const client/**/ = getClient();

const markers = test.markers();

verify.getInlayHints([
{
text: ': Client',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
}
], undefined, {
includeInlayVariableTypeHints: true,
includeInlayVariableTypeHintsWhenTypeMatchesName: true
});

verify.getInlayHints([], undefined, {
includeInlayVariableTypeHints: true,
includeInlayVariableTypeHintsWhenTypeMatchesName: false
});

0 comments on commit 1fb2b2d

Please sign in to comment.