Skip to content

Commit

Permalink
fix(51225): Go-to-definition on case or default should jump to the co…
Browse files Browse the repository at this point in the history
…ntaining switch statement if available.
  • Loading branch information
sviat9440 authored and Святослав Зайцев committed Oct 21, 2022
1 parent 8ac4652 commit b9dbe02
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ namespace ts.GoToDefinition {
return label ? [createDefinitionInfoFromName(typeChecker, label, ScriptElementKind.label, node.text, /*containerName*/ undefined!)] : undefined; // TODO: GH#18217
}

if (node.kind === SyntaxKind.ReturnKeyword) {
const functionDeclaration = findAncestor(node.parent, n =>
isClassStaticBlockDeclaration(n) ? "quit" : isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
switch (node.kind) {
case SyntaxKind.ReturnKeyword:
const functionDeclaration = findAncestor(node.parent, n =>
isClassStaticBlockDeclaration(n) ? "quit" : isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
case SyntaxKind.DefaultKeyword:
if (!isDefaultClause(node.parent)) {
break;
}
// falls through
case SyntaxKind.CaseKeyword:
const switchStatement = findAncestor(node.parent, isSwitchStatement);
if (switchStatement) {
return [createDefinitionInfoFromNode(switchStatement, "switch")];
}
break;
}

if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
Expand Down Expand Up @@ -510,4 +522,21 @@ namespace ts.GoToDefinition {
return false;
}
}

function createDefinitionInfoFromNode(node: Node, name: string): DefinitionInfo {
const sourceFile = node.getSourceFile();
return {
fileName: sourceFile.fileName,
textSpan: createTextSpanFromNode(node, sourceFile),
kind: ScriptElementKind.label,
name,
containerKind: ScriptElementKind.unknown,
containerName: "",
contextSpan: createTextSpanFromNode(node, sourceFile),
isLocal: false,
isAmbient: false,
unverified: false,
failedAliasResolution: undefined,
};
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit b9dbe02

Please sign in to comment.