
rules.jshint.W037.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
History
This warning has existed in two 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 all versions of JSLint and JSHint the warning given is "{a} is a
statement label"
In ESLint the warning given is "Found identifier with the same name as
label"
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 "{a} is a statement label" error (and the alternative "Found identifier with
the same name as label" error) are thrown when JSLint, JSHint or ESLint
encounters a reference that shares an identifier with a label defined in the
same scope. In the following example there is a variable declared in the
global execution context with the identifier x
. Inside the test
function,
there is a for
statement with a label that also has the identifier x
. JSLint
and JSHint throw this error when we attempt to refer to the x
variable:
x 1var x = 0; // Variable with identifier 'x'
2
3function test(i) {
4
5 "use strict";
6
7x: // Label with identifier 'x'
8 while (i) {
9 i -= 1;
10 x = i; // Reference to variable 'x'
11 if (i === 5) {
12 break x; // Reference to label 'x'
13 }
14 }
15}
16
17test(10);
18
ESLint raises its equivalent "Found identifier with the same name as label"
error a little more readily. All it needs to find is a variable and label with
the same identifier:
Why do I get this error?
This error is raised to help improve the readability of your code. It may be
confusing to others (and to you, if you revisit your code some time in the
future) to have various references using the same identifier but to refer to
completely different things.
However, the code in the example above is valid and will not cause problems in
any environment. Read on if you would like to understand the actual behaviour of
the code.
The ECMAScript 5 specification states the following (section
§12.12):
The production Identifier : Statement is evaluated by adding Identifier
to the label set of Statement and then evaluating Statement.
Every statement has a label set (it's empty by default for most statements,
except loops and switch statements which have an implicit label so you can break
out of them). When you give a statement a label, the identifier of that label is
added to the label set. The label set is completely separate from the variable
environment of the scope, so there is no potential for naming conflicts between
label identifiers and variable identifiers. Therefore, this error is designed
purely to make your code clearer and easier to follow.
You can prevent it by simply using different identifiers for labels and
variables:
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 W037. This means you can tell JSHint to not issue this warning
with the /*jshint -W037 */
directive.
In ESLint the rule that generates this warning is named no-label-var
. You can
disable it by setting it to 0
, or enable it by setting it to 1
.