Skip to content

Commit

Permalink
no-for-loop: Only report on the for loop head part (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 21, 2020
1 parent 375d11a commit e9c6963
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 51 deletions.
13 changes: 10 additions & 3 deletions rules/no-for-loop.js
@@ -1,9 +1,10 @@
'use strict';
const {singular} = require('pluralize');
const {isClosingParenToken} = require('eslint-utils');
const {flatten} = require('lodash');
const getDocumentationUrl = require('./utils/get-documentation-url');
const isLiteralValue = require('./utils/is-literal-value');
const {flatten} = require('lodash');
const avoidCapture = require('./utils/avoid-capture');
const {singular} = require('pluralize');

const MESSAGE_ID = 'no-for-loop';
const messages = {
Expand Down Expand Up @@ -329,8 +330,14 @@ const create = context => {
return;
}

const [start] = node.range;
const [, end] = sourceCode.getTokenBefore(node.body, isClosingParenToken).range;

const problem = {
node,
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end)
},
messageId: MESSAGE_ID
};

Expand Down
14 changes: 10 additions & 4 deletions test/no-for-loop.js
Expand Up @@ -787,10 +787,16 @@ runTest.visualize([
}
`,
outdent`
for (let i = 0; i < array.length; i++) {
var foo = array[i];
foo = bar();
}
for (
let i = 0;
i < array.length;
i++
)
// comment (foo)
{
var foo = array[i];
foo = bar();
}
`,
outdent`
for (let i = 0; i < array.length; i++) {
Expand Down
93 changes: 49 additions & 44 deletions test/snapshots/no-for-loop.js.md
Expand Up @@ -21,11 +21,9 @@ Generated by [AVA](https://avajs.dev).
Error 1/1:␊
> 1 | for (let i = 0; i < arr.length; i += 1) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | console.log(arr[i])␊
| ^^^^^^^^^^^^^^^^^^^^␊
> 3 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a `for-of` loop instead of this `for` loop.␊
2 | console.log(arr[i])␊
3 | }␊
`

## no-for-loop - #2
Expand All @@ -48,15 +46,11 @@ Generated by [AVA](https://avajs.dev).
Error 1/1:␊
> 1 | for (let i = 0; i < plugins.length; i++) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | let plugin = plugins[i];␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 3 | plugin = calculateSomeNewValue();␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 4 | // ...␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 5 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a `for-of` loop instead of this `for` loop.␊
2 | let plugin = plugins[i];␊
3 | plugin = calculateSomeNewValue();␊
4 | // ...␊
5 | }␊
`

## no-for-loop - #3
Expand All @@ -65,25 +59,42 @@ Generated by [AVA](https://avajs.dev).
`␊
Input:␊
1 | for (let i = 0; i < array.length; i++) {␊
2 | var foo = array[i];␊
3 | foo = bar();␊
4 | }␊
1 | for (␊
2 | let i = 0;␊
3 | i < array.length;␊
4 | i++␊
5 | )␊
6 | // comment (foo)␊
7 | {␊
8 | var foo = array[i];␊
9 | foo = bar();␊
10 | }␊
Output:␊
1 | for (var foo of array) {␊
2 | foo = bar();␊
3 | }␊
1 | for (␊
2 | var foo of array␊
3 | )␊
4 | // comment (foo)␊
5 | {␊
6 | foo = bar();␊
7 | }␊
Error 1/1:␊
> 1 | for (let i = 0; i < array.length; i++) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | var foo = array[i];␊
| ^^^^^^^^^^^^^^^^^^^^␊
> 3 | foo = bar();␊
| ^^^^^^^^^^^^^^^^^^^^␊
> 4 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
> 1 | for (␊
| ^^^^^␊
> 2 | let i = 0;␊
| ^^^^^^^^^^^␊
> 3 | i < array.length;␊
| ^^^^^^^^^^^␊
> 4 | i++␊
| ^^^^^^^^^^^␊
> 5 | )␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
6 | // comment (foo)␊
7 | {␊
8 | var foo = array[i];␊
9 | foo = bar();␊
10 | }␊
`

## no-for-loop - #4
Expand All @@ -102,11 +113,9 @@ Generated by [AVA](https://avajs.dev).
Error 1/1:␊
> 1 | for (let i = 0; i < array.length; i++) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | let foo = array[i];␊
| ^^^^^^^^^^^^^^^^^^^^␊
> 3 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a `for-of` loop instead of this `for` loop.␊
2 | let foo = array[i];␊
3 | }␊
`

## no-for-loop - #5
Expand All @@ -125,11 +134,9 @@ Generated by [AVA](https://avajs.dev).
Error 1/1:␊
> 1 | for (let i = 0; i < array.length; i++) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | const foo = array[i];␊
| ^^^^^^^^^^^^^^^^^^^^^^␊
> 3 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a `for-of` loop instead of this `for` loop.␊
2 | const foo = array[i];␊
3 | }␊
`

## no-for-loop - #6
Expand All @@ -149,9 +156,7 @@ Generated by [AVA](https://avajs.dev).
Error 1/1:␊
> 1 | for (let i = 0; i < array.length; i++) {␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 2 | var foo = array[i], bar = 1;␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
> 3 | }␊
| ^^ Use a `for-of` loop instead of this `for` loop.␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a `for-of` loop instead of this `for` loop.␊
2 | var foo = array[i], bar = 1;␊
3 | }␊
`
Binary file modified test/snapshots/no-for-loop.js.snap
Binary file not shown.

0 comments on commit e9c6963

Please sign in to comment.