-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Docs: [prefer-for-of] mention non-array loops that cannot be converted #8984
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
Comments
I'm curious - are you able to use |
Forgot to include playground link with |
This rule is ported from tslint. It's one of the few rules that (a) doesn't handle TS specific syntax (b) doesn't use type information to check JS patterns. I think this rule ought to use type information because there could be many APIs that are indexable but not iterable—I've always been surprised that this rule false-positives so much but people have never complained. |
Theoretically - yes. IIRC the rule looks for an indexer using the loop variable and the loop variable being incremented and the loop variable compared against a So you need someone to build an API that exposes both contiguous numeric properties AND a The DOM pseudo-arrays are the only example I know of and that happens purely because legacy reasons. |
That's the definition of array-like objects. There are many non-array array-like objects, including typed arrays, DOM arrays, |
Which is my point. In reality So it's a teeny tiny hole that's very very unlikely to occur. Theoretically bad, Realistically fine. |
My 2c ACK that it's a weird rule to be in typescript-eslint. Seems like in its present implementation it really could/should just be in eslint core. I'm always very sympathetic to edge-case correctness, but
So, I'm -0.1 on fixing this, unless we know of some examples in the wild that don't have |
I think it's okay to just document as a known limitation and move on given how rare the circumstances are. We can include the note about the lib as a solution for the DOM usecase. |
OK fair. Not sure why this rule should remain in our plugin though🤷♂️ |
It's a stylistic concern - not a correctness one. So either we pass it off to a catch-all plugin like unicorn or we just keep the status quo. I don't see any real benefit to the user pain of moving it. |
Interestingly, we've had a bit more willingness than ESLint core to have non-formatting somewhat-mostly-stylistic rules (https://typescript-eslint.io/troubleshooting/formatting#eslint-core-and-formatting).
A few references of other rules that could be for JavaScript in general, but need type information:
Similar to #8792, we don't really have an ideal next place for |
👍 Accepting PRs as a documentation issue. |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.4.5&fileType=.ts&code=MYewdgzgLgBADgQwE4QKZJgXhmVB3GAEQHkBZABWTSQAoBKAbgFgAoUSWRFVAEy3iroAdFzQAxJCAC2AZShIAlmADmNAOQRpqGAA8pAGzUAaGGqiodUAPR7DjVu2gwkqCAFd9UAHIger-qK8QsqoUACi%2BqhSqGBQEABCAJ4AKgjKXgjR6prRAPpgvqhq9iysVlYwAAJQiXCuwIpwUAC0rvpK1nAuAGbozd0gSM0g3TCgUnD6CEoQMFAAFgiwAEao%2BiAE6yBwMBDzIB58q86okwjAvDDLiTADQyOsdzA0kbAK-AAMDDDvADwn7k8Pj8ECEkRUC2%2BCgA1NC6DAAN6sGAwcq7KBubrdVgAX1YZQqyzcsAWSxgPBA-gKsFQADcYmNpHAFJErqhgAg3GgYAAJZKkAAyAGEQPpIsAoApwMRRhSqSBYIt6TAANoyRJSZaioQKcxIJaDAC6j0Gz0csCQYBgIwBHm8hQg8KRLBRaOgmOxLDxpRYQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0Wlq4D2fAV3RRe0IZHBgAviFlA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg2SrIARqwDavGlHQkATAAZp0yDwm0AJgkjiwAXV4BfEHqA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
The rule suggesting the conversion to for-of loop should not trigger in this case
Actual Result
Rule triggers, suggesting uncompilable conversion
Additional Info
No response
The text was updated successfully, but these errors were encountered: