Skip to content

Commit

Permalink
fix(60375): Parameter inlay hint is incorrect when function has a thi…
Browse files Browse the repository at this point in the history
…s type (#60378)
  • Loading branch information
a-tarasyuk authored Jan 17, 2025
1 parent 5170645 commit 589f734
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import {
NodeArray,
NodeBuilderFlags,
ParameterDeclaration,
parameterIsThisKeyword,
PrefixUnaryExpression,
PropertyDeclaration,
QuotePreference,
Expand Down Expand Up @@ -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]);
}

const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param);
if (effectiveTypeAnnotation) {
if (parameterIsThisKeyword(param)) {
continue;
}
pos++;
}
}

const typeHints = getParameterDeclarationTypeHints(signature.parameters[i]);
if (!typeHints) {
continue;
}
function addParameterTypeHint(node: ParameterDeclaration, symbol: Symbol | undefined) {
const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(node);
if (effectiveTypeAnnotation || symbol === undefined) return;

addTypeHints(typeHints, param.questionToken ? param.questionToken.end : param.name.end);
}
const typeHints = getParameterDeclarationTypeHints(symbol);
if (typeHints === undefined) return;

addTypeHints(typeHints, node.questionToken ? node.questionToken.end : node.name.end);
}

function getParameterDeclarationTypeHints(symbol: Symbol) {
Expand Down
45 changes: 45 additions & 0 deletions tests/baselines/reference/inlayHintsThisParameter.baseline
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions tests/cases/fourslash/inlayHintsThisParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

////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,
});

0 comments on commit 589f734

Please sign in to comment.