Extending the existing Sonar Rules

SonarQube Version: 6.7
Sonar Scanner:
Plugin: sonar-javascript-plugin-

Problem Statement: Possibility of extending existing rules

Problem Details: In our code base we define a common function in the JS Controller file where it will have multiple function definitions inside the same. The cognitive complexity rule calculates the complexity from the top level and reports the number. We would like the cognitive complexity rule to ignore the first level function and then evaluate the inside functions.

Below is the sample code snippet for quick reference:
function (MainFunction) {
abc.register.controller(’$stateParams’, ‘$modal’, ‘_’, ‘$state’, ‘$scope’) {

           _fun1 = function () {
           fun2 = function () {



The expectation is in the above code, MainFunction cognitive complexity rule should ignore the same and nested functions (fun1, fun2) should be calculated.

The options considered from my end:

  1. Write the custom rule: It require to rewrite the cognitive complexity logic as well, is there a way i can extend the mentioned rule and write a custom rule to ignore the needful?
  2. Ignore Issues in Blocks: This will completely ignore the MainFunction along with nested functions.
  3. Resolve the issue as false positive, the number of false positive will grow as the code base grows and also every new file create will report one issue

Can someone guide what would be the better approach to address the same.

Thanks in advance.
Venkat Thota

Hi Venkat,

If you check the whitepaper you’ll find that there’s an exception for JavaScript.

outer functions are ignored when they are used purely as a declarative mechanism, that is when they contain only declarations at the top level.

However, the presence at the top level of a function (i.e. not nested inside a sub-function) of statements subject to structural increments indicates something other than a pure declarative usage. Consequently, such functions should receive a standard treatment.

There are some examples in the whitepaper, but I won’t reproduce them here.


1 Like


Thanks for the information, the document provided helped us to identify the issue.


1 Like

Hi Ann,

As per the white paper, using conditional statements / blocks like if, switch or other statements like for loops are considering other than declarative usage. However in libraries like jQuery do use if statements in top level function.
// Add button/input type pseudos
for ( i in { radio: true, checkbox: true, file: true, password: true, image: true } ) {
Expr.pseudos[ i ] = createInputPseudo( i );
for ( i in { submit: true, reset: true } ) {
Expr.pseudos[ i ] = createButtonPseudo( i );

This is making whole function treated as normal function. Is there any hint available which analyzer considers for making any function declarative?


Hi Arjun,

I’m not sure I understand the question. However, the white paper does provide some examples in Appendix A that may be helpful to you.