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