Re: [Codenarc-user] CodeNarc: Adding Custom Rules in a Grails project
Brought to you by:
chrismair
From: Jan K. <ja...@fa...> - 2011-04-29 19:45:02
|
Chris, thank you so much. I was indeed running into issue 13 - http://jira.grails.org/browse/GPCODENARC-13. Commenting out the two rules made it CodeNarc work again. Also, following your approach to add a custom rule using the "classic" rule("file:...") approach worked for me. Cheers, Jan 2011/4/29 Chris Mair <chr...@ea...> > > Jan, > > > > >> 1) Changing from the standard ruleset (somehow part of the CodeNarc for grails Plugin) to my ruleset using this configuration in Config.groovy: > > >>codenarc.ruleSetFiles="file:grails-app/conf/MyRuleSet.groovy" > > >> Causes all violations to disappear from the generated report. > > > > I suspect you may be running into http://jira.grails.org/browse/GPCODENARC-13. > > Also see http://grails.1312388.n4.nabble.com/Codenarc-Plugin-ruleset-configuration-td3462471.html#a3462495. > > If you are including the AbcComplexity and CyclomaticComplexity rules, try removing them (or the entire “size” ruleset). > > > > I have not been able yet to use a custom rule class (compiled) from within the CodeNarc plugin. I’ll try to look into that a bit more next week. I was able to use a rule script, however. > > > > My “Config.groovy” included: > > codenarc.ruleSetFiles = "file:grails-app/conf/MyGroovyRuleSet.groovy" > > > > My ruleset file (“MyGroovyRuleSet.groovy"): > > > > ruleset { > > MethodName > > > > // Custom rule > > rule("file:grails-app/conf/MyStaticFieldRuleScript.groovy") { > > priority = 3 > > } > > > > ruleset('rulesets/basic.xml') > > ruleset('rulesets/imports.xml') > > } > > > > And “grails-app/conf/MyStaticFieldRuleScript.groovy": > > > > import org.codenarc.rule.AbstractRule > > import org.codenarc.source.SourceCode > > > > /** > > * Sample rule. Checks for static fields. > > */ > > class MyStaticFieldRule extends AbstractRule { > > String name = 'MyStaticField' > > String description = 'Check for static fields' > > int priority = 2 > > > > void applyTo(SourceCode sourceCode, List violations) { > > sourceCode.ast.classes.each { clazz -> > > clazz.fields.each { fieldNode -> > > if (fieldNode.static) { > > violations << createViolation(sourceCode, fieldNode) > > } > > } > > } > > } > > } > > > > Chris > > > > From: Jan Karstens [mailto:ja...@fa...] > Sent: Friday, April 29, 2011 4:08 AM > To: cod...@li... > Subject: [Codenarc-user] CodeNarc: Adding Custom Rules in a Grails project > > > > Hello, > > > > I am new to CodeNarc but rather experienced in Static Code Analysis. I am working on a Grails project where we want to apply static code analysis to scan for some project specific patterns. Adding the CodeNarc plugin to Grails and running codenarc (Plugin 0.12, CodeNarc 0.13) was really simple. > > > > I am wondering if it is possible to create CodeNarc Rules local to my Grails project. I read the documentation and watched the screencast on creating new rules. But this all refers to creating new rules in the context of the codenarc project. Is it possible to create a rule in a grails project and reference it from the ruleset? > > > > I am a bit stuck right now and any hint or help is greatly appreciated. > > > > 1) Changing from the standard ruleset (somehow part of the CodeNarc for grails Plugin) to my ruleset using this configuration in Config.groovy: > > > > codenarc.ruleSetFiles="file:grails-app/conf/MyRuleSet.groovy" > > > > Causes all violations to disappear from the generated report. I have the standard groovy ruleset configured and all these rules get listed in the report, but no violation is generated. Commenting out the ruleSetFile configuration and running grails codenarc brings back the violations. > > > > Is this a Grails Plugin issue or a CodeNarc Issue? Any idea or suggestion how I could test this outside of the Grails context? > > > > 2) I have created my own simple test rule and put it into the src/groovy folder in the Grails project structure. I am not sure if this is a valid approach, but it seems to violate some implicit assumptions. Adding my rule to the ruleset and running 'grails codenarc' leads to the following error: > > > > Running CodeNarc ... > > FAILED -- java.lang.AssertionError: No such rule named [MyRule]. Expression: ruleClass. Values: ruleClass = null > > > > Seems that the Rule script could not be found by CodeNarc. Again, am I missing something? Is it possible at to create and use a Grails project local rule? > > > > Again, any hints or help is greatly appreciated. I would really like to get this to work, as the combination of Groovy and the CodeNarc API for AST-analysis seem to be pretty powerful for writing static code checks. > > > > Best regards, > > > > Jan |