Reputation: 10413
I am new to StyleCop, and I need to implement own coding standarts for the place I work. I am using VS2005 and cannot debug it. Upgrading to VS2008/2010 is not an option for us now.
I wonder many things:
1) How can I identify the methods parameters? I tried the below but do not know where to go, SDK documentation is not really helpful.
private bool VisitElement(CsElement element, CsElement parentElement, object context)
{
if (element.ElementType == ElementType.Method)
{
...
2) How can I find out that a declaration do not follow an assignment? Ex.given.
int i; // Wrong, give warning
int i = 0; // True usage
3) How can I find out that a document does not contain only 1 namespace or only 1 class inside it and how can I get their identifiers (names)?
True:
namespace Hello
{
class P{
}
}
- Wrong:
namespace Hi {
class C {
}
class E {
}
}
namespace Ho {
class D {
}
}
4) How can I find out function calls and find out where to? (i.e. Blocking a call to specific function)
Upvotes: 2
Views: 2558
Reputation: 20992
For #1, take a look at the Microsoft.StyleCop.CSharp.ReadabilityRules.CheckMethodParameters method implementation (either in Reflector or at http://stylecop.codeplex.com/SourceControl/changeset/view/64d44becb157#Project%2fSrc%2fAddIns%2fCSharp%2fAnalyzers%2fReadabilityRules.MethodParameters.cs).
For #2, something like the following should do the trick:
private bool VisitExpression(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, object context)
{
if (expression.ExpressionType == ExpressionType.VariableDeclarator)
{
VariableDeclaratorExpression declaratorExpression = (VariableDeclaratorExpression)expression;
if (declaratorExpression.Initializer == null)
{
this.AddViolation(parentElement, expression.LineNumber, "YourRule", declaratorExpression.Identifier.Text);
}
}
return true;
}
The existing SA1402 (FileMayOnlyContainASingleClass) and SA1403 (FileMayOnlyContainASingleNamespace) rules should take care of #3. If they don't work for your scenario, please specify what you would like a custom rule to do differently.
#4 should be an FxCop rule, not a StyleCop rule since it has nothing to do with source code style.
Upvotes: 4