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

Add prefer-array-some rule #887

Merged
merged 16 commits into from Dec 24, 2020
Merged

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Oct 21, 2020

Fixes #736

fisker added a commit to fisker/eslint-plugin-unicorn that referenced this pull request Oct 21, 2020
@fisker fisker marked this pull request as ready for review October 21, 2020 08:28
docs/rules/prefer-array-some.md Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Is it possible to add autofixing?

@fisker
Copy link
Collaborator Author

fisker commented Oct 21, 2020

Is it possible to add autofixing?

It's possible, but not safe.

const array = [""]

// find `""`, falsely
if (array.find(element => typeof element !== 'number'));

// true
if (array.some(element => typeof element !== 'number'));

@fisker
Copy link
Collaborator Author

fisker commented Oct 21, 2020

Note to my self, forgot negative test

if (!foo.find(fn)){}

@fisker
Copy link
Collaborator Author

fisker commented Oct 21, 2020

After a second thought, this rule, should not related to if nor ternary, it should be any expression returns a boolean.

const foo = !array.find(element => element === foo);

Should be reported too.

The same thing is missing in explicit-length-check rule.

And it seems hard to implement.

Maybe I'll use some logic from explicit-length-check , so we can check if (!foo.find(fn)){} if (foo.find(fn) || bar){} etc

@fisker fisker marked this pull request as ready for review December 24, 2020 01:58
@fisker
Copy link
Collaborator Author

fisker commented Dec 24, 2020

Ready for review.

@sindresorhus sindresorhus merged commit b6a5a50 into sindresorhus:master Dec 24, 2020
@sindresorhus
Copy link
Owner

Nice! I really like how simple the rule logic is because it's just using a reusable utility. 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prefer-array-some
2 participants