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

Rule proposal: no-restricted-loops #2453

Open
zanminkian opened this issue Sep 16, 2024 · 3 comments
Open

Rule proposal: no-restricted-loops #2453

zanminkian opened this issue Sep 16, 2024 · 3 comments

Comments

@zanminkian
Copy link
Contributor

Description

JavaScript has 6 kind of loops. We don't really need all of them. Just allow 2 of them (by default) is enough.

Loop Example Allow(by default)
while while(true){}
for-of for(const foo of bar){}
for for(let i=0;i<10;i++){}
for-in for(const foo in bar){}
do-while do{}while(foo<bar)
for-await-of for await (const num of foo()){}

This rule provide an options: restricted-loops, defaults to ['for','for-in','do-while','for-await-of']

Fail

See Description.

Pass

See Description.

Proposed rule name

no-restricted-loops

Additional Info

No response

@zanminkian
Copy link
Contributor Author

no-for-loop can be removed if this rule is implemented. no-for-loop will do auto-fixing. I don't think we need to do auto-fixing in this rule. Auto-fixing make this rule complex to understand.

@fregante
Copy link
Collaborator

fregante commented Sep 27, 2024

Each loop serves a different purpose, definitely for-await-of is not something that can/should be avoided.

I think only for and for-in should generally be avoided because of their possible issues. There are already 2 rules for for:

I'm surprised that there's no rule or rule suggestion for for-in though. @fisker did I miss any?

@fisker
Copy link
Collaborator

fisker commented Sep 27, 2024

I'm surprised that there's no rule or rule suggestion for for-in though.

There seems no rule forbids for..in loop, but only guard-for-in.

Fun fact: This is valid 😄

for (var property = 1 in object);
//                ^^^

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#statements

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

No branches or pull requests

3 participants