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.
There are a couple of cases where the prefer-for-of rule produces linter errors on code can not be trivially converted to a for-of loop. In particular:
Step values != 1
Start of iteration != 0
Reverse iteration
Additionally, there are a few other cases where errors aren't reported, but possibly should, such as: <= arr.length - 1 instead of < arr.length.
I have mixed feelings about whether or not reverse iteration should trigger this error or not. Sometimes the array can just be reversed before iteration, other times you might want to clone the array first before reversing.
I guess in any of these cases, you could write helper functions to minimize the places where you need to opt-out of this rule, but at that point you aren't really preferring for-of so much as you are not preferring bare for-loops.
declarevararr: number[];declarevarstep: number;// trivial #1: array iteration [0,length)for(leti=0;i<arr.length;i++){console.log(arr[i]);}// trivial #2: effectively the same as trivial #1, but not caughtfor(leti=0;i<=arr.length-1;i++){console.log(arr[i]);}// trivial #3: i++, ++i, and i += 1 should all be consideredfor(leti=0;i<arr.length;i+=1){console.log(arr[i]);}/****************************************************************/// non-trivial #1: array iteration with step size not known to be 1for(leti=0;i<arr.length;i+=step){console.log(arr[i]);}// non-trivial #2: non-zero start of iterationfor(leti=1;i<arr.length;i++){console.log(arr[i]);}// non-trivial #3: reverse iteration...for(leti=arr.length;i>=0;i--){console.log(arr[i]);}
with tslint.json configuration:
{
"rules": {
"prefer-for-of": true
}
}
Actual behavior
Trivial Loops:
test.ts[5, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
test.ts[15, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
Non-Trivial Loops:
test.ts[22, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
test.ts[27, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
test.ts[32, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
Expected behavior
The first 3 trivial loops should be reported as errors, the last three loops should not trigger the error.
Trivial Loops:
test.ts[5, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
test.ts[10, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
test.ts[15, 1]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
Non-Trivial Loops:
[no errors]
The text was updated successfully, but these errors were encountered:
I just noticed that #1758 was merged while I was writing up the bug report. It looks like that should cover most of these cases except possibly Trivial #2.
Bug Report
There are a couple of cases where the
prefer-for-of
rule produces linter errors on code can not be trivially converted to afor-of
loop. In particular:Additionally, there are a few other cases where errors aren't reported, but possibly should, such as:
<= arr.length - 1
instead of< arr.length
.I have mixed feelings about whether or not reverse iteration should trigger this error or not. Sometimes the array can just be reversed before iteration, other times you might want to clone the array first before reversing.
I guess in any of these cases, you could write helper functions to minimize the places where you need to opt-out of this rule, but at that point you aren't really preferring
for-of
so much as you are not preferring bare for-loops.TypeScript code being linted
with
tslint.json
configuration:Actual behavior
Expected behavior
The first 3 trivial loops should be reported as errors, the last three loops should not trigger the error.
The text was updated successfully, but these errors were encountered: