From 1625ce570bfbc55eeb30c58c4f3af2112faad503 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Wed, 30 Oct 2024 23:26:34 +0200 Subject: [PATCH] fix(60375): adjust parameter positions when an explicit this declaration is present --- src/services/inlayHints.ts | 31 +++++++------ .../inlayHintsThisParameter.baseline | 45 +++++++++++++++++++ .../fourslash/inlayHintsThisParameter.ts | 17 +++++++ 3 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 tests/baselines/reference/inlayHintsThisParameter.baseline create mode 100644 tests/cases/fourslash/inlayHintsThisParameter.ts diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index f5a29b332b595..5c5aa9ef87b94 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -103,6 +103,7 @@ import { NodeArray, NodeBuilderFlags, ParameterDeclaration, + parameterIsThisKeyword, PrefixUnaryExpression, PropertyDeclaration, QuotePreference, @@ -437,24 +438,26 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] { return; } - for (let i = 0; i < node.parameters.length && i < signature.parameters.length; ++i) { - const param = node.parameters[i]; - if (!isHintableDeclaration(param)) { - continue; + let pos = 0; + for (const param of node.parameters) { + if (isHintableDeclaration(param)) { + addParameterTypeHint(param, parameterIsThisKeyword(param) ? signature.thisParameter : signature.parameters[pos]); + if (parameterIsThisKeyword(param)) { + continue; + } } + pos++; + } + } - const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param); - if (effectiveTypeAnnotation) { - continue; - } + function addParameterTypeHint(node: ParameterDeclaration, symbol: Symbol | undefined) { + const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(node); + if (effectiveTypeAnnotation || symbol === undefined) return; - const typeHints = getParameterDeclarationTypeHints(signature.parameters[i]); - if (!typeHints) { - continue; - } + const typeHints = getParameterDeclarationTypeHints(symbol); + if (typeHints === undefined) return; - addTypeHints(typeHints, param.questionToken ? param.questionToken.end : param.name.end); - } + addTypeHints(typeHints, node.questionToken ? node.questionToken.end : node.name.end); } function getParameterDeclarationTypeHints(symbol: Symbol) { diff --git a/tests/baselines/reference/inlayHintsThisParameter.baseline b/tests/baselines/reference/inlayHintsThisParameter.baseline new file mode 100644 index 0000000000000..d41eb76410e93 --- /dev/null +++ b/tests/baselines/reference/inlayHintsThisParameter.baseline @@ -0,0 +1,45 @@ +// === Inlay Hints === +fn(function (this, a, b) { }); + ^ +{ + "text": ": any", + "position": 126, + "kind": "Type", + "whitespaceBefore": true +} + +fn(function (this, a, b) { }); + ^ +{ + "text": ": number", + "position": 129, + "kind": "Type", + "whitespaceBefore": true +} + +fn(function (this, a, b) { }); + ^ +{ + "text": ": string", + "position": 132, + "kind": "Type", + "whitespaceBefore": true +} + +fn(function (this: I, a, b) { }); + ^ +{ + "text": ": number", + "position": 163, + "kind": "Type", + "whitespaceBefore": true +} + +fn(function (this: I, a, b) { }); + ^ +{ + "text": ": string", + "position": 166, + "kind": "Type", + "whitespaceBefore": true +} \ No newline at end of file diff --git a/tests/cases/fourslash/inlayHintsThisParameter.ts b/tests/cases/fourslash/inlayHintsThisParameter.ts new file mode 100644 index 0000000000000..dd4e181b1936b --- /dev/null +++ b/tests/cases/fourslash/inlayHintsThisParameter.ts @@ -0,0 +1,17 @@ +/// + +////interface I { +//// a: number; +////} +//// +////declare function fn( +//// callback: (a: number, b: string) => void +////): void; +//// +//// +////fn(function (this, a, b) { }); +////fn(function (this: I, a, b) { }); + +verify.baselineInlayHints(undefined, { + includeInlayFunctionParameterTypeHints: true, +});