62 lines
2.1 KiB
Plaintext
62 lines
2.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In JavaScript, labels are identifiers that allow you to name blocks of code, such as loops and conditional statements. They are used in conjunction with statements like `break` and `continue` to control the flow of execution within nested loops and conditionals.
|
|
|
|
It's worth noting that labels are not widely used in modern JavaScript programming because they can lead to complex and hard-to-maintain code. In most cases, there are better alternatives to achieve the desired control flow without resorting to labels.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
myLabel: {
|
|
let x = doSomething();
|
|
if (x > 0) {
|
|
break myLabel;
|
|
}
|
|
doSomethingElse();
|
|
}
|
|
----
|
|
|
|
If you find yourself using labels, you should reevaluate your code structure and explore other options for better code clarity and maintainability.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let x = doSomething();
|
|
if (x <= 0) {
|
|
doSomethingElse();
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label[label]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break[``++break++``]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue[``++continue++``]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 14 Mar 2017, 16:53:55 Elena Vilchik wrote:
|
|
\[~ann.campbell.2] After SONARJS-953 this rule will not raise issue for any loop. WDYT how this should be in RSPEC? exception?
|
|
|
|
=== on 14 Mar 2017, 18:35:20 Ann Campbell wrote:
|
|
\[~elena.vilchik] I've added an exception. Since I'm not sure I've ever seen any other use of labels outside of COBOL, will you update the code samples?
|
|
|
|
=== on 14 Mar 2017, 18:51:42 Elena Vilchik wrote:
|
|
\[~ann.campbell.2] thanks! done!
|
|
|
|
=== on 20 Jan 2020, 15:11:56 Tibor Blenessy wrote:
|
|
I removed the exception, see main RSPEC-1119 for explanation
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|