-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use correct type for async refactoring diagnostics #26749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
d21c078
cc4e1f8
158f0b0
3ad6a66
97e5393
bb892d9
f4765a6
64bbf89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,18 @@ namespace ts.codefix { | |
|
|
||
| function convertToAsyncFunction(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker, context: CodeFixContextBase): void { | ||
| // get the function declaration - returns a promise | ||
| const functionToConvert: FunctionLikeDeclaration = getContainingFunction(getTokenAtPosition(sourceFile, position)) as FunctionLikeDeclaration; | ||
| const tokenAtPosition = getTokenAtPosition(sourceFile, position); | ||
| let functionToConvert: FunctionLikeDeclaration; | ||
|
|
||
| // if the parent of a FunctionLikeDeclaration is a variable declaration, the convertToAsync diagnostic will be reported on the variable name | ||
| if (isIdentifier(tokenAtPosition) && isVariableDeclaration(tokenAtPosition.parent) && | ||
| tokenAtPosition.parent.initializer && isFunctionLikeDeclaration(tokenAtPosition.parent.initializer)) { | ||
| functionToConvert = tokenAtPosition.parent.initializer; | ||
| } | ||
| else { | ||
| functionToConvert = getContainingFunction(getTokenAtPosition(sourceFile, position)) as FunctionLikeDeclaration; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use |
||
| } | ||
|
|
||
| if (!functionToConvert) { | ||
| return; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -319,7 +319,7 @@ interface String { charAt: any; } | |
| interface Array<T> {}` | ||
| }; | ||
|
|
||
| function testConvertToAsyncFunction(caption: string, text: string, baselineFolder: string, description: DiagnosticMessage, includeLib?: boolean) { | ||
| function testConvertToAsyncFunction(caption: string, text: string, baselineFolder: string, diagnosticDescription: DiagnosticMessage, codeFixDescription: DiagnosticMessage, includeLib?: boolean) { | ||
| const t = getTest(text); | ||
| const selectionRange = t.ranges.get("selection")!; | ||
| if (!selectionRange) { | ||
|
|
@@ -361,12 +361,14 @@ interface Array<T> {}` | |
| }; | ||
|
|
||
| const diagnostics = languageService.getSuggestionDiagnostics(f.path); | ||
| const diagnostic = find(diagnostics, diagnostic => diagnostic.messageText === description.message); | ||
| assert.isNotNull(diagnostic); | ||
| const diagnostic = find(diagnostics, diagnostic => diagnostic.messageText === diagnosticDescription.message); | ||
| assert.exists(diagnostic); | ||
| assert.equal(diagnostic!.start, context.span.start); | ||
| assert.equal(diagnostic!.length, context.span.length); | ||
|
|
||
| const actions = codefix.getFixes(context); | ||
| const action = find(actions, action => action.description === description.message)!; | ||
| assert.isNotNull(action); | ||
| const action = find(actions, action => action.description === codeFixDescription.message)!; | ||
| assert.exists(action); | ||
|
|
||
| const data: string[] = []; | ||
| data.push(`// ==ORIGINAL==`); | ||
|
|
@@ -423,6 +425,10 @@ interface Array<T> {}` | |
| _testConvertToAsyncFunction("convertToAsyncFunction_basic", ` | ||
| function [#|f|](): Promise<void>{ | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| }`); | ||
| _testConvertToAsyncFunction("convertToAsyncFunction_basicNoReturnTypeAnnotation", ` | ||
| function [#|f|]() { | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| }`); | ||
| _testConvertToAsyncFunction("convertToAsyncFunction_basicWithComments", ` | ||
| function [#|f|](): Promise<void>{ | ||
|
|
@@ -436,6 +442,10 @@ function [#|f|](): Promise<void>{ | |
| _testConvertToAsyncFunction("convertToAsyncFunction_ArrowFunction", ` | ||
| [#|():Promise<void> => {|] | ||
| return fetch('https://typescriptlang.org').then(result => console.log(result)); | ||
| }`); | ||
| _testConvertToAsyncFunction("convertToAsyncFunction_ArrowFunctionNoAnnotation", ` | ||
| [#|() => {|] | ||
| return fetch('https://typescriptlang.org').then(result => console.log(result)); | ||
| }`); | ||
| _testConvertToAsyncFunction("convertToAsyncFunction_Catch", ` | ||
| function [#|f|]():Promise<void> { | ||
|
|
@@ -1178,11 +1188,17 @@ function [#|f|]() { | |
| } | ||
| `); | ||
|
|
||
| _testConvertToAsyncFunction("convertToAsyncFunction_simpleFunctionExpression", ` | ||
| const [#|foo|] = function () { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I realize you didn't change this, but that seems like a weird place for the squiggle. What if the function had a name?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracked at #26801. |
||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| } | ||
| `); | ||
|
|
||
|
|
||
| }); | ||
|
|
||
| function _testConvertToAsyncFunction(caption: string, text: string) { | ||
| testConvertToAsyncFunction(caption, text, "convertToAsyncFunction", Diagnostics.Convert_to_async_function, /*includeLib*/ true); | ||
| testConvertToAsyncFunction(caption, text, "convertToAsyncFunction", Diagnostics.This_may_be_converted_to_an_async_function, Diagnostics.Convert_to_async_function, /*includeLib*/ true); | ||
| } | ||
|
|
||
| function _testConvertToAsyncFunctionFailed(caption: string, text: string) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| /*[#|*/() => {/*|]*/ | ||
| return fetch('https://typescriptlang.org').then(result => console.log(result)); | ||
| } | ||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| async () => { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| return console.log(result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| /*[#|*/() => {/*|]*/ | ||
| return fetch('https://typescriptlang.org').then(result => console.log(result)); | ||
| } | ||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| async () => { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| return console.log(result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| function /*[#|*/f/*|]*/() { | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| } | ||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| async function f() { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| console.log(result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| function /*[#|*/f/*|]*/() { | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| } | ||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| async function f() { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| console.log(result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| const /*[#|*/foo/*|]*/ = function () { | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| } | ||
|
|
||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| const foo = async function () { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| console.log(result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // ==ORIGINAL== | ||
|
|
||
| const /*[#|*/foo/*|]*/ = function () { | ||
| return fetch('https://typescriptlang.org').then(result => { console.log(result) }); | ||
| } | ||
|
|
||
| // ==ASYNC FUNCTION::Convert to async function== | ||
|
|
||
| const foo = async function () { | ||
| const result = await fetch('https://typescriptlang.org'); | ||
| console.log(result); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let functionToConvert: FunctionLikeDeclaration | undefined;, sincegetContainingFunctionmay return undefined.