Skip to content

Commit 256cc27

Browse files
sviat9440Святослав Зайцев
authored andcommitted
fix(51225): Go-to-definition on case or default should jump to the containing switch statement if available.
1 parent 8ac4652 commit 256cc27

8 files changed

+90
-4
lines changed

src/services/goToDefinition.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@ namespace ts.GoToDefinition {
2626
return label ? [createDefinitionInfoFromName(typeChecker, label, ScriptElementKind.label, node.text, /*containerName*/ undefined!)] : undefined; // TODO: GH#18217
2727
}
2828

29-
if (node.kind === SyntaxKind.ReturnKeyword) {
30-
const functionDeclaration = findAncestor(node.parent, n =>
31-
isClassStaticBlockDeclaration(n) ? "quit" : isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
32-
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
29+
switch (node.kind) {
30+
case SyntaxKind.ReturnKeyword:
31+
const functionDeclaration = findAncestor(node.parent, n =>
32+
isClassStaticBlockDeclaration(n) ? "quit" : isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
33+
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
34+
case SyntaxKind.DefaultKeyword:
35+
if (!isDefaultClause(node.parent)) {
36+
break;
37+
}
38+
case SyntaxKind.CaseKeyword:
39+
const switchStatement = findAncestor(node.parent, isSwitchStatement);
40+
if (switchStatement) {
41+
return [createDefinitionInfoFromNode(switchStatement, "switch")];
42+
}
43+
break;
3344
}
3445

3546
if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
@@ -510,4 +521,21 @@ namespace ts.GoToDefinition {
510521
return false;
511522
}
512523
}
524+
525+
function createDefinitionInfoFromNode(node: Node, name: string): DefinitionInfo {
526+
const sourceFile = node.getSourceFile();
527+
return {
528+
fileName: sourceFile.fileName,
529+
textSpan: createTextSpanFromNode(node, sourceFile),
530+
kind: ScriptElementKind.label,
531+
name,
532+
containerKind: ScriptElementKind.unknown,
533+
containerName: "",
534+
contextSpan: createTextSpanFromNode(node, sourceFile),
535+
isLocal: false,
536+
isAmbient: false,
537+
unverified: false,
538+
failedAliasResolution: undefined,
539+
};
540+
}
513541
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*end*/switch (null) {
4+
//// [|/*start*/case|] null: break;
5+
////}
6+
7+
verify.goToDefinition("start", "end");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*end*/switch (null) {
4+
//// [|/*start*/default|]: break;
5+
////}
6+
7+
verify.goToDefinition("start", "end");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*end1*/switch (null) {
4+
//// [|/*start1*/default|]: {
5+
//// /*end2*/switch (null) {
6+
//// [|/*start2*/default|]: break;
7+
//// }
8+
//// };
9+
////}
10+
11+
verify.goToDefinition("start1", "end1");
12+
verify.goToDefinition("start2", "end2");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// switch (null) {
4+
//// case null: break;
5+
//// }
6+
////
7+
//// /*end*/switch (null) {
8+
//// [|/*start*/case|] null: break;
9+
//// }
10+
11+
verify.goToDefinition("start", "end");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*end*/export [|/*start*/default|] {}
4+
5+
verify.goToDefinition("start", "end");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
/////*d*/export default { [|/*a*/case|] };
4+
////[|/*b*/default|];
5+
////[|/*c*/case|] 42;
6+
7+
verify.goToDefinition("a", "a");
8+
verify.goToDefinition("b", "d");
9+
verify.goToDefinition("c", []);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////switch (null) {
4+
//// case null:
5+
//// /*end*/export [|/*start*/default|] 123;
6+
7+
verify.goToDefinition("start", "end");

0 commit comments

Comments
 (0)