From 1fb2b2d70fcd97c14a4fa1c74c28cf53497fc205 Mon Sep 17 00:00:00 2001 From: Huy Date: Wed, 25 May 2022 12:07:38 -0700 Subject: [PATCH] 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 --- src/compiler/types.ts | 1 + src/server/protocol.ts | 9 +++---- src/services/inlayHints.ts | 4 ++++ .../reference/api/tsserverlibrary.d.ts | 2 ++ tests/baselines/reference/api/typescript.d.ts | 1 + tests/cases/fourslash/fourslash.ts | 1 + .../cases/fourslash/inlayHintsShouldWork67.ts | 24 +++++++++++++++++++ 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/inlayHintsShouldWork67.ts diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 714112664a100..41162d9b3e491 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -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; diff --git a/src/server/protocol.ts b/src/server/protocol.ts index 121b69895f947..623699de435d6 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -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 { /** @@ -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; @@ -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. */ @@ -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"]`. @@ -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; diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index 110e9718405cb..ca95df202b810 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -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); } } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 97b3a54182090..ab7ae0cde4997 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -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; @@ -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; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 2b3ffba10cb89..658786b63f18c 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -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; diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 79e0f52cb5882..ea690be401430 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -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; diff --git a/tests/cases/fourslash/inlayHintsShouldWork67.ts b/tests/cases/fourslash/inlayHintsShouldWork67.ts new file mode 100644 index 0000000000000..2b35b152b7956 --- /dev/null +++ b/tests/cases/fourslash/inlayHintsShouldWork67.ts @@ -0,0 +1,24 @@ +/// + +//// 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 +});