Skip to content

Commit 61246af

Browse files
Use information from contextual type in hovers/quick info (#2073)
1 parent 16aa7b5 commit 61246af

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

internal/fourslash/_scripts/failingTests.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ TestQuickInfoForTypeParameterInTypeAlias1
454454
TestQuickInfoForTypeParameterInTypeAlias2
455455
TestQuickInfoForTypeofParameter
456456
TestQuickInfoForUMDModuleAlias
457-
TestQuickInfoFromContextualType
458457
TestQuickInfoFunctionKeyword
459458
TestQuickInfoGenerics
460459
TestQuickInfoGetterSetter
@@ -471,7 +470,6 @@ TestQuickInfoJsDocNonDiscriminatedUnionSharedProp
471470
TestQuickInfoJsdocTypedefMissingType
472471
TestQuickInfoMappedSpreadTypes
473472
TestQuickInfoMappedType
474-
TestQuickInfoMappedTypeMethods
475473
TestQuickInfoMappedTypeRecursiveInference
476474
TestQuickInfoModuleVariables
477475
TestQuickInfoNarrowedTypeOfAliasSymbol
@@ -526,7 +524,6 @@ TestQuickInfoTypeError
526524
TestQuickInfoTypeOfThisInStatics
527525
TestQuickInfoTypeOnlyNamespaceAndClass
528526
TestQuickInfoUnionOfNamespaces
529-
TestQuickInfoUnion_discriminated
530527
TestQuickInfoWidenedTypes
531528
TestQuickInfo_notInsideComment
532529
TestQuickInforForSucessiveInferencesIsNotAny

internal/fourslash/tests/gen/quickInfoFromContextualType_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestQuickInfoFromContextualType(t *testing.T) {
1111
t.Parallel()
12-
t.Skip()
12+
1313
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1414
const content = `// @Filename: quickInfoExportAssignmentOfGenericInterface_0.ts
1515
interface I {

internal/fourslash/tests/gen/quickInfoMappedTypeMethods_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestQuickInfoMappedTypeMethods(t *testing.T) {
1111
t.Parallel()
12-
t.Skip()
12+
1313
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1414
const content = `type M = { [K in 'one']: any };
1515
const x: M = {

internal/fourslash/tests/gen/quickInfoUnion_discriminated_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestQuickInfoUnion_discriminated(t *testing.T) {
1111
t.Parallel()
12-
t.Skip()
12+
1313
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1414
const content = `// @Filename: quickInfoJsDocTags.ts
1515
type U = A | B;

internal/ls/hover.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.
2828
c, done := program.GetTypeCheckerForFile(ctx, file)
2929
defer done()
3030
rangeNode := getNodeForQuickInfo(node)
31-
quickInfo, documentation := l.getQuickInfoAndDocumentationForSymbol(c, c.GetSymbolAtLocation(node), rangeNode, contentFormat)
31+
symbol := getSymbolAtLocationForQuickInfo(c, node)
32+
quickInfo, documentation := l.getQuickInfoAndDocumentationForSymbol(c, symbol, rangeNode, contentFormat)
3233
if quickInfo == "" {
3334
return lsproto.HoverOrNull{}, nil
3435
}
@@ -282,6 +283,17 @@ func getNodeForQuickInfo(node *ast.Node) *ast.Node {
282283
return node
283284
}
284285

286+
func getSymbolAtLocationForQuickInfo(c *checker.Checker, node *ast.Node) *ast.Symbol {
287+
if objectElement := getContainingObjectLiteralElement(node); objectElement != nil {
288+
if contextualType := c.GetContextualType(objectElement.Parent, checker.ContextFlagsNone); contextualType != nil {
289+
if properties := c.GetPropertySymbolsFromContextualType(objectElement, contextualType, false /*unionSymbolOk*/); len(properties) == 1 {
290+
return properties[0]
291+
}
292+
}
293+
}
294+
return c.GetSymbolAtLocation(node)
295+
}
296+
285297
func inConstructorContext(node *ast.Node) bool {
286298
if node.Kind == ast.KindConstructorKeyword {
287299
return true

0 commit comments

Comments
 (0)