简体   繁体   English

如何让SonarQube与AngularJS很好地配合?

[英]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. 我一直在寻找使用SonarQube对一些javascript代码进行质量检查,但这段代码是使用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. 其中一个SonarQube规则检查函数中的行数 - 这似乎是合理的 - 但在AngularJS中,函数用于定义控制器,服务和指令,并且这些函数可以变得非常大。 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. 理想情况下,我希望SonarQube检查内部函数的长度,并可能排除内部函数的外部函数,但我不知道有什么方法可以做到这一点。

Has anyone else encountered this problem using SonarQube with AngularJS, or does anyone know a good solution? 有没有其他人使用SonarQube和AngularJS遇到这个问题,或者有人知道一个好的解决方案吗?

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. 对于某些开发者来说,这绝对是一种令人不舒服的模式,但它是将SonreQube的功能分解为小块的一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM