You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
Basically a standard for loop should only be allowed if the incrementer variable is used in the block of the loop for anything other than accessing the current array item.
👍 This is Fine since the array index is needed for something
for(varj=0;j<=arr.length;j++){doMath(j);}
👎 This could be replaced with a for(...of...) loop since the index is only used to access the array
for(vari=0;i<=arr.length;i++){doThing(arr[i]);}
Here's the preferred way to write the above loop
for(varitemofarr){doThing(item);}
Is this a feasible rule idea? I've already begun some work on it, but I'll work on it more later. Right now it's only partially working since I'm not sure how to look for variable usages within a certain scope/Block/SyntaxList. Any tips would be appreciated!
The text was updated successfully, but these errors were encountered:
I'm a fan of this rule; it could be called prefer-for-of-loop.
I'm not sure how to look for variable usages within a certain scope/Block/SyntaxList
One idea is to use the regular languageService.getDocumentHighlights method, iterate through the highlights and verify that the source code positions of those spans are within the Block you are searching within.
If you are adding elements to the end of the array being iterated, don’t you need to use a standard for? In this case, a reference to the index variable might not appear inside the loop.
This rule should not be default because in the DOM there are array-like objects which you can't iterate with for..of or .forEach() - at least not without extra hassle. I've been struggling repeatedly with this rule in node collections like you get with document.querySelectorAll().
Classic for loops are highly performant, simple, and readable and should IMHO be considered good practice.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Basically a standard
for
loop should only be allowed if the incrementer variable is used in the block of the loop for anything other than accessing the current array item.👍 This is Fine since the array index is needed for something
👎 This could be replaced with a
for(...of...)
loop since the index is only used to access the arrayHere's the preferred way to write the above loop
Is this a feasible rule idea? I've already begun some work on it, but I'll work on it more later. Right now it's only partially working since I'm not sure how to look for variable usages within a certain scope/Block/SyntaxList. Any tips would be appreciated!
The text was updated successfully, but these errors were encountered: