@@ -111,7 +111,53 @@ ruleTester.run("no-loop-func", rule, {
111111 "let a;"
112112 ] . join ( "\n" ) ,
113113 parserOptions : { ecmaVersion : 6 }
114+ } ,
115+
116+ /*
117+ * These loops _look_ like they might be unsafe, but because i is undeclared, they're fine
118+ * at least as far as this rule is concerned - the loop doesn't declare/generate the variable.
119+ */
120+ "while(i) { (function() { i; }) }" ,
121+ "do { (function() { i; }) } while (i)" ,
122+
123+ /**
124+ * These loops _look_ like they might be unsafe, but because i is declared outside the loop
125+ * and is not updated in or after the loop, they're fine as far as this rule is concerned.
126+ * The variable that's captured is just the one variable shared by all the loops, but that's
127+ * explicitly expected in these cases.
128+ */
129+ "var i; while(i) { (function() { i; }) }" ,
130+ "var i; do { (function() { i; }) } while (i)" ,
131+
132+ /**
133+ * These loops use an undeclared variable, and so shouldn't be flagged by this rule,
134+ * they'll be picked up by no-undef.
135+ */
136+ {
137+ code : "for (var i=0; i<l; i++) { (function() { undeclared; }) }" ,
138+ parserOptions : { ecmaVersion : 6 }
139+ } ,
140+ {
141+ code : "for (let i=0; i<l; i++) { (function() { undeclared; }) }" ,
142+ parserOptions : { ecmaVersion : 6 }
143+ } ,
144+ {
145+ code : "for (var i in {}) { i = 7; (function() { undeclared; }) }" ,
146+ parserOptions : { ecmaVersion : 6 }
147+ } ,
148+ {
149+ code : "for (let i in {}) { i = 7; (function() { undeclared; }) }" ,
150+ parserOptions : { ecmaVersion : 6 }
151+ } ,
152+ {
153+ code : "for (const i of {}) { (function() { undeclared; }) }" ,
154+ parserOptions : { ecmaVersion : 6 }
155+ } ,
156+ {
157+ code : "for (let i = 0; i < 10; ++i) { for (let x in xs.filter(x => x != undeclared)) { } }" ,
158+ parserOptions : { ecmaVersion : 6 }
114159 }
160+
115161 ] ,
116162 invalid : [
117163 {
@@ -152,14 +198,6 @@ ruleTester.run("no-loop-func", rule, {
152198 code : "for (var i=0; i<l; (function() { i; })(), i++) { }" ,
153199 errors : [ { messageId : "unsafeRefs" , data : { varNames : "'i'" } , type : "FunctionExpression" } ]
154200 } ,
155- {
156- code : "while(i) { (function() { i; }) }" ,
157- errors : [ { messageId : "unsafeRefs" , data : { varNames : "'i'" } , type : "FunctionExpression" } ]
158- } ,
159- {
160- code : "do { (function() { i; }) } while (i)" ,
161- errors : [ { messageId : "unsafeRefs" , data : { varNames : "'i'" } , type : "FunctionExpression" } ]
162- } ,
163201
164202 // Warns functions which are using modified variables.
165203 {
0 commit comments