Skip to content

Commit 6ddf752

Browse files
Merge pull request #26749 from uniqueiniquity/getWholeType
Use correct type for async refactoring diagnostics
2 parents c929e74 + 64bbf89 commit 6ddf752

9 files changed

+106
-10
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ namespace ts.codefix {
4242

4343
function convertToAsyncFunction(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker, context: CodeFixContextBase): void {
4444
// get the function declaration - returns a promise
45-
const functionToConvert: FunctionLikeDeclaration = getContainingFunction(getTokenAtPosition(sourceFile, position)) as FunctionLikeDeclaration;
45+
const tokenAtPosition = getTokenAtPosition(sourceFile, position);
46+
let functionToConvert: FunctionLikeDeclaration | undefined;
47+
48+
// if the parent of a FunctionLikeDeclaration is a variable declaration, the convertToAsync diagnostic will be reported on the variable name
49+
if (isIdentifier(tokenAtPosition) && isVariableDeclaration(tokenAtPosition.parent) &&
50+
tokenAtPosition.parent.initializer && isFunctionLikeDeclaration(tokenAtPosition.parent.initializer)) {
51+
functionToConvert = tokenAtPosition.parent.initializer;
52+
}
53+
else {
54+
functionToConvert = tryCast(getContainingFunction(getTokenAtPosition(sourceFile, position)), isFunctionLikeDeclaration);
55+
}
56+
4657
if (!functionToConvert) {
4758
return;
4859
}

src/services/suggestionDiagnostics.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace ts {
33
export function computeSuggestionDiagnostics(sourceFile: SourceFile, program: Program, cancellationToken: CancellationToken): DiagnosticWithLocation[] {
44
program.getSemanticDiagnostics(sourceFile, cancellationToken);
55
const diags: DiagnosticWithLocation[] = [];
6-
const checker = program.getDiagnosticsProducingTypeChecker();
6+
const checker = program.getTypeChecker();
77

88
if (sourceFile.commonJsModuleIndicator &&
99
(programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) &&
@@ -115,11 +115,12 @@ namespace ts {
115115

116116
function addConvertToAsyncFunctionDiagnostics(node: FunctionLikeDeclaration, checker: TypeChecker, diags: DiagnosticWithLocation[]): void {
117117

118-
const functionType = node.type ? checker.getTypeFromTypeNode(node.type) : undefined;
119-
if (isAsyncFunction(node) || !node.body || !functionType) {
118+
if (isAsyncFunction(node) || !node.body) {
120119
return;
121120
}
122121

122+
const functionType = checker.getTypeAtLocation(node);
123+
123124
const callSignatures = checker.getSignaturesOfType(functionType, SignatureKind.Call);
124125
const returnType = callSignatures.length ? checker.getReturnTypeOfSignature(callSignatures[0]) : undefined;
125126

src/testRunner/unittests/convertToAsyncFunction.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ interface String { charAt: any; }
319319
interface Array<T> {}`
320320
};
321321

322-
function testConvertToAsyncFunction(caption: string, text: string, baselineFolder: string, description: DiagnosticMessage, includeLib?: boolean) {
322+
function testConvertToAsyncFunction(caption: string, text: string, baselineFolder: string, diagnosticDescription: DiagnosticMessage, codeFixDescription: DiagnosticMessage, includeLib?: boolean) {
323323
const t = getTest(text);
324324
const selectionRange = t.ranges.get("selection")!;
325325
if (!selectionRange) {
@@ -361,12 +361,14 @@ interface Array<T> {}`
361361
};
362362

363363
const diagnostics = languageService.getSuggestionDiagnostics(f.path);
364-
const diagnostic = find(diagnostics, diagnostic => diagnostic.messageText === description.message);
365-
assert.isNotNull(diagnostic);
364+
const diagnostic = find(diagnostics, diagnostic => diagnostic.messageText === diagnosticDescription.message);
365+
assert.exists(diagnostic);
366+
assert.equal(diagnostic!.start, context.span.start);
367+
assert.equal(diagnostic!.length, context.span.length);
366368

367369
const actions = codefix.getFixes(context);
368-
const action = find(actions, action => action.description === description.message)!;
369-
assert.isNotNull(action);
370+
const action = find(actions, action => action.description === codeFixDescription.message)!;
371+
assert.exists(action);
370372

371373
const data: string[] = [];
372374
data.push(`// ==ORIGINAL==`);
@@ -423,6 +425,10 @@ interface Array<T> {}`
423425
_testConvertToAsyncFunction("convertToAsyncFunction_basic", `
424426
function [#|f|](): Promise<void>{
425427
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
428+
}`);
429+
_testConvertToAsyncFunction("convertToAsyncFunction_basicNoReturnTypeAnnotation", `
430+
function [#|f|]() {
431+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
426432
}`);
427433
_testConvertToAsyncFunction("convertToAsyncFunction_basicWithComments", `
428434
function [#|f|](): Promise<void>{
@@ -436,6 +442,10 @@ function [#|f|](): Promise<void>{
436442
_testConvertToAsyncFunction("convertToAsyncFunction_ArrowFunction", `
437443
[#|():Promise<void> => {|]
438444
return fetch('https://typescriptlang.org').then(result => console.log(result));
445+
}`);
446+
_testConvertToAsyncFunction("convertToAsyncFunction_ArrowFunctionNoAnnotation", `
447+
[#|() => {|]
448+
return fetch('https://typescriptlang.org').then(result => console.log(result));
439449
}`);
440450
_testConvertToAsyncFunction("convertToAsyncFunction_Catch", `
441451
function [#|f|]():Promise<void> {
@@ -1178,11 +1188,17 @@ function [#|f|]() {
11781188
}
11791189
`);
11801190

1191+
_testConvertToAsyncFunction("convertToAsyncFunction_simpleFunctionExpression", `
1192+
const [#|foo|] = function () {
1193+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
1194+
}
1195+
`);
1196+
11811197

11821198
});
11831199

11841200
function _testConvertToAsyncFunction(caption: string, text: string) {
1185-
testConvertToAsyncFunction(caption, text, "convertToAsyncFunction", Diagnostics.Convert_to_async_function, /*includeLib*/ true);
1201+
testConvertToAsyncFunction(caption, text, "convertToAsyncFunction", Diagnostics.This_may_be_converted_to_an_async_function, Diagnostics.Convert_to_async_function, /*includeLib*/ true);
11861202
}
11871203

11881204
function _testConvertToAsyncFunctionFailed(caption: string, text: string) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
/*[#|*/() => {/*|]*/
4+
return fetch('https://typescriptlang.org').then(result => console.log(result));
5+
}
6+
// ==ASYNC FUNCTION::Convert to async function==
7+
8+
async () => {
9+
const result = await fetch('https://typescriptlang.org');
10+
return console.log(result);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
/*[#|*/() => {/*|]*/
4+
return fetch('https://typescriptlang.org').then(result => console.log(result));
5+
}
6+
// ==ASYNC FUNCTION::Convert to async function==
7+
8+
async () => {
9+
const result = await fetch('https://typescriptlang.org');
10+
return console.log(result);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
// ==ASYNC FUNCTION::Convert to async function==
7+
8+
async function f() {
9+
const result = await fetch('https://typescriptlang.org');
10+
console.log(result);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
// ==ASYNC FUNCTION::Convert to async function==
7+
8+
async function f() {
9+
const result = await fetch('https://typescriptlang.org');
10+
console.log(result);
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const /*[#|*/foo/*|]*/ = function () {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
const foo = async function () {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const /*[#|*/foo/*|]*/ = function () {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
const foo = async function () {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}

0 commit comments

Comments
 (0)