Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-condition] false positive when ar…
Browse files Browse the repository at this point in the history
…ray predicate returns unknown (#2772)
  • Loading branch information
ArnaudBarre committed Nov 17, 2020
1 parent 3e4a0ed commit 111c244
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit 111c244

Please sign in to comment.