Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-unnecessary-condition] don't flag values of an unconstrained or valid type parameter#10473
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
40a0961
991ff23
d33a7d4
ff59a13
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -646,22 +646,53 @@ export default createRule<Options, MessageId>({ | ||
.getCallSignaturesOfType( | ||
getConstrainedTypeAtLocation(services, callback), | ||
) | ||
.map(sig => sig.getReturnType()) | ||
.map(t => { | ||
// TODO: use `getConstraintTypeInfoAtLocation` once it's merged | ||
// https://github.com/typescript-eslint/typescript-eslint/pull/10496 | ||
if (tsutils.isTypeParameter(t)) { | ||
return checker.getBaseConstraintOfType(t); | ||
} | ||
return t; | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
}); | ||
if (returnTypes.length === 0) { | ||
// Not a callable function, e.g. `any` | ||
return; | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
let hasFalsyReturnTypes = false; | ||
let hasTruthyReturnTypes = false; | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
for (const type of returnTypes) { | ||
// Predicate is always necessary if it involves `any` or `unknown` | ||
if (!type || isTypeAnyType(type) || isTypeUnknownType(type)) { | ||
return; | ||
} | ||
if (isPossiblyFalsy(type)) { | ||
hasFalsyReturnTypes = true; | ||
} | ||
if (isPossiblyTruthy(type)) { | ||
hasTruthyReturnTypes = true; | ||
} | ||
// bail early if both a possibly-truthy and a possibly-falsy have been detected | ||
if (hasFalsyReturnTypes && hasTruthyReturnTypes) { | ||
return; | ||
} | ||
} | ||
if (!hasFalsyReturnTypes) { | ||
return context.report({ | ||
node: callback, | ||
messageId: 'alwaysTruthyFunc', | ||
}); | ||
} | ||
if (!hasTruthyReturnTypes) { | ||
return context.report({ | ||
node: callback, | ||
messageId: 'alwaysFalsyFunc', | ||
Uh oh!
There was an error while loading.Please reload this page.