Skip to content

Commit 1613057

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 1613057

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

src/services/goToDefinition.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ namespace ts.GoToDefinition {
3232
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
3333
}
3434

35+
if (node.kind === SyntaxKind.CaseKeyword || node.kind === SyntaxKind.DefaultKeyword) {
36+
const switchStatement = findAncestor(node.parent, isSwitchStatement) as SwitchStatement | undefined;
37+
if (switchStatement) {
38+
return [createDefinitionInfoFromNode(switchStatement, 'switch')];
39+
}
40+
}
41+
3542
if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
3643
const classDecl = node.parent.parent;
3744
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker, stopAtAlias);
@@ -510,4 +517,21 @@ namespace ts.GoToDefinition {
510517
return false;
511518
}
512519
}
520+
521+
function createDefinitionInfoFromNode(node: Node, name: string): DefinitionInfo {
522+
const sourceFile = node.getSourceFile();
523+
return {
524+
fileName: sourceFile.fileName,
525+
textSpan: createTextSpanFromNode(node),
526+
kind: ScriptElementKind.label,
527+
name,
528+
containerKind: ScriptElementKind.unknown,
529+
containerName: "",
530+
contextSpan: createTextSpanFromNode(node),
531+
isLocal: false,
532+
isAmbient: false,
533+
unverified: false,
534+
failedAliasResolution: undefined,
535+
};
536+
}
513537
}
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", []);

0 commit comments

Comments
 (0)