Skip to content

Commit b620104

Browse files
committed
Merge branch 'master' of https://github.com./microsoft/TypeScript into multiline-comment-directives
2 parents 036a4ae + 94c5c3f commit b620104

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/services/codefixes/fixReturnTypeInAsyncFunction.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ namespace ts.codefix {
3939
});
4040

4141
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined {
42-
const returnTypeNode = getReturnTypeNode(sourceFile, pos);
42+
if (isInJSFile(sourceFile)) {
43+
return undefined;
44+
}
45+
46+
const token = getTokenAtPosition(sourceFile, pos);
47+
const func = findAncestor(token, isFunctionLikeDeclaration);
48+
const returnTypeNode = func?.type;
4349
if (!returnTypeNode) {
4450
return undefined;
4551
}
4652

4753
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
4854
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
49-
const promisedTypeNode = checker.typeToTypeNode(promisedType);
55+
const promisedTypeNode = checker.typeToTypeNode(promisedType, /*enclosingDeclaration*/ returnTypeNode, /*flags*/ undefined);
5056
if (promisedTypeNode) {
5157
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
5258
}
@@ -55,16 +61,4 @@ namespace ts.codefix {
5561
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
5662
changes.replaceNode(sourceFile, returnTypeNode, createTypeReferenceNode("Promise", [promisedTypeNode]));
5763
}
58-
59-
function getReturnTypeNode(sourceFile: SourceFile, pos: number): TypeNode | undefined {
60-
if (isInJSFile(sourceFile)) {
61-
return undefined;
62-
}
63-
64-
const token = getTokenAtPosition(sourceFile, pos);
65-
const parent = findAncestor(token, isFunctionLikeDeclaration);
66-
if (parent?.type) {
67-
return parent.type;
68-
}
69-
}
7064
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////
5+
////interface A {}
6+
////export { A as PublicA };
7+
////async function foo(): A {
8+
//// return {}
9+
////}
10+
11+
verify.codeFix({
12+
index: 0,
13+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "A", "A"],
14+
newFileContent: `
15+
interface A {}
16+
export { A as PublicA };
17+
async function foo(): Promise<A> {
18+
return {}
19+
}`
20+
});

0 commit comments

Comments
 (0)