Skip to content

Commit

Permalink
allow more keywords to return definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
iisaduan committed Jan 15, 2025
1 parent 8731885 commit 57c2f6d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
19 changes: 2 additions & 17 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,28 +159,13 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile

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

if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
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
16 changes: 14 additions & 2 deletions tests/baselines/reference/goToDefinitionReturn5.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionReturn5.ts ===
// function foo() {
// <|function [|foo|]() {
// class Foo {
// static { /*GOTO DEF*/return; }
// }
// }
// }|>

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

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



Expand Down
16 changes: 14 additions & 2 deletions tests/baselines/reference/goToDefinitionYield4.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionYield4.ts ===
// function* gen() {
// class C { [/*GOTO DEF*/yield 10]() {} }
// }
// class C { <|[|[/*GOTO DEF*/yield 10]|]() {}|> }
// }

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

0 comments on commit 57c2f6d

Please sign in to comment.