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
This commit is contained in:
Huy 2022-05-25 12:07:38 -07:00 committed by GitHub
parent b57d6e1df4
commit 1fb2b2d70f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 4 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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
});