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
Святослав Зайцев committed Oct 20, 2022
1 parent 8ac4652 commit aeddc05
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ namespace ts.GoToDefinition {
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
}

if ([SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword].indexOf(node.kind) !== -1) {
const switchStatement = findAncestor(node.parent, n => n.kind === SyntaxKind.SwitchStatement) as SwitchStatement | undefined;
if (switchStatement) {
return [createDefinitionInfoFromSwitchStatement(switchStatement)];
}
}

if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
const classDecl = node.parent.parent;
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker, stopAtAlias);
Expand Down Expand Up @@ -510,4 +517,22 @@ namespace ts.GoToDefinition {
return false;
}
}

function createDefinitionInfoFromSwitchStatement(statement: SwitchStatement): DefinitionInfo {
const switchKeyword = find(statement.getChildren(), n => n.kind === SyntaxKind.SwitchKeyword);
const sourceFile = statement.getSourceFile();
return {
fileName: sourceFile.fileName,
textSpan: createTextSpanFromNode(switchKeyword!),
kind: ScriptElementKind.label,
name: 'switch',
containerKind: ScriptElementKind.unknown,
containerName: '',
contextSpan: createTextSpanFromNode(statement),
isLocal: false,
isAmbient: false,
unverified: false,
failedAliasResolution: undefined,
};
}
}
7 changes: 7 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

/////*end*/switch (null) {
//// [|/*start*/case|] null: break;
////}

verify.goToDefinition("start", "end");
7 changes: 7 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

/////*end*/switch (null) {
//// [|/*start*/default|]: break;
////}

verify.goToDefinition("start", "end");
12 changes: 12 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

/////*end1*/switch (null) {
//// [|/*start1*/default|]: {
//// /*end2*/switch (null) {
//// [|/*start2*/default|]: break;
//// }
//// };
////}

verify.goToDefinition("start1", "end1");
verify.goToDefinition("start2", "end2");
11 changes: 11 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />

//// switch (null) {
//// case null: break;
//// }
////
//// /*end*/switch (null) {
//// [|/*start*/case|] null: break;
//// }

verify.goToDefinition("start", "end");
5 changes: 5 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="fourslash.ts" />

/////*end*/export [|/*start*/default|] {}

verify.goToDefinition("start", "end");
9 changes: 9 additions & 0 deletions tests/cases/fourslash/goToDefinitionSwitch6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />

/////*d*/export default { [|/*a*/case|] };
////[|/*b*/default|];
////[|/*c*/case|] 42;

verify.goToDefinition("a", "a");
verify.goToDefinition("b", "d");
verify.goToDefinition("c", []);

0 comments on commit aeddc05

Please sign in to comment.