Custom StyleCop rule not working as expected

Jul 7, 2012 at 3:08 PM

I'm trying to write a StyleCop rule that disallows underscores anywhere. There is a rule to say that you cant have public string _myfield but I don't want underscores anywhere ie/method names, property names, method parameters.

Below is my code but its not working properly. Can anyone suggest why?

using Microsoft.StyleCop;
using Microsoft.StyleCop.CSharp;

namespace DotNetExtensions.StyleCop.Rules
{

� � [SourceAnalyzer(typeof(CsParser))]
� � public class NoUnderScores : SourceAnalyzer
� � {

� � � � public override void AnalyzeDocument(CodeDocument document)
� � � � {
� � � � � � CsDocument csdocument �= (CsDocument) document;
� � � � � � if (csdocument.RootElement != null && !csdocument.RootElement.Generated)
� � � � � � � � csdocument.WalkDocument(new CodeWalkerElementVisitor<object>(this.VisitElement), null, null);
� � � � }

� � � � private bool VisitElement(CsElement element, CsElement parentElement, object context)
� � � � {

� � � � � � if (!element.Generated)
� � � � � � {
� � � � � � � � foreach(var token in element.Tokens) 
� � � � � � � � {
� � � � � � � � � �if (token.Text.Contains("_"))
� � � � � � � � � � �AddViolation(element, "NoUnderScores");
� � � � � � � � }
� � � � � � }
� � � � � � return true;
� � � � }
� � }
}

 

Coordinator
Jul 7, 2012 at 3:23 PM
You've only got an element visitor which if I recall correctly means your not being called for all the code. Have a look at the source code for documentation rules which should help.


~A.

On 7 Jul 2012, at 16:08, jchannon <notifications@codeplex.com> wrote:

From: jchannon

I'm trying to write a StyleCop rule that disallows underscores anywhere. There is a rule to say that you cant have public string _myfield but I don't want underscores anywhere ie/method names, property names, method parameters.

Below is my code but its not working properly. Can anyone suggest why?

using Microsoft.StyleCop;
using Microsoft.StyleCop.CSharp;

namespace DotNetExtensions.StyleCop.Rules
{

� � [SourceAnalyzer(typeof(CsParser))]
� � public class NoUnderScores : SourceAnalyzer
� � {

� � � � public override void AnalyzeDocument(CodeDocument document)
� � � � {
� � � � � � CsDocument csdocument �= (CsDocument) document;
� � � � � � if (csdocument.RootElement != null && !csdocument.RootElement.Generated)
� � � � � � � � csdocument.WalkDocument(new CodeWalkerElementVisitor<object>(this.VisitElement), null, null);
� � � � }

� � � � private bool VisitElement(CsElement element, CsElement parentElement, object context)
� � � � {

� � � � � � if (!element.Generated)
� � � � � � {
� � � � � � � � foreach(var token in element.Tokens) 
� � � � � � � � {
� � � � � � � � � �if (token.Text.Contains("_"))
� � � � � � � � � � �AddViolation(element, "NoUnderScores");
� � � � � � � � }
� � � � � � }
� � � � � � return true;
� � � � }
� � }
}

Jul 7, 2012 at 3:29 PM
Edited Jul 7, 2012 at 3:39 PM

Maybe the same logic should be added to



private bool VisitStatement(Statement statement, Expression parentExpression, Statement parentStatement, CsElement parentElement, CustomSourceAnalyzer context)
{
// Add your code here.
return true;
}