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?
3 回答
您不能专门排除控制器操作,也不能按类名排除(使用doNotApplyToClassNames
属性),因为规则是按文件级别应用的,而不是按类应用的。
我能得到的最接近的是将整个 Controller 从规则中排除,如下所示:
NoDef.doNotApplyToFilesMatching = /.*Controller.groovy/
我还建议从规则中排除Services
和grailsApplication
注入,因为在那里有 defs 不会打扰我。例如:
class SomeService {
def otherService
def grailsApplication
}
对此的规则配置应该是:
NoDef.excludeRegex = /(.+Service|.+Controller|grailsApplication)/
我阅读了博客文章并尝试了 doNotApplyToClassNames 选项,但它不起作用。
从文档中:
NoDef 规则
自 CodeNarc 0.22 起
注意:此规则适用于文件的文本内容而不是特定的类,因此它不支持 applyToClassNames 和 doNotApplyToClassNames 配置属性。
所以你实际上不能使用 doNotApplyToClassNames 参数,但是excludeRegex
你可以使用一个属性,但是你需要想出一个正则表达式来排除你的控制器操作。
描述可以在 def 关键字前面的属性、参数或方法的名称的正则表达式。
如果您使用的是 codenarc 插件,那么我相信您应该能够禁用 codenarc 规则集文件中的一些规则,如本博客所述
http://www.tothenew.com/blog/grails-clean-code-configure-codenarc-plugin/