Skip to content

Commit 70ce7ab

Browse files
Merge pull request #27109 from uniqueiniquity/diagnosticLocation
Report async code fix diagnostic on name whenever it exists
2 parents 921863e + 32cb9ec commit 70ce7ab

6 files changed

+61
-1
lines changed

src/services/suggestionDiagnostics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ namespace ts {
132132
// check that a property access expression exists in there and that it is a handler
133133
const returnStatements = getReturnStatementsWithPromiseHandlers(node);
134134
if (returnStatements.length > 0) {
135-
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_may_be_converted_to_an_async_function));
135+
diags.push(createDiagnosticForNode(!node.name && isVariableDeclaration(node.parent) && isIdentifier(node.parent.name) ? node.parent.name : node, Diagnostics.This_may_be_converted_to_an_async_function));
136136
}
137137
}
138138

src/testRunner/unittests/convertToAsyncFunction.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,18 @@ function [#|f|]() {
11321132
const [#|foo|] = function () {
11331133
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
11341134
}
1135+
`);
1136+
1137+
_testConvertToAsyncFunction("convertToAsyncFunction_simpleFunctionExpressionWithName", `
1138+
const foo = function [#|f|]() {
1139+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
1140+
}
1141+
`);
1142+
1143+
_testConvertToAsyncFunction("convertToAsyncFunction_simpleFunctionExpressionAssignedToBindingPattern", `
1144+
const { length } = [#|function|] () {
1145+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
1146+
}
11351147
`);
11361148

11371149
_testConvertToAsyncFunction("convertToAsyncFunction_catchBlockUniqueParams", `
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const { length } = /*[#|*/function/*|]*/ () {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
const { length } = async function () {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const { length } = /*[#|*/function/*|]*/ () {
4+
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
const { length } = async function () {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const foo = function /*[#|*/f/*|]*/() {
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 f() {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
const foo = function /*[#|*/f/*|]*/() {
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 f() {
10+
const result = await fetch('https://typescriptlang.org');
11+
console.log(result);
12+
}

0 commit comments

Comments
 (0)