Skip to content

allow calling goToDef on modifiers of named declarations #60384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ import {
isJSDocOverrideTag,
isJsxOpeningLikeElement,
isJumpStatementTarget,
isModifier,
isModuleSpecifierLike,
isNamedDeclaration,
isNameOfFunctionDeclaration,
isNewExpressionTarget,
isObjectBindingPattern,
Expand Down Expand Up @@ -128,7 +130,10 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
const typeChecker = program.getTypeChecker();

if (node.kind === SyntaxKind.OverrideKeyword || (isIdentifier(node) && isJSDocOverrideTag(parent) && parent.tagName === node)) {
return getDefinitionFromOverriddenMember(typeChecker, node) || emptyArray;
const def = getDefinitionFromOverriddenMember(typeChecker, node);
if (def !== undefined || node.kind !== SyntaxKind.OverrideKeyword) {
return def || emptyArray;
}
}

// Labels
Expand All @@ -137,15 +142,8 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
return label ? [createDefinitionInfoFromName(typeChecker, label, ScriptElementKind.label, node.text, /*containerName*/ undefined!)] : undefined; // TODO: GH#18217
}

// for switch statments
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;
Expand All @@ -159,16 +157,15 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
break;
}

if (node.kind === SyntaxKind.AwaitKeyword) {
const functionDeclaration = findAncestor(node, n => isFunctionLikeDeclaration(n));
const isAsyncFunction = functionDeclaration && some(functionDeclaration.modifiers, node => node.kind === SyntaxKind.AsyncKeyword);
return isAsyncFunction ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
}

if (node.kind === SyntaxKind.YieldKeyword) {
const functionDeclaration = findAncestor(node, n => isFunctionLikeDeclaration(n));
const isGeneratorFunction = functionDeclaration && functionDeclaration.asteriskToken;
return isGeneratorFunction ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
// for keywords related to function or method definitions
let findFunctionDecl: ((n: Node) => boolean | "quit") | undefined;
switch (node.kind) {
case SyntaxKind.ReturnKeyword:
case SyntaxKind.AwaitKeyword:
case SyntaxKind.YieldKeyword:
findFunctionDecl = isFunctionLikeDeclaration;
const functionDeclaration = findAncestor(node, findFunctionDecl) as FunctionLikeDeclaration | undefined;
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
}

if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
Expand Down Expand Up @@ -217,6 +214,10 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
}
}

if (isModifier(node) && (isClassElement(parent) || isNamedDeclaration(parent))) {
symbol = parent.symbol;
}

// Could not find a symbol e.g. node is string or number keyword,
// or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol
if (!symbol) {
Expand Down Expand Up @@ -457,7 +458,11 @@ export function getTypeDefinitionAtPosition(typeChecker: TypeChecker, sourceFile
if (isImportMeta(node.parent) && node.parent.name === node) {
return definitionFromType(typeChecker.getTypeAtLocation(node.parent), typeChecker, node.parent, /*failedAliasResolution*/ false);
}
const { symbol, failedAliasResolution } = getSymbol(node, typeChecker, /*stopAtAlias*/ false);
let { symbol, failedAliasResolution } = getSymbol(node, typeChecker, /*stopAtAlias*/ false);
if (isModifier(node) && (isClassElement(node.parent) || isNamedDeclaration(node.parent))) {
symbol = node.parent.symbol;
failedAliasResolution = false;
}
if (!symbol) return undefined;

const typeAtLocation = typeChecker.getTypeOfSymbolAtLocation(symbol, node);
Expand Down
16 changes: 14 additions & 2 deletions tests/baselines/reference/goToDefinitionAwait1.baseline.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
// async function foo() {
// await Promise.resolve(0);
// }
// function notAsync() {
// <|function [|notAsync|]() {
// /*GOTO DEF*/await Promise.resolve(0);
// }
// }|>

// === Details ===
[
{
"kind": "function",
"name": "notAsync",
"containerName": "",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]
20 changes: 17 additions & 3 deletions tests/baselines/reference/goToDefinitionAwait3.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionAwait3.ts ===
// class C {
// notAsync() {
// <|[|notAsync|]() {
// /*GOTO DEF*/await Promise.resolve(0);
// }
// }|>
//
// async foo() {
// --- (line: 7) skipped ---
// await Promise.resolve(0);
// }
// }

// === Details ===
[
{
"kind": "method",
"name": "notAsync",
"containerName": "C",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]



Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/goToDefinitionMember.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// === goToDefinition ===
// === /a.ts ===
// class A {
// <|private [|{| textSpan: true |}z|]/*GOTO DEF*/: string;|>
// }

// === Details ===
[
{
"kind": "property",
"name": "z",
"containerName": "A",
"isLocal": true,
"isAmbient": false,
"unverified": false,
"failedAliasResolution": false
}
]
Loading
Loading