Create rule S2004: Functions should not be nested too deeply (#3623)

This commit is contained in:
github-actions[bot] 2024-02-09 15:21:09 +01:00 committed by GitHub
parent 916019aa40
commit acb83fd1a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 32 deletions

View File

@ -0,0 +1,2 @@
{
}

View 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]

View File

@ -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"
}

View File

@ -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"
}