Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inlay hints for IndexSignature #56580

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
isIdentifierText,
isImportTypeNode,
isIndexedAccessTypeNode,
isIndexSignatureDeclaration,
isInferTypeNode,
isInfinityOrNaNString,
isIntersectionTypeNode,
Expand Down Expand Up @@ -750,6 +751,17 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
visitForDisplayParts(node.type);
}
break;
case SyntaxKind.IndexSignature:
Debug.assertNode(node, isIndexSignatureDeclaration);
Debug.assertEqual(node.parameters.length, 1);
Copy link
Member

@DanielRosenwasser DanielRosenwasser Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if an assertion is appropriate - what happens if you have something like this?

const x: { []: string }
const y = x;

Copy link
Member

@DanielRosenwasser DanielRosenwasser Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, assertions shouldn't assume well-formed code, they should assert invariants of how the compiler constructs the tree. You'd probably be better off with just visitParametersAndTypeParameters or node.parameters.forEach(visitForDisplayParts).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get An index signature must have exactly one parameter.ts(1096) for that code. Also, the parser doesn't create an index signature node for that (the type literal node just has no member children nodes) so this switch case isn't hit,

That being said, I also wasn't so enthusiastic about using the assert but wasn't sure about alternatives. Your suggestion is better though since this is a user error and we already report diagnostics to the user elsewhere.

I think this should be represented in the types though so we can enforce the invariant. Can we just add something like:

export interface IndexSignatureDeclaration extends SignatureDeclarationBase, ...
{
    ...
    readonly parameters: [ParameterDeclaration] & NodeArray<ParameterDeclaration>;
}

It's probably not that easy since this is a public API and would be breaking.

parts.push({ text: "[" });
visitForDisplayParts(node.parameters[0]);
parts.push({ text: "]" });
if (node.type) {
parts.push({ text: ": " });
visitForDisplayParts(node.type);
}
break;
case SyntaxKind.MethodSignature:
Debug.assertNode(node, isMethodSignature);
if (node.modifiers?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@
{
"text": "; "
},
{
"text": "["
},
{
"text": "i"
},
{
"text": ": "
},
{
"text": "string"
},
{
"text": "]"
},
{
"text": ": "
},
{
"text": "number"
},
{
"text": "; "
},
{
"text": "a"
},
Expand Down Expand Up @@ -464,7 +488,7 @@
"text": "}"
}
],
"position": 646,
"position": 671,
"kind": "Type",
"whitespaceBefore": true
}
Expand All @@ -490,7 +514,7 @@
"text": "42"
}
],
"position": 716,
"position": 741,
"kind": "Type",
"whitespaceBefore": true
}
Expand All @@ -506,13 +530,60 @@ foo4(p => {})
{
"text": "Thing",
"span": {
"start": 735,
"start": 760,
"length": 5
},
"file": "/tests/cases/fourslash/inlayHintsInteractiveFunctionParameterTypes1.ts"
}
],
"position": 801,
"position": 826,
"kind": "Type",
"whitespaceBefore": true
}

const foo5: F4 = (a) => { }
^
{
"text": "",
"displayParts": [
{
"text": ": "
},
{
"text": "{"
},
{
"text": " "
},
{
"text": "["
},
{
"text": "x"
},
{
"text": ": "
},
{
"text": "string"
},
{
"text": "]"
},
{
"text": ": "
},
{
"text": "number"
},
{
"text": " "
},
{
"text": "}"
}
],
"position": 910,
"kind": "Type",
"whitespaceBefore": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//// g<T>(): T
//// h?<X, Y>(x: X): Y
//// <X, Y>(x: X): Y
//// [i: string]: number
//// }) => void
//// const foo5: F2 = (a) => { }

Expand All @@ -41,6 +42,11 @@
////function foo4(callback: (thing: Thing) => void) {}
////foo4(p => {})

//// type F4 = (a: {
//// [i in string]: number
//// }) => void
//// const foo5: F4 = (a) => { }

verify.baselineInlayHints(undefined, {
includeInlayFunctionParameterTypeHints: true,
interactiveInlayHints: true,
Expand Down
Loading