
rules.jshint.W088.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
History
This warning has existed in several forms across the three main linters. It was
introduced in the original version of JSLint and has remained in all three tools
ever since.
In JSLint and JSHint prior to version 1.0.0 the warning has always been "Bad
for-in variable '{a}'"
In JSHint 1.0.0 and above the warning is "Creating global 'for' variable"
In ESLint the warning has always been "Invalid left-hand side in for-in"
The situations that produce the warning have not changed despite changes to the
text of the warning itself.
When do I get this error?
The "Bad for-in variable '{a}'" error (and the alternative "Creating global
'for' variable" and "Invalid left-hand side in for-in" errors) are thrown when
JSLint, JSHint or ESLint encounters a for-in
statement in which the
initializer contains a literal value. In the following example we have a for-
in
statement in which we attempt to assign each property to a string literal:
x 1function test() {
2 "use strict";
3 var y = {};
4 for ("a" in y) {
5 // ...
6 }
7}
8
JSLint and JSHint also raise this warning when it encounters a for-in
statement in which the initializer contains an undefined variable reference.
In this example the for-in
statement will assign each property to x
which
has not been declared:
8 1function test() {
2 "use strict";
3 var y = {};
4 for (x in y) {
5 // ...
6 }
7}
8
Why do I get this error?
In the case of non-identifiers like the string literal in the first example
above this error is raised to highlight a fatal reference error. Your code
will throw an error in all environments if you do not resolve this issue. The
for-in
statement effectively assigns a value to its left-hand side expression
on each iteration (ES5 §12.6.4). It makes no sense to assign
a value to a literal. Imagine it as if you're trying to do 123 = 234
.
In the case of an undefined variable this error is raised to highlight a lack
of convention and a possible misunderstanding of how the language works.
As mentioned previously the for-in
statement assigns a value to its left-hand
side expression on each iteration. If the variable used in the left-hand side
expression has not been declared it will be created as a property of the global
object. You can resolve this issue by simply declaring the variable before the
for-in
statement:
9 1function test() {
2 "use strict";
3 var y = {},
4 x;
5 for (x in y) {
6 // ...
7 }
8}
9
In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. The identifier of this warning is W088.
This means you can tell JSHint to not issue this warning with the /*jshint
-W088 */
directive.
In ESLint this error is generated by the Esprima parser and can therefore not be
disabled.