Skip to content

Commit 589f734

Browse files
authored
fix(60375): Parameter inlay hint is incorrect when function has a this type (#60378)
1 parent 5170645 commit 589f734

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

Diff for: src/services/inlayHints.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import {
103103
NodeArray,
104104
NodeBuilderFlags,
105105
ParameterDeclaration,
106+
parameterIsThisKeyword,
106107
PrefixUnaryExpression,
107108
PropertyDeclaration,
108109
QuotePreference,
@@ -437,24 +438,26 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
437438
return;
438439
}
439440

440-
for (let i = 0; i < node.parameters.length && i < signature.parameters.length; ++i) {
441-
const param = node.parameters[i];
442-
if (!isHintableDeclaration(param)) {
443-
continue;
441+
let pos = 0;
442+
for (const param of node.parameters) {
443+
if (isHintableDeclaration(param)) {
444+
addParameterTypeHint(param, parameterIsThisKeyword(param) ? signature.thisParameter : signature.parameters[pos]);
444445
}
445-
446-
const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param);
447-
if (effectiveTypeAnnotation) {
446+
if (parameterIsThisKeyword(param)) {
448447
continue;
449448
}
449+
pos++;
450+
}
451+
}
450452

451-
const typeHints = getParameterDeclarationTypeHints(signature.parameters[i]);
452-
if (!typeHints) {
453-
continue;
454-
}
453+
function addParameterTypeHint(node: ParameterDeclaration, symbol: Symbol | undefined) {
454+
const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(node);
455+
if (effectiveTypeAnnotation || symbol === undefined) return;
455456

456-
addTypeHints(typeHints, param.questionToken ? param.questionToken.end : param.name.end);
457-
}
457+
const typeHints = getParameterDeclarationTypeHints(symbol);
458+
if (typeHints === undefined) return;
459+
460+
addTypeHints(typeHints, node.questionToken ? node.questionToken.end : node.name.end);
458461
}
459462

460463
function getParameterDeclarationTypeHints(symbol: Symbol) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// === Inlay Hints ===
2+
fn(function (this, a, b) { });
3+
^
4+
{
5+
"text": ": any",
6+
"position": 126,
7+
"kind": "Type",
8+
"whitespaceBefore": true
9+
}
10+
11+
fn(function (this, a, b) { });
12+
^
13+
{
14+
"text": ": number",
15+
"position": 129,
16+
"kind": "Type",
17+
"whitespaceBefore": true
18+
}
19+
20+
fn(function (this, a, b) { });
21+
^
22+
{
23+
"text": ": string",
24+
"position": 132,
25+
"kind": "Type",
26+
"whitespaceBefore": true
27+
}
28+
29+
fn(function (this: I, a, b) { });
30+
^
31+
{
32+
"text": ": number",
33+
"position": 163,
34+
"kind": "Type",
35+
"whitespaceBefore": true
36+
}
37+
38+
fn(function (this: I, a, b) { });
39+
^
40+
{
41+
"text": ": string",
42+
"position": 166,
43+
"kind": "Type",
44+
"whitespaceBefore": true
45+
}

Diff for: tests/cases/fourslash/inlayHintsThisParameter.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////interface I {
4+
//// a: number;
5+
////}
6+
////
7+
////declare function fn(
8+
//// callback: (a: number, b: string) => void
9+
////): void;
10+
////
11+
////
12+
////fn(function (this, a, b) { });
13+
////fn(function (this: I, a, b) { });
14+
15+
verify.baselineInlayHints(undefined, {
16+
includeInlayFunctionParameterTypeHints: true,
17+
});

0 commit comments

Comments
 (0)