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 no-array-for-each rule #1017

Merged
merged 43 commits into from Jan 19, 2021
Merged

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Jan 14, 2021

Fixes #955

Not done yet, still two TODOs need fix.

return false;
}

// TODO: check parameters conflicts
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not really sure how to check this,

Should not fix

foo.forEach(foo => bar());

->

for (const foo of foo) bar();
a[foo].forEach(foo => bar());

->

for (const foo of a[foo]) bar();

Should fix

a((foo) => foo).forEach(foo => bar());

->

for (const foo of a((foo) => foo)) bar();

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Figured a way to detect this, but not very efficient.

Copy link
Collaborator Author

@fisker fisker Jan 18, 2021

Choose a reason for hiding this comment

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

A similar logic maybe possible to auto fix destructuring parameters, but it's much complicated, I'm going to try in a seperate PR.

@fisker fisker marked this pull request as ready for review January 18, 2021 08:29
@fisker
Copy link
Collaborator Author

fisker commented Jan 18, 2021

There are some fix errors on typescript parser , I'll look into it later. Fixed

@sindresorhus sindresorhus changed the title Add no-array-for-each Add no-array-for-each rule Jan 19, 2021
@sindresorhus sindresorhus merged commit 719cd00 into sindresorhus:master Jan 19, 2021
@sindresorhus
Copy link
Owner

This is really well done (as always). And a super useful rule. 🙌🏻

@fisker fisker deleted the no-array-for-each branch January 19, 2021 08:11
> Output

`␊
1 | for (const [foo, index] of a((foo, index) => foo + index).entries()) bar();␊
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was stupid, forgot to swap parameters.

@fregante
Copy link
Collaborator

You're killin' it, @fisker! 🥳

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: no-array-for-each
3 participants