fix(51225): Go-to-definition on case or default should jump to the containing switch statement if available. (#51236)

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Sviatoslav Zaytsev
2024-01-12 01:14:03 +06:00
committed by GitHub
parent 9a0c7b1c3b
commit 1717826b6e
16 changed files with 291 additions and 6 deletions

View File

@@ -234,6 +234,7 @@ import {
StringLiteralLike,
stripQuotes,
SuperContainer,
SwitchStatement,
Symbol,
SymbolDisplay,
SymbolDisplayPart,
@@ -406,7 +407,7 @@ function getContextNodeForNodeEntry(node: Node): ContextNode | undefined {
}
/** @internal */
export function getContextNode(node: NamedDeclaration | BinaryExpression | ForInOrOfStatement | undefined): ContextNode | undefined {
export function getContextNode(node: NamedDeclaration | BinaryExpression | ForInOrOfStatement | SwitchStatement | undefined): ContextNode | undefined {
if (!node) return undefined;
switch (node.kind) {
case SyntaxKind.VariableDeclaration:
@@ -451,14 +452,18 @@ export function getContextNode(node: NamedDeclaration | BinaryExpression | ForIn
findAncestor(node.parent, node => isBinaryExpression(node) || isForInOrOfStatement(node)) as BinaryExpression | ForInOrOfStatement,
) :
node;
case SyntaxKind.SwitchStatement:
return {
start: find(node.getChildren(node.getSourceFile()), node => node.kind === SyntaxKind.SwitchKeyword)!,
end: (node as SwitchStatement).caseBlock,
};
default:
return node;
}
}
/** @internal */
export function toContextSpan(textSpan: TextSpan, sourceFile: SourceFile, context?: ContextNode): { contextSpan: TextSpan; } | undefined {
export function toContextSpan(textSpan: TextSpan, sourceFile: SourceFile, context: ContextNode | undefined): { contextSpan: TextSpan; } | undefined {
if (!context) return undefined;
const contextSpan = isContextWithStartAndEndNode(context) ?
getTextSpan(context.start, sourceFile, context.end) :
@@ -874,6 +879,9 @@ function getTextSpan(node: Node, sourceFile: SourceFile, endNode?: Node): TextSp
start += 1;
end -= 1;
}
if (endNode?.kind === SyntaxKind.CaseBlock) {
end = endNode.getFullStart();
}
return createTextSpanFromBounds(start, end);
}

View File

@@ -53,6 +53,7 @@ import {
isClassStaticBlockDeclaration,
isConstructorDeclaration,
isDeclarationFileName,
isDefaultClause,
isExternalModuleNameRelative,
isFunctionLike,
isFunctionLikeDeclaration,
@@ -69,6 +70,7 @@ import {
isPropertyName,
isRightSideOfPropertyAccess,
isStaticModifier,
isSwitchStatement,
isTypeAliasDeclaration,
isTypeReferenceNode,
isVariableDeclaration,
@@ -91,6 +93,7 @@ import {
skipTrivia,
some,
SourceFile,
SwitchStatement,
Symbol,
SymbolDisplay,
SymbolFlags,
@@ -105,6 +108,9 @@ import {
TypeReference,
unescapeLeadingUnderscores,
} from "./_namespaces/ts";
import {
isContextWithStartAndEndNode,
} from "./_namespaces/ts.FindAllReferences";
/** @internal */
export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile, position: number, searchOtherFilesOnly?: boolean, stopAtAlias?: boolean): readonly DefinitionInfo[] | undefined {
@@ -133,9 +139,26 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
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 [createDefinitionInfoFromSwitch(switchStatement, sourceFile)];
}
break;
}
if (node.kind === SyntaxKind.AwaitKeyword) {
@@ -634,6 +657,24 @@ function createDefinitionInfoFromName(checker: TypeChecker, declaration: Declara
};
}
function createDefinitionInfoFromSwitch(statement: SwitchStatement, sourceFile: SourceFile): DefinitionInfo {
const keyword = FindAllReferences.getContextNode(statement)!;
const textSpan = createTextSpanFromNode(isContextWithStartAndEndNode(keyword) ? keyword.start : keyword, sourceFile);
return {
fileName: sourceFile.fileName,
textSpan,
kind: ScriptElementKind.keyword,
name: "switch",
containerKind: undefined!,
containerName: "",
...FindAllReferences.toContextSpan(textSpan, sourceFile, keyword),
isLocal: true,
isAmbient: false,
unverified: false,
failedAliasResolution: undefined,
};
}
function isDefinitionVisible(checker: TypeChecker, declaration: Declaration): boolean {
if (checker.isDeclarationVisible(declaration)) return true;
if (!declaration.parent) return false;