-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
add fixAwaitInSyncFunction code fix #21069
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
626f399
add fixAwaitInSyncFunction code fix
srolel 5dfb0c5
Just insert the keyword
15e916e
only one codefix
srolel c945beb
remove comment
srolel 0cb462a
Change explicit return type T to Promise<T>
srolel bbba931
Review changes
srolel 752a972
Change codefix message
srolel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "fixAwaitInSyncFunction"; | ||
const errorCodes = [ | ||
Diagnostics.await_expression_is_only_allowed_within_an_async_function.code, | ||
Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator.code, | ||
]; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const { sourceFile, span } = context; | ||
const node = getNodeToInsertBefore(sourceFile, span.start); | ||
if (!node) return undefined; | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node)); | ||
return [{ description: getLocaleSpecificMessage(Diagnostics.Convert_to_async), changes, fixId }]; | ||
}, | ||
fixIds: [fixId], | ||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => | ||
doChange(changes, context.sourceFile, getNodeToInsertBefore(diag.file, diag.start!))), | ||
}); | ||
|
||
function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined { | ||
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); | ||
const containingFunction = getContainingFunction(token); | ||
switch (containingFunction.kind) { | ||
case SyntaxKind.MethodDeclaration: | ||
return containingFunction.name; | ||
case SyntaxKind.FunctionExpression: | ||
case SyntaxKind.FunctionDeclaration: | ||
return findChildOfKind(containingFunction, SyntaxKind.FunctionKeyword, sourceFile); | ||
case SyntaxKind.ArrowFunction: | ||
return findChildOfKind(containingFunction, SyntaxKind.OpenParenToken, sourceFile) || first(containingFunction.parameters); | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, insertBefore: Node): void { | ||
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, insertBefore); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////function f() { | ||
//// await Promise.resolve(); | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`async function f() { | ||
await Promise.resolve(); | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////const f = function() { | ||
//// await Promise.resolve(); | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`const f = async function() { | ||
await Promise.resolve(); | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////const f = { | ||
//// get a() { | ||
//// return await Promise.resolve(); | ||
//// }, | ||
//// get a() { | ||
//// await Promise.resolve(); | ||
//// }, | ||
////} | ||
|
||
verify.not.codeFixAvailable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class Foo { | ||
//// constructor { | ||
//// await Promise.resolve(); | ||
//// } | ||
////} | ||
|
||
verify.not.codeFixAvailable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class Foo { | ||
//// bar() { | ||
//// await Promise.resolve(); | ||
//// } | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`class Foo { | ||
async bar() { | ||
await Promise.resolve(); | ||
} | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////const f = promise => { | ||
//// await promise; | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`const f = async promise => { | ||
await promise; | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////const f = (promise) => { | ||
//// await promise; | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`const f = async (promise) => { | ||
await promise; | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////function f() { | ||
//// for await (const x of g()) { | ||
//// console.log(x); | ||
//// } | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Convert to async", | ||
newFileContent: | ||
`async function f() { | ||
for await (const x of g()) { | ||
console.log(x); | ||
} | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////function f() { | ||
//// await Promise.resolve(); | ||
////} | ||
//// | ||
////const g = () => { | ||
//// await f(); | ||
////} | ||
|
||
verify.codeFixAll({ | ||
fixId: "fixAwaitInSyncFunction", | ||
newFileContent: | ||
`async function f() { | ||
await Promise.resolve(); | ||
} | ||
|
||
const g = async () => { | ||
await f(); | ||
}`, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit.
Add async modifier to containing function
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.
@DanielRosenwasser any better suggestions for the message?