Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-for-in #4504

Closed
jpike88 opened this issue Feb 5, 2019 · 5 comments · Fixed by #4747
Closed

no-for-in #4504

jpike88 opened this issue Feb 5, 2019 · 5 comments · Fixed by #4747

Comments

@jpike88
Copy link
Contributor

jpike88 commented Feb 5, 2019

Rule Suggestion

Is your rule for a general problem or is it specific to your development style?
Specific to my style

What does your suggested rule do?
Prevents forin from ever being used, forces forof.

List several examples where your rule could be used
should be obvious

Additional context
forin and forof are easy to confuse, I'd rather stick with one that is simpler in behaviour

@adidahiya
Copy link
Contributor

no-for-in-array already exists

do you want to disallow this syntax for objects as well?

@jpike88
Copy link
Contributor Author

jpike88 commented Feb 6, 2019

Correct

@jpike88
Copy link
Contributor Author

jpike88 commented May 27, 2019

bump... what do you think

@jpike88
Copy link
Contributor Author

jpike88 commented May 28, 2019

It can be used in every example where a forof is used in our codebase.

for (const key in object) {
    if (object.hasOwnProperty(key)) {
          const element = object[key];
    }
}

^ this is what our autocomplete does when we write a forif. It might be correct but it's just a bother to worry about.

We find it preferable to not even worry about forif, when forof can do it in a more succinct way:

 for (const key of Object.keys(object)){
        const element = object[key];
}

Enforcing this as a rule on a codebase would be great, it just ensures that forof is the only way to iterate (aside from classic for loops, which tslint will recommend against if the index isn't being used with prefer-for-of). We already ban foreach in our tslint.json, we want to extend that to forin.

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented May 28, 2019

This sounds similar to tslint-microsoft-contrib's no-for-in. Accepting PRs! 🙌

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

Successfully merging a pull request may close this issue.

3 participants