Create rule S6660: If
statements should not be the only statement in else
blocks (#2383)
https://github.com/SonarSource/SonarJS/issues/3916
This commit is contained in:
parent
817af72c6c
commit
96155fb71f
17
rules/S6660/javascript/metadata.json
Normal file
17
rules/S6660/javascript/metadata.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"title": "If statements should not be the only statement in else blocks",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-6660",
|
||||
"sqKey": "S6660",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "covered"
|
||||
}
|
52
rules/S6660/javascript/rule.adoc
Normal file
52
rules/S6660/javascript/rule.adoc
Normal file
@ -0,0 +1,52 @@
|
||||
== Why is this an issue?
|
||||
|
||||
When `if` is the only statement in the `else` block, it is better to use `else if` because it simplifies the code and makes it more readable.
|
||||
|
||||
When using nested `if` statements, it can be difficult to keep track of the logic and understand the flow of the code. Using `else if` makes the code more concise and easier to follow.
|
||||
|
||||
[source,javascript,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
if (condition1) {
|
||||
// ...
|
||||
} else {
|
||||
if (condition2) { // Noncompliant: 'if' statement is the only statement in the 'else' block
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (condition3) {
|
||||
// ...
|
||||
} else {
|
||||
if (condition4) { // Noncompliant: 'if' statement is the only statement in the 'else' block
|
||||
// ...
|
||||
} else {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
Fix your code by using `else if` if the nested `if` is the only statement in the `else` block.
|
||||
|
||||
[source,javascript,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
if (condition1) {
|
||||
// ...
|
||||
} else if (condition2) {
|
||||
// ...
|
||||
}
|
||||
|
||||
|
||||
if (condition3) {
|
||||
// ...
|
||||
} else if (condition4) {
|
||||
// ...
|
||||
} else {
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
* link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else++[MDN - ``++if...else++``]
|
2
rules/S6660/metadata.json
Normal file
2
rules/S6660/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user