Skip to content
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

fix(eslint-plugin): [no-unnecessary-condition] false positive when array predicate returns unknown #2772

Merged
merged 2 commits into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -450,6 +450,10 @@ export default createRule<Options, MessageId>({
// Not a callable function
return;
}
// Predicate is always necessary if it involves `any` or `unknown`
if (returnTypes.some(t => isTypeAnyType(t) || isTypeUnknownType(t))) {
return;
}
if (!returnTypes.some(isPossiblyFalsy)) {
return context.report({
node: callback,
Expand Down
Expand Up @@ -235,6 +235,14 @@ function length(x: string) {
function nonEmptyStrings(x: string[]) {
return x.filter(length);
}

// filter-like predicate
function count(
list: string[],
predicate: (value: string, index: number, array: string[]) => unknown,
) {
return list.filter(predicate).length;
}
`,
// Ignores non-array methods of the same name
`
Expand Down