-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [return-await] add option to report in error-handling scenarios only, and deprecate "never" #9364
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
Merged
kirkwaiblinger
merged 19 commits into
typescript-eslint:main
from
kirkwaiblinger:return-await-unopinionated
Jul 18, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1384bf5
first stab
kirkwaiblinger bc8310f
fix CI stuff
kirkwaiblinger 7c5340f
Update packages/eslint-plugin/src/rules/return-await.ts
kirkwaiblinger 624eeee
change comments and stuff
kirkwaiblinger 68d8c5b
Merge branch 'main' into return-await-unopinionated
kirkwaiblinger 845812e
Merge branch 'main' into return-await-unopinionated
kirkwaiblinger 93c66be
add table to docs
kirkwaiblinger 59192fc
dry it up
kirkwaiblinger c2bac5c
revert useAutoFix
kirkwaiblinger 5150439
Update packages/eslint-plugin/docs/rules/return-await.mdx
kirkwaiblinger d050fe4
to awaiting
kirkwaiblinger c12dfeb
changes and deprecation
kirkwaiblinger 5ea1b81
mention motivation for each option in its section
kirkwaiblinger 584aad9
subjunctive
8000
kirkwaiblinger b27b673
Update packages/eslint-plugin/docs/rules/return-await.mdx
kirkwaiblinger 14f801b
Merge branch 'main' into return-await-unopinionated
kirkwaiblinger 9309769
Merge branch 'main' into return-await-unopinionated
kirkwaiblinger f5a4060
move things out of closure
kirkwaiblinger 8cd6116
Merge branch 'main' into return-await-unopinionated
kirkwaiblinger 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
kirkwaiblinger marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
|
@@ -24,6 +24,12 @@ interface ScopeInfo { | |
owningFunc: FunctionNode; | ||
} | ||
|
||
type Option = | ||
| 'in-try-catch' | ||
| 'always' | ||
| 'never' | ||
| 'error-handling-correctness-only'; | ||
|
||
export default createRule({ | ||
name: 'return-await', | ||
meta: { | ||
|
@@ -50,7 +56,12 @@ export default createRule({ | |
schema: [ | ||
{ | ||
type: 'string', | ||
enum: ['in-try-catch', 'always', 'never'], | ||
enum: [ | ||
'in-try-catch', | ||
'always', | ||
'never', | ||
'error-handling-correctness-only', | ||
] satisfies Option[], | ||
}, | ||
], | ||
}, | ||
|
@@ -271,92 +282,70 @@ export default createRule({ | |
const type = checker.getTypeAtLocation(child); | ||
const isThenable = tsutils.isThenableType(checker, expression, type); | ||
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. github's diffing gets real confused about what actually changed here if you don't use the "ignore whitespace" here. Be sure to use that. |
||
|
||
if (!isAwait && !isThenable) { | ||
return; | ||
} | ||
|
||
if (isAwait && !isThenable) { | ||
// any/unknown could be thenable; do not auto-fix | ||
const useAutoFix = !(isTypeAnyType(type) || isTypeUnknownType(type)); | ||
|
||
context.report({ | ||
messageId: 'nonPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'nonPromiseAwait', | ||
fix: fixer => removeAwait(fixer, node), | ||
}), | ||
}); | ||
return; | ||
} | ||
// handle awaited _non_thenables | ||
|
||
const affectsErrorHandling = | ||
affectsExplicitErrorHandling(expression) || | ||
affectsExplicitResourceManagement(node); | ||
const useAutoFix = !affectsErrorHandling; | ||
if (!isThenable) { | ||
if (isAwait) { | ||
// any/unknown could be thenable; do not auto-fix | ||
const useAutoFix = !(isTypeAnyType(type) || isTypeUnknownType(type)); | ||
|
||
if (option === 'always') { | ||
if (!isAwait && isThenable) { | ||
context.report({ | ||
messageId: 'requiredPromiseAwait', | ||
messageId: 'nonPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'requiredPromiseAwaitSuggestion', | ||
fix: fixer => | ||
insertAwait( | ||
fixer, | ||
node, | ||
isHigherPrecedenceThanAwait(expression), | ||
), | ||
messageId: 'nonPromiseAwait', | ||
fix: fixer => removeAwait(fixer, node), | ||
}), | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (option === 'never') { | ||
if (isAwait) { | ||
context.report({ | ||
messageId: 'disallowedPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'disallowedPromiseAwaitSuggestion', | ||
fix: fixer => removeAwait(fixer, node), | ||
}), | ||
}); | ||
} | ||
// At this point it's definitely a thenable. | ||
|
||
return; | ||
} | ||
const affectsErrorHandling = | ||
affectsExplicitErrorHandling(expression) || | ||
affectsExplicitResourceManagement(node); | ||
const useAutoFix = !affectsErrorHandling; | ||
|
||
if (option === 'in-try-catch') { | ||
if (isAwait && !affectsErrorHandling) { | ||
context.report({ | ||
messageId: 'disallowedPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'disallowedPromiseAwaitSuggestion', | ||
fix: fixer => removeAwait(fixer, node), | ||
}), | ||
}); | ||
} else if (!isAwait && affectsErrorHandling) { | ||
context.report({ | ||
messageId: 'requiredPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'requiredPromiseAwaitSuggestion', | ||
fix: fixer => | ||
insertAwait( | ||
fixer, | ||
node, | ||
isHigherPrecedenceThanAwait(expression), | ||
), | ||
}), | ||
}); | ||
} | ||
const ruleConfiguration = getConfiguration(option as Option); | ||
|
||
return; | ||
const shouldAwaitInCurrentContext = affectsErrorHandling | ||
? ruleConfiguration.errorHandlingContext | ||
: ruleConfiguration.ordinaryContext; | ||
|
||
switch (shouldAwaitInCurrentContext) { | ||
case "don't-care": | ||
break; | ||
case 'await': | ||
if (!isAwait) { | ||
context.report({ | ||
messageId: 'requiredPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'requiredPromiseAwaitSuggestion', | ||
fix: fixer => | ||
insertAwait( | ||
fixer, | ||
node, | ||
isHigherPrecedenceThanAwait(expression), | ||
), | ||
}), | ||
}); | ||
} | ||
break; | ||
case 'no-await': | ||
if (isAwait) { | ||
context.report({ | ||
messageId: 'disallowedPromiseAwait', | ||
node, | ||
...fixOrSuggest(useAutoFix, { | ||
messageId: 'disallowedPromiseAwaitSuggestion', | ||
fix: fixer => removeAwait(fixer, node), | ||
}), | ||
}); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -406,6 +395,38 @@ export default createRule({ | |
}, | ||
}); | ||
|
||
type WhetherToAwait = 'await' | 'no-await' | "don't-care"; | ||
|
||
interface RuleConfiguration { | ||
ordinaryContext: WhetherToAwait; | ||
errorHandlingContext: WhetherToAwait; | ||
} | ||
|
||
function getConfiguration(option: Option): RuleConfiguration { | ||
switch (option) { | ||
case 'always': | ||
return { | ||
ordinaryContext: 'await', | ||
errorHandlingContext: 'await', | ||
}; | ||
case 'never': | ||
return { | ||
ordinaryContext: 'no-await', | ||
errorHandlingContext: 'no-await', | ||
}; | ||
case 'error-handling-correctness-only': | ||
return { | ||
ordinaryContext: "don't-care", | ||
errorHandlingContext: 'await', | ||
}; | ||
case 'in-try-catch': | ||
return { | ||
ordinaryContext: 'no-await', | ||
errorHandlingContext: 'await', | ||
}; | ||
} | ||
} | ||
|
||
function fixOrSuggest<MessageId extends string>( | ||
useFix: boolean, | ||
suggestion: TSESLint.SuggestionReportDescriptor<MessageId>, | ||
|
16 changes: 15 additions & 1 deletion
16
packages/eslint-plugin/tests/docs-eslint-output-snapshots/return-await.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.