2

Is there a way to turn off the Codenarc NoDef rule for Grails controller actions? The rule is a good practice to follow in general but since some actions return a value and some don't, we typically use def to support all variants. I looked around a bit to see if this rule can be turned off for controller actions but came up empty. Does anyone know?

4

3 回答 3

2

不能专门排除控制器操作,也不能按类名排除(使用doNotApplyToClassNames属性),因为规则是按文件级别应用的,而不是按类应用的。

我能得到的最接近的是将整个 Controller 从规则中排除,如下所示:

NoDef.doNotApplyToFilesMatching = /.*Controller.groovy/

我还建议从规则中排除ServicesgrailsApplication注入,因为在那里有 defs 不会打扰我。例如:

class SomeService {
    def otherService
    def grailsApplication
}

对此的规则配置应该是:

NoDef.excludeRegex = /(.+Service|.+Controller|grailsApplication)/
于 2016-12-29T12:37:51.250 回答
1

我阅读了博客文章并尝试了 doNotApplyToClassNames 选项,但它不起作用。

从文档中:

NoDef 规则

自 CodeNarc 0.22 起

注意:此规则适用于文件的文本内容而不是特定的类,因此它不支持 applyToClassNames 和 doNotApplyToClassNames 配置属性。

所以你实际上不能使用 doNotApplyToClassNames 参数,但是excludeRegex你可以使用一个属性,但是你需要想出一个正则表达式来排除你的控制器操作。

描述可以在 def 关键字前面的属性、参数或方法的名称的正则表达式。

于 2016-02-25T22:01:15.947 回答
0

如果您使用的是 codenarc 插件,那么我相信您应该能够禁用 codenarc 规则集文件中的一些规则,如本博客所述

http://www.tothenew.com/blog/grails-clean-code-configure-codenarc-plugin/

于 2015-10-23T04:27:54.950 回答