Create rule S2004: Functions should not be nested too deeply (#3623)
This commit is contained in:
parent
916019aa40
commit
acb83fd1a5
2
rules/S2004/javascript/metadata.json
Normal file
2
rules/S2004/javascript/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
29
rules/S2004/javascript/rule.adoc
Normal file
29
rules/S2004/javascript/rule.adoc
Normal file
@ -0,0 +1,29 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
|
||||
|
||||
While nesting functions is a common practice in JavaScript, deeply nested functions can make the code harder to read and understand, especially if the functions are long or if there are many levels of nesting.
|
||||
|
||||
This can make it difficult for other developers or even yourself to understand and maintain the code.
|
||||
|
||||
=== Noncompliant code example
|
||||
|
||||
With the default threshold of 3 levels:
|
||||
|
||||
[source,javascript]
|
||||
----
|
||||
function f() {
|
||||
function f_inner() {
|
||||
function f_inner_inner() {
|
||||
function f_inner_inner_inner() { // Noncompliant
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#nested_functions_and_closures[Nested functions and closures]
|
@ -1,2 +1,34 @@
|
||||
{
|
||||
"title": "Functions should not be nested too deeply",
|
||||
"type": "CODE_SMELL",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "FOCUSED"
|
||||
},
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "20min"
|
||||
},
|
||||
"tags": [
|
||||
"brain-overload"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [
|
||||
|
||||
],
|
||||
"legacyKeys": [
|
||||
|
||||
]
|
||||
},
|
||||
"defaultSeverity": "Critical",
|
||||
"ruleSpecification": "RSPEC-2004",
|
||||
"sqKey": "S2004",
|
||||
"scope": "Main",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "unknown"
|
||||
}
|
||||
|
@ -1,34 +1,2 @@
|
||||
{
|
||||
"title": "Functions should not be nested too deeply",
|
||||
"type": "CODE_SMELL",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "FOCUSED"
|
||||
},
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "20min"
|
||||
},
|
||||
"tags": [
|
||||
"brain-overload"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [
|
||||
|
||||
],
|
||||
"legacyKeys": [
|
||||
|
||||
]
|
||||
},
|
||||
"defaultSeverity": "Critical",
|
||||
"ruleSpecification": "RSPEC-2004",
|
||||
"sqKey": "S2004",
|
||||
"scope": "Main",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "unknown"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user