]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-unmodified-loop-condition.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-unmodified-loop-condition.md
1 ---
2 title: no-unmodified-loop-condition
3 layout: doc
4 rule_type: problem
5 ---
6
7
8 Variables in a loop condition often are modified in the loop.
9 If not, it's possibly a mistake.
10
11 ```js
12 while (node) {
13 doSomething(node);
14 }
15 ```
16
17 ```js
18 while (node) {
19 doSomething(node);
20 node = node.parent;
21 }
22 ```
23
24 ## Rule Details
25
26 This rule finds references which are inside of loop conditions, then checks the
27 variables of those references are modified in the loop.
28
29 If a reference is inside of a binary expression or a ternary expression, this rule checks the result of
30 the expression instead.
31 If a reference is inside of a dynamic expression (e.g. `CallExpression`,
32 `YieldExpression`, ...), this rule ignores it.
33
34 Examples of **incorrect** code for this rule:
35
36 ::: incorrect
37
38 ```js
39 /*eslint no-unmodified-loop-condition: "error"*/
40
41 var node = something;
42
43 while (node) {
44 doSomething(node);
45 }
46 node = other;
47
48 for (var j = 0; j < items.length; ++i) {
49 doSomething(items[j]);
50 }
51
52 while (node !== root) {
53 doSomething(node);
54 }
55 ```
56
57 :::
58
59 Examples of **correct** code for this rule:
60
61 ::: correct
62
63 ```js
64 /*eslint no-unmodified-loop-condition: "error"*/
65
66 while (node) {
67 doSomething(node);
68 node = node.parent;
69 }
70
71 for (var j = 0; j < items.length; ++j) {
72 doSomething(items[j]);
73 }
74
75 // OK, the result of this binary expression is changed in this loop.
76 while (node !== root) {
77 doSomething(node);
78 node = node.parent;
79 }
80
81 // OK, the result of this ternary expression is changed in this loop.
82 while (node ? A : B) {
83 doSomething(node);
84 node = node.parent;
85 }
86
87 // A property might be a getter which has side effect...
88 // Or "doSomething" can modify "obj.foo".
89 while (obj.foo) {
90 doSomething(obj);
91 }
92
93 // A function call can return various values.
94 while (check(obj)) {
95 doSomething(obj);
96 }
97 ```
98
99 :::
100
101 ## When Not To Use It
102
103 If you don't want to notified about references inside of loop conditions, then it's safe to disable this rule.