-
-
Notifications
You must be signed in to change notification settings - Fork 498
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
💅 lint/complexity/noForEach
suggests an impossible code change when using the index parameter.
#547
Comments
You could use Array.proptotype.entries() as follows: for (const [idx, entry] of data.entries()) {
console.log(`Entry at idx ${idx}: ${entry}`);
} |
Ah, interesting, thanks! What a language. 😆 The description for this linting item is:
When not chained, wouldn't |
I am unsure about this. Because these arrays are short-lived, their cost could be very low. Some engines could even have some shortcut to avoid the cost. The best way to know is to benchmark that. |
To fix this issue, I propose adding a code fix that turns |
There's actually a difference between using
I'm using |
lint/complexity/noForEach
suggests an impossible code change when using the index parameter.lint/complexity/noForEach
suggests an impossible code change when using the index parameter.
Environment information
What happened?
Consider the following code:
Biome shows an error, stating:
I'm not aware of any way to rewrite the code this way, given that the second parameter (array index) is used.
for (const [idx, entry] of Object.entries) { … }
sort of works, butidx
changes from a number to a string in the inner loop (and also the relative positions of the two parameters is reversed). If someone assumes that this is a recommended rewrite, this may cause subtle bugs — ones that may not show up immediately (since they can be masked by type coercion), but at some point in the future.For a more real-world example, see:
https://github.com/cubing/cubing.js/blob/340f18f5824b79d432af238ce2379b601d089d80/src/cubing/bluetooth/smart-puzzle/gan.ts#L187-L196
Expected result
No error when the second parameter is actually used.
Code of Conduct
The text was updated successfully, but these errors were encountered: