From 9665bc6199bf4c50fb731d152083347652a58318 Mon Sep 17 00:00:00 2001 From: Wenlu Wang Date: Tue, 27 Jul 2021 02:59:14 +0800 Subject: [PATCH] Supress hints for access expr too (#45121) --- src/services/inlayHints.ts | 12 ++++- .../cases/fourslash/inlayHintsShouldWork54.ts | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/inlayHintsShouldWork54.ts diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index d01574941a3..461945679af 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -163,7 +163,7 @@ namespace ts.InlayHints { const identifierNameInfo = checker.getParameterIdentifierNameAtPosition(signature, i); if (identifierNameInfo) { const [parameterName, isFirstVariadicArgument] = identifierNameInfo; - const isParameterNameNotSameAsArgument = preferences.includeInlayParameterNameHintsWhenArgumentMatchesName || !isIdentifier(arg) || arg.text !== parameterName; + const isParameterNameNotSameAsArgument = preferences.includeInlayParameterNameHintsWhenArgumentMatchesName || !identifierOrAccessExpressionPostfixMatchesParameterName(arg, parameterName); if (!isParameterNameNotSameAsArgument && !isFirstVariadicArgument) { continue; } @@ -178,6 +178,16 @@ namespace ts.InlayHints { } } + function identifierOrAccessExpressionPostfixMatchesParameterName(expr: Expression, parameterName: __String) { + if (isIdentifier(expr)) { + return expr.text === parameterName; + } + if (isPropertyAccessExpression(expr)) { + return expr.name.text === parameterName; + } + return false; + } + function leadingCommentsContainsParameterName(node: Node, name: string) { if (!isIdentifierText(name, compilerOptions.target, getLanguageVariant(file.scriptKind))) { return false; diff --git a/tests/cases/fourslash/inlayHintsShouldWork54.ts b/tests/cases/fourslash/inlayHintsShouldWork54.ts new file mode 100644 index 00000000000..addc50c86cb --- /dev/null +++ b/tests/cases/fourslash/inlayHintsShouldWork54.ts @@ -0,0 +1,46 @@ +/// + +//// function foo (a: number, b: number) {} +//// declare const a: 1; +//// foo(a, /*b*/2); +//// declare const v: any; +//// foo(v.a, /*c*/v.a); +//// foo(/*d*/v.b, v.b); +//// foo(/*e*/v.c, /*f*/v.c); + +const markers = test.markers(); +verify.getInlayHints([ + { + text: 'b:', + position: markers[0].position, + kind: ts.InlayHintKind.Parameter, + whitespaceAfter: true + }, + { + text: 'b:', + position: markers[1].position, + kind: ts.InlayHintKind.Parameter, + whitespaceAfter: true + }, + { + text: 'a:', + position: markers[2].position, + kind: ts.InlayHintKind.Parameter, + whitespaceAfter: true + }, + { + text: 'a:', + position: markers[3].position, + kind: ts.InlayHintKind.Parameter, + whitespaceAfter: true + }, + { + text: 'b:', + position: markers[4].position, + kind: ts.InlayHintKind.Parameter, + whitespaceAfter: true + }, +], undefined, { + includeInlayParameterNameHints: "all", + includeInlayParameterNameHintsWhenArgumentMatchesName: false, +});