Skip to content

Fix wrong script info used to resolve line/offset for call hierarchy #36559

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

Merged
merged 1 commit into from
Jan 31, 2020
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
36 changes: 16 additions & 20 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2164,15 +2164,18 @@ namespace ts.server {
return result;
}

private toProtocolCallHierarchyItem(item: CallHierarchyItem, scriptInfo?: ScriptInfo): protocol.CallHierarchyItem {
private getScriptInfoFromProjectService(file: string) {
const normalizedFile = toNormalizedPath(file);
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(normalizedFile);
if (!scriptInfo) {
const file = toNormalizedPath(item.file);
scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
if (!scriptInfo) {
this.projectService.logErrorForScriptInfoNotFound(file);
return Errors.ThrowNoProject();
}
this.projectService.logErrorForScriptInfoNotFound(normalizedFile);
return Errors.ThrowNoProject();
}
return scriptInfo;
}

private toProtocolCallHierarchyItem(item: CallHierarchyItem): protocol.CallHierarchyItem {
const scriptInfo = this.getScriptInfoFromProjectService(item.file);
return {
name: item.name,
kind: item.kind,
Expand All @@ -2182,7 +2185,8 @@ namespace ts.server {
};
}

private toProtocolCallHierarchyIncomingCall(incomingCall: CallHierarchyIncomingCall, scriptInfo: ScriptInfo): protocol.CallHierarchyIncomingCall {
private toProtocolCallHierarchyIncomingCall(incomingCall: CallHierarchyIncomingCall): protocol.CallHierarchyIncomingCall {
const scriptInfo = this.getScriptInfoFromProjectService(incomingCall.from.file);
return {
from: this.toProtocolCallHierarchyItem(incomingCall.from),
fromSpans: incomingCall.fromSpans.map(fromSpan => toProtocolTextSpan(fromSpan, scriptInfo))
Expand All @@ -2202,29 +2206,21 @@ namespace ts.server {
if (scriptInfo) {
const position = this.getPosition(args, scriptInfo);
const result = project.getLanguageService().prepareCallHierarchy(file, position);
return result && mapOneOrMany(result, item => this.toProtocolCallHierarchyItem(item, scriptInfo));
return result && mapOneOrMany(result, item => this.toProtocolCallHierarchyItem(item));
}
return undefined;
}

private provideCallHierarchyIncomingCalls(args: protocol.FileLocationRequestArgs): protocol.CallHierarchyIncomingCall[] {
const { file, project } = this.getFileAndProject(args);
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
if (!scriptInfo) {
this.projectService.logErrorForScriptInfoNotFound(file);
return Errors.ThrowNoProject();
}
const scriptInfo = this.getScriptInfoFromProjectService(file);
const incomingCalls = project.getLanguageService().provideCallHierarchyIncomingCalls(file, this.getPosition(args, scriptInfo));
return incomingCalls.map(call => this.toProtocolCallHierarchyIncomingCall(call, scriptInfo));
return incomingCalls.map(call => this.toProtocolCallHierarchyIncomingCall(call));
}

private provideCallHierarchyOutgoingCalls(args: protocol.FileLocationRequestArgs): protocol.CallHierarchyOutgoingCall[] {
const { file, project } = this.getFileAndProject(args);
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
if (!scriptInfo) {
this.projectService.logErrorForScriptInfoNotFound(file);
return Errors.ThrowNoProject();
}
const scriptInfo = this.getScriptInfoFromProjectService(file);
const outgoingCalls = project.getLanguageService().provideCallHierarchyOutgoingCalls(file, this.getPosition(args, scriptInfo));
return outgoingCalls.map(call => this.toProtocolCallHierarchyOutgoingCall(call, scriptInfo));
}
Expand Down
20 changes: 11 additions & 9 deletions src/services/callHierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,18 @@ namespace ts.CallHierarchy {
range: TextRange;
}

function convertEntryToCallSite(entry: FindAllReferences.Entry, _originalNode: Node, typeChecker: TypeChecker): CallSite | undefined {
function convertEntryToCallSite(entry: FindAllReferences.Entry): CallSite | undefined {
if (entry.kind === FindAllReferences.EntryKind.Node) {
if (isCallOrNewExpressionTarget(entry.node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isTaggedTemplateTag(entry.node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isDecoratorTarget(entry.node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isJsxOpeningLikeElementTagName(entry.node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isRightSideOfPropertyAccess(entry.node)
|| isArgumentExpressionOfElementAccess(entry.node)) {
const ancestor = findAncestor(entry.node, isValidCallHierarchyDeclaration) || entry.node.getSourceFile();
return { declaration: firstOrOnly(findImplementationOrAllInitialDeclarations(typeChecker, ancestor)), range: createTextRangeFromNode(entry.node, entry.node.getSourceFile()) };
const { node } = entry;
if (isCallOrNewExpressionTarget(node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isTaggedTemplateTag(node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isDecoratorTarget(node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isJsxOpeningLikeElementTagName(node, /*includeElementAccess*/ true, /*skipPastOuterExpressions*/ true)
|| isRightSideOfPropertyAccess(node)
|| isArgumentExpressionOfElementAccess(node)) {
const sourceFile = node.getSourceFile();
const ancestor = findAncestor(node, isValidCallHierarchyDeclaration) || sourceFile;
return { declaration: ancestor, range: createTextRangeFromNode(node, sourceFile) };
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9551,6 +9551,7 @@ declare namespace ts.server {
private configurePlugin;
private getSmartSelectionRange;
private mapSelectionRange;
private getScriptInfoFromProjectService;
private toProtocolCallHierarchyItem;
private toProtocolCallHierarchyIncomingCall;
private toProtocolCallHierarchyOutgoingCall;
Expand Down
63 changes: 63 additions & 0 deletions tests/baselines/reference/callHierarchyCrossFile.callHierarchy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
╭ name: createModelReference
├ kind: function
├ file: /a.ts
├ span:
│ ╭ /a.ts:1:1-1:42
│ │ 1: export function createModelReference() {}
│ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ ╰
├ selectionSpan:
│ ╭ /a.ts:1:17-1:37
│ │ 1: export function createModelReference() {}
│ │ ^^^^^^^^^^^^^^^^^^^^
│ ╰
├ incoming:
│ ╭ from:
│ │ ╭ name: openElementsAtEditor
│ │ ├ kind: function
│ │ ├ file: /b.ts
│ │ ├ span:
│ │ │ ╭ /b.ts:2:1-4:2
│ │ │ │ 2: function openElementsAtEditor() {
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │ │ 3: createModelReference();
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │ │ 4: }
│ │ │ │ ^
│ │ │ ╰
│ │ ├ selectionSpan:
│ │ │ ╭ /b.ts:2:10-2:30
│ │ │ │ 2: function openElementsAtEditor() {
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^
│ │ │ ╰
│ │ ╰ incoming: none
│ ├ fromSpans:
│ │ ╭ /b.ts:3:3-3:23
│ │ │ 3: createModelReference();
│ │ │ ^^^^^^^^^^^^^^^^^^^^
│ ╰ ╰
│ ╭ from:
│ │ ╭ name: registerDefaultLanguageCommand
│ │ ├ kind: function
│ │ ├ file: /c.ts
│ │ ├ span:
│ │ │ ╭ /c.ts:2:1-4:2
│ │ │ │ 2: function registerDefaultLanguageCommand() {
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │ │ 3: createModelReference();
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │ │ 4: }
│ │ │ │ ^
│ │ │ ╰
│ │ ├ selectionSpan:
│ │ │ ╭ /c.ts:2:10-2:40
│ │ │ │ 2: function registerDefaultLanguageCommand() {
│ │ │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │ ╰
│ │ ╰ incoming: none
│ ├ fromSpans:
│ │ ╭ /c.ts:3:3-3:23
│ │ │ 3: createModelReference();
│ │ │ ^^^^^^^^^^^^^^^^^^^^
│ ╰ ╰
╰ outgoing: none
19 changes: 19 additions & 0 deletions tests/cases/fourslash/callHierarchyCrossFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

// @filename: /a.ts
////export function /**/createModelReference() {}

// @filename: /b.ts
////import { createModelReference } from "./a";
////function openElementsAtEditor() {
//// createModelReference();
////}

// @filename: /c.ts
////import { createModelReference } from "./a";
////function registerDefaultLanguageCommand() {
//// createModelReference();
////}

goTo.marker();
verify.baselineCallHierarchy();