Repository navigation
fix(prefer-array-some): skip find() fixes when elements may be falsy - #183
Closed
kelvindesman wants to merge 1 commit into
Closed
kelvindesman wants to merge 1 commit into
kelvindesman wants to merge 1 commit into
Conversation
With type information, don't report find() truthiness checks when the array element type can be falsy, or find() === undefined checks when it can be undefined, since some() would give a different result. Without types the behaviour is unchanged. Fixes e18e#168 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
the problem with having an agent take a stab at an unresolved issue: the issue is unresolved 😅 we still need to settle on an approach in the issue. claude has also approached this the wrong way, but i'm not going to review as i don't want another claude iteration, i want a discussion in the issue. let's discuss there until we've decided a solution |
Author
|
Fair point, sorry about that. I jumped ahead of the discussion. Closing this; I'll follow the conversation in #168 and only come back with code once there's an agreed approach. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #168
Following the suggestion in the issue: when type information is available,
prefer-array-someno longer reportsfind()checks where switching tosome()would change the result.if (arr.find(fn)),!arr.find(fn),!!arr.find(fn)) are skipped when the element type can be falsy:0,'',false,null,undefinedor0n, which also coversnumber(and soNaN),string,boolean,any,unknownand unconstrained type parameters.arr.find(fn) === undefined/!== undefinedis skipped when the element type can beundefined.filter(fn).lengthchecks are unaffected, since they don't depend on the element's value.I added
canArrayContainFalsyandcanArrayContainUndefinedtosrc/utils/typescript.ts. They check whether a falsy literal type (checker.getFalseType(),getNumberLiteralType(0), etc.) is assignable to the element type, rather than readingTypeFlags, because the flag values changed in TypeScript 6. Type parameters use their constraint, soT[]andT extends number[]work too.Known limits:
string & {__brand: 'x'}) and string enums with an empty member are still reported, because''isn't assignable to them.strictNullChecksoff,nullandundefinedare assignable to every type, so they're left out of the check there (otherwise every typed array would be skipped). The=== undefinedpath keeps the old behaviour in that mode. I couldn't get the rule tester to load a non-strict tsconfig, so that branch isn't covered by a test.Tests: new typed cases in
prefer-array-some.test.ts, covering number, string, boolean, bigint, any, unknown, tuples, unions of arrays, readonly arrays, enums with and without a0member, genericT[]andT extends number[], and both=== undefineddirections. 19 of them fail without the change.npm run build && npm run lint && npm testpass locally (1090 tests).I used Claude Code to help with this, and I reviewed the change and the tests myself.
🤖 Generated with Claude Code