Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get SonarQube to play nicely with AngularJS?

I've been looking at using SonarQube to do quality checking on some javascript code, but this code is written using AngularJS.

One of the SonarQube rules checks the number of lines in a function - which seems sensible - but in AngularJS, functions are used to define controllers, services and directives, and these functions can get pretty big. Conceptually, they're really more like class definitions, with other functions nested within them.

Ideally, I'd like SonarQube to check the lengths of the inner functions, and possibly the outer function with the inner ones excluded, but I don't know of any way to do this.

Has anyone else encountered this problem using SonarQube with AngularJS, or does anyone know a good solution?

like image 648
Dan King Avatar asked Sep 11 '25 00:09

Dan King


1 Answers

One solution is to declare all your methods separately in your self-executing function.

(function(){
    var controller = function(dependency){
         //...
    },

    someDirective = function(dependency){
        //...
    },

    //Finally, your module
    module = angular.module("MyMod", []);

    module.controller("MyController", ['dependency', controller]);
    module.directive("someDirective", ['dependency', someDirective]);
}());

This definitely can be an uncomfortable pattern for some devs, but it's one way to break your functions into smaller pieces for SonarQube.

like image 119
Hylianpuffball Avatar answered Sep 12 '25 14:09

Hylianpuffball