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-find rule #735

Merged
merged 24 commits into from May 31, 2020
Merged

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented May 17, 2020

Fixes #730

@fisker fisker force-pushed the rule/prefer-find branch 3 times, most recently from b328e31 to 661da4c Compare May 17, 2020 14:34
test/prefer-find.js Outdated Show resolved Hide resolved
docs/rules/prefer-find.md Outdated Show resolved Hide resolved
docs/rules/prefer-find.md Outdated Show resolved Hide resolved
rules/prefer-find.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

The rule should be named prefer-array-find.

@fisker
Copy link
Collaborator Author

fisker commented May 18, 2020

I thought about this rule name too, I agree it should be prefer-array-find, and I even want rename no-reduce to no-array-reduce, then I realize that rule already published in v20.

fisker and others added 2 commits May 18, 2020 15:30
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@sindresorhus sindresorhus changed the title Add prefer-find rule Add prefer-array-find rule May 18, 2020
@fisker fisker force-pushed the rule/prefer-find branch 4 times, most recently from 81fcfef to bed2aaa Compare May 18, 2020 08:15
@fisker
Copy link
Collaborator Author

fisker commented May 18, 2020

function foo([bar] = array.filter(baz)) {}

This can't fix, right?

@fisker fisker force-pushed the rule/prefer-find branch 2 times, most recently from 3897a9e to b4d57b8 Compare May 18, 2020 09:37
@fisker
Copy link
Collaborator Author

fisker commented May 19, 2020

I'm going to add suggestion to

const [foo = bar] = array.filter(fn);

Suggestion 1:

const foo = array.find(fn) || bar;

Suggestion 2:

const foo = array.find(fn) ?? bar;

God, I thought this is simple, so I started it.

@fisker
Copy link
Collaborator Author

fisker commented May 19, 2020

Following cases not handled.

const foo = array.filter(fn);
const bar = foo.shift();

I didn't handle this, because .shift() has side effect, we should know it's only called once, and not in a function/while/for.

const foo = array.filter(fn);
while (foo.shift()) {}

const foo = array.filter(fn);
const [bar] = foo;

I didn't handle this, because we need handle/exclude this

const foo = array.filter(fn);
const [bar = baz] = foo;

I use suggestion for const [foo = baz] = array.filter(fn);, but we can't use suggestion for this. because we are reportting problem on .filter, suggest to use ?? or || will be weird, we need decide ignore this case or do something different.


let bar;
const foo = array.filter(fn);
[bar] = foo;

Same as second one.

let bar;
const foo = array.filter(fn);
[bar = baz] = foo;

@sindresorhus
Copy link
Owner

function foo([bar] = array.filter(baz)) {}
This can't fix, right?

No point in handling that. I have never seen such code in practice.

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2020

@oscarmarcelo Can you help review this?

Copy link

@oscarmarcelo oscarmarcelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'm not yet that familiar with the intricacies of unicorn, but for what I've seen here, this looks pretty solid! Congratulations and thank you, @fisker! 👏

@sindresorhus sindresorhus merged commit 12b46da into sindresorhus:master May 31, 2020
@sindresorhus
Copy link
Owner

Nice work as always, @fisker 👍

@fisker fisker deleted the rule/prefer-find branch May 31, 2020 16:24
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-find
5 participants