Skip to content

Commit add14f6

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 add14f6

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

src/services/goToDefinition.ts

Lines changed: 25 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 ([SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword].indexOf(node.kind) !== -1) {
36+
const switchStatement = findAncestor(node.parent, n => n.kind === SyntaxKind.SwitchStatement) as SwitchStatement | undefined;
37+
if (switchStatement) {
38+
return [createDefinitionInfoFromSwitchStatement(switchStatement)];
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,22 @@ namespace ts.GoToDefinition {
510517
return false;
511518
}
512519
}
520+
521+
function createDefinitionInfoFromSwitchStatement(statement: SwitchStatement): DefinitionInfo {
522+
const switchKeyword = find(statement.getChildren(), n => n.kind === SyntaxKind.SwitchKeyword);
523+
const sourceFile = statement.getSourceFile();
524+
return {
525+
fileName: sourceFile.fileName,
526+
textSpan: createTextSpanFromNode(switchKeyword!),
527+
kind: ScriptElementKind.label,
528+
name: "switch",
529+
containerKind: ScriptElementKind.unknown,
530+
containerName: "",
531+
contextSpan: createTextSpanFromNode(statement),
532+
isLocal: false,
533+
isAmbient: false,
534+
unverified: false,
535+
failedAliasResolution: undefined,
536+
};
537+
}
513538
}
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)