diff --git a/tsc/internal/fourslash/tests/callHierarchyInterfaceMethod_test.go b/tsc/internal/fourslash/tests/callHierarchyInterfaceMethod_test.go index 3917092b3507d..ac082a27c8223 100644 --- a/tsc/internal/fourslash/tests/callHierarchyInterfaceMethod_test.go +++ b/tsc/internal/fourslash/tests/callHierarchyInterfaceMethod_test.go @@ -22,3 +22,14 @@ obj.foo();` f.GoToMarker(t, "") f.VerifyBaselineCallHierarchy(t) } + +func TestCallHierarchyOutgoingInterfaceMethod(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `interface G { m(): void; } +function /**/probe(g: G) { g.m(); }` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.GoToMarker(t, "") + f.VerifyBaselineCallHierarchy(t) +} diff --git a/tsc/internal/ls/callhierarchy.go b/tsc/internal/ls/callhierarchy.go index 140e2da205b20..d39a693fab467 100644 --- a/tsc/internal/ls/callhierarchy.go +++ b/tsc/internal/ls/callhierarchy.go @@ -622,6 +622,7 @@ func (l *LanguageService) convertCallSiteGroupToIncomingCall(program *compiler.P } slices.SortFunc(fromRanges, lsproto.CompareRanges) + fromRanges = slices.Compact(fromRanges) return &lsproto.CallHierarchyIncomingCall{ From: from, @@ -805,6 +806,25 @@ func (c *callSiteCollector) recordCallSite(node *ast.Node) { } } +func isCallSiteTarget(node *ast.Node) bool { + parent := node.Parent + if parent == nil { + return false + } + + switch { + case ast.IsCallOrNewExpression(parent): + return parent.Expression() == node + case ast.IsTaggedTemplateExpression(parent): + return parent.AsTaggedTemplateExpression().Tag == node + case ast.IsDecorator(parent): + return parent.Expression() == node + case ast.IsJsxOpeningElement(parent), ast.IsJsxSelfClosingElement(parent): + return parent.TagName() == node + } + return false +} + func (c *callSiteCollector) collect(node *ast.Node) { if node == nil { return @@ -882,7 +902,9 @@ func (c *callSiteCollector) collect(node *ast.Node) { c.collect(node.Expression()) return case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression: - c.recordCallSite(node) + if !isCallSiteTarget(node) { + c.recordCallSite(node) + } node.ForEachChild(func(child *ast.Node) bool { c.collect(child) return false @@ -992,6 +1014,7 @@ func (l *LanguageService) convertCallSiteGroupToOutgoingCall(program *compiler.P } slices.SortFunc(fromRanges, lsproto.CompareRanges) + fromRanges = slices.Compact(fromRanges) return &lsproto.CallHierarchyOutgoingCall{ To: to, diff --git a/tsc/testdata/baselines/reference/fourslash/callHierarchy/callHierarchyOutgoingInterfaceMethod.callHierarchy.txt b/tsc/testdata/baselines/reference/fourslash/callHierarchy/callHierarchyOutgoingInterfaceMethod.callHierarchy.txt new file mode 100644 index 0000000000000..e380dc6b110fd --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/callHierarchy/callHierarchyOutgoingInterfaceMethod.callHierarchy.txt @@ -0,0 +1,36 @@ +// === Call Hierarchy === +╭ name: probe +├ kind: function +├ file: /callHierarchyOutgoingInterfaceMethod.ts +├ span: +│ ╭ /callHierarchyOutgoingInterfaceMethod.ts:2:1-2:32 +│ │ 2: function probe(g: G) { g.m(); } +│ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +│ ╰ +├ selectionSpan: +│ ╭ /callHierarchyOutgoingInterfaceMethod.ts:2:10-2:15 +│ │ 2: function probe(g: G) { g.m(); } +│ │ ^^^^^ +│ ╰ +├ incoming: none +├ outgoing: +│ ╭ to: +│ │ ╭ name: m +│ │ ├ kind: method +│ │ ├ file: /callHierarchyOutgoingInterfaceMethod.ts +│ │ ├ span: +│ │ │ ╭ /callHierarchyOutgoingInterfaceMethod.ts:1:15-1:25 +│ │ │ │ 1: interface G { m(): void; } +│ │ │ │ ^^^^^^^^^^ +│ │ │ ╰ +│ │ ├ selectionSpan: +│ │ │ ╭ /callHierarchyOutgoingInterfaceMethod.ts:1:15-1:16 +│ │ │ │ 1: interface G { m(): void; } +│ │ │ │ ^ +│ │ │ ╰ +│ │ ╰ outgoing: none +│ ├ fromSpans: +│ │ ╭ /callHierarchyOutgoingInterfaceMethod.ts:2:24-2:27 +│ │ │ 2: function probe(g: G) { g.m(); } +│ │ │ ^^^ +╰ ╰ ╰ \ No newline at end of file