Skip to content

Commit 8b6f873

Browse files
authored
fix(51223): Go-to-definition for yield and await keyword; jump to respective function definition (#51838)
1 parent e73a51d commit 8b6f873

9 files changed

+95
-0
lines changed

src/services/goToDefinition.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
131131
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
132132
}
133133

134+
if (node.kind === SyntaxKind.AwaitKeyword) {
135+
const functionDeclaration = findAncestor(node, n => isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
136+
const isAsyncFunction = functionDeclaration && some(functionDeclaration.modifiers, (node) => node.kind === SyntaxKind.AsyncKeyword);
137+
return isAsyncFunction ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
138+
}
139+
140+
if (node.kind === SyntaxKind.YieldKeyword) {
141+
const functionDeclaration = findAncestor(node, n => isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
142+
const isGeneratorFunction = functionDeclaration && functionDeclaration.asteriskToken;
143+
return isGeneratorFunction ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
144+
}
145+
134146
if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
135147
const classDecl = node.parent.parent;
136148
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker, stopAtAlias);
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+
//// async function /*end1*/foo() {
4+
//// [|/*start1*/await|] Promise.resolve(0);
5+
//// }
6+
7+
//// function notAsync() {
8+
//// [|/*start2*/await|] Promise.resolve(0);
9+
//// }
10+
11+
verify.goToDefinition("start1", "end1");
12+
verify.goToDefinition("start2", []);
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+
//// [|/*start*/await|] Promise.resolve(0);
4+
5+
verify.goToDefinition("start", []);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// class C {
4+
//// notAsync() {
5+
//// [|/*start1*/await|] Promise.resolve(0);
6+
//// }
7+
////
8+
//// async /*end2*/foo() {
9+
//// [|/*start2*/await|] Promise.resolve(0);
10+
//// }
11+
//// }
12+
13+
verify.goToDefinition("start1", []);
14+
verify.goToDefinition("start2", "end2");
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+
//// async function outerAsyncFun() {
4+
//// let /*end*/af = async () => {
5+
//// [|/*start*/await|] Promise.resolve(0);
6+
//// }
7+
//// }
8+
9+
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+
//// function* /*end1*/gen() {
4+
//// [|/*start1*/yield|] 0;
5+
//// }
6+
////
7+
//// const /*end2*/genFunction = function*() {
8+
//// [|/*start2*/yield|] 0;
9+
//// }
10+
11+
verify.goToDefinition("start1", "end1");
12+
verify.goToDefinition("start2", "end2");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// function* outerGen() {
4+
//// function* /*end*/gen() {
5+
//// [|/*start*/yield|] 0;
6+
//// }
7+
//// return gen
8+
//// }
9+
10+
verify.goToDefinition("start", "end");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// class C {
4+
//// notAGenerator() {
5+
//// [|/*start1*/yield|] 0;
6+
//// }
7+
////
8+
//// foo*/*end2*/() {
9+
//// [|/*start2*/yield|] 0;
10+
//// }
11+
//// }
12+
13+
verify.goToDefinition("start1", []);
14+
verify.goToDefinition("start2", "end2");
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+
//// function* gen() {
4+
//// class C { [/*start*/yield 10]() {} }
5+
//// }
6+
7+
verify.goToDefinition("start", []);

0 commit comments

Comments
 (0)