Skip to content

Commit

Permalink
fix(51223): Go-to-definition for yield and await keyword; jump to res…
Browse files Browse the repository at this point in the history
…pective function definition (#51838)
  • Loading branch information
abuob authored Dec 20, 2022
1 parent e73a51d commit 8b6f873
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
return functionDeclaration ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
}

if (node.kind === SyntaxKind.AwaitKeyword) {
const functionDeclaration = findAncestor(node, n => isFunctionLikeDeclaration(n)) as FunctionLikeDeclaration | undefined;
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)) as FunctionLikeDeclaration | undefined;
const isGeneratorFunction = functionDeclaration && functionDeclaration.asteriskToken;
return isGeneratorFunction ? [createDefinitionFromSignatureDeclaration(typeChecker, functionDeclaration)] : undefined;
}

if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
const classDecl = node.parent.parent;
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker, stopAtAlias);
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/goToDefinitionAwait1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

//// async function /*end1*/foo() {
//// [|/*start1*/await|] Promise.resolve(0);
//// }

//// function notAsync() {
//// [|/*start2*/await|] Promise.resolve(0);
//// }

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

//// [|/*start*/await|] Promise.resolve(0);

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

//// class C {
//// notAsync() {
//// [|/*start1*/await|] Promise.resolve(0);
//// }
////
//// async /*end2*/foo() {
//// [|/*start2*/await|] Promise.resolve(0);
//// }
//// }

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

//// async function outerAsyncFun() {
//// let /*end*/af = async () => {
//// [|/*start*/await|] Promise.resolve(0);
//// }
//// }

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

//// function* /*end1*/gen() {
//// [|/*start1*/yield|] 0;
//// }
////
//// const /*end2*/genFunction = function*() {
//// [|/*start2*/yield|] 0;
//// }

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

//// function* outerGen() {
//// function* /*end*/gen() {
//// [|/*start*/yield|] 0;
//// }
//// return gen
//// }

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

//// class C {
//// notAGenerator() {
//// [|/*start1*/yield|] 0;
//// }
////
//// foo*/*end2*/() {
//// [|/*start2*/yield|] 0;
//// }
//// }

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

//// function* gen() {
//// class C { [/*start*/yield 10]() {} }
//// }

verify.goToDefinition("start", []);

0 comments on commit 8b6f873

Please sign in to comment.