Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 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]);
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) {
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,
});