如何在 StyleCop 设置和并发性中的单个"Custom Rules"节点下组合多个自定义 StyleCop 规则



基于几篇好文章,我已经能够成功地创建一些自定义的StyleCop规则。作为参考,这里列出了一些我发现对这个主题非常有用的文章:

  • 如何实现自定义样式警察规则
  • 为Microsoft源分析器创建自定义规则 - 第一部分
  • 为Microsoft源分析器创建自定义规则 - 第二部分
  • 为Microsoft源分析器创建自定义规则 - 第 III 部分

我正在使用Visual Studio 2010 Ultimate版本以及StyleCop版本4.4.0.14。

创建自定义 StyleCop 规则

需要创建类文件以及相应的 XML 文件,该文件用于将规则添加到 StyleCop 设置中。当我这样做时,我的所有自定义规则都会正确执行。但是,我不喜欢的是,在 StyleCop 设置树中,您最终会得到多个"自定义规则"节点,每个 XML 文件一个。

跳过不同规则的实现细节,这是我所做的。让我们将以下两个简单的自定义规则类及其相应的 XML 文件放在一起:

文件:自定义规则1.cs

namespace StyleCop.CustomRules
{
    [SourceAnalyzer(typeof(CsParser))]
    public class CustomRule1 : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            Param.RequireNotNull(document, "document");
            CsDocument csDocument = document as CsDocument;
            if ((csDocument.RootElement != null) && !csDocument.RootElement.Generated)
            {
                // Do something...
            }
        }
    }
}

文件:自定义规则2.cs

namespace StyleCop.CustomRules
{
    [SourceAnalyzer(typeof(CsParser))]
    public class CustomRule2 : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            Param.RequireNotNull(document, "document");
            CsDocument csDocument = document as CsDocument;
            if ((csDocument.RootElement != null) && !csDocument.RootElement.Generated)
            {
                // Do something...
            }
        }
    }
}

文件:自定义规则1.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Custom Rules">
  <Description>
    These custom rules provide extensions to the ones provided with StyleCop.
  </Description>
  <Rules>
    <Rule Name="CustomRule1" CheckId="CR1001">
      <Context>Test rule 1.</Context>
      <Description>Test rule 1.</Description>
    </Rule>
  </Rules>
</SourceAnalyzer>

文件:自定义规则2.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Custom Rules">
  <Description>
    These custom rules provide extensions to the ones provided with StyleCop.
  </Description>
  <Rules>
    <Rule Name="CustomRule2" CheckId="CR1002">
      <Context>Test rule 2.</Context>
      <Description>Test rule 2.</Description>
    </Rule>
  </Rules>
</SourceAnalyzer>

有了上述内容,我的所有(两个)规则都得到了正确执行。以下内容出现在 StyleCop 设置树中(方括号表示复选框):

[] C#
    [] {} Custom Rules
        [] {} CR1001: CustomRule1
    [] {} Custom Rules
        [] {} CR1002: CustomRule2
    [] {} Documentation Rules
    [] {} Layout Rules
    etc.

我想要的是将我的自定义规则放在 StyleCop 设置文件中一个名为"自定义规则"的节点下,如下所示:

[] C#
    [] {} Custom Rules
        [] {} CR1001: CustomRule1
        [] {} CR1002: CustomRule2
    [] {} Documentation Rules
    [] {} Layout Rules
    etc.

通过将两个 XML 文件合并为一个,我能够将这些规则合并到 StyleCop 设置中的单个"自定义规则"节点中,如下所示:

文件:自定义规则1.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Custom Rules">
  <Description>
    These custom rules provide extensions to the ones provided with StyleCop.
  </Description>
  <Rules>
    <Rule Name="CustomRule1" CheckId="CR1001">
      <Context>Test rule 1.</Context>
      <Description>Test rule 1.</Description>
    </Rule>
    <Rule Name="CustomRule2" CheckId="CR1002">
      <Context>Test rule 2.</Context>
      <Description>Test rule 2.</Description>
    </Rule>
  </Rules>
</SourceAnalyzer>

但是,一旦我这样做,只有一个自定义规则被执行,那就是 CustomRule1,即类(文件)名称与 XML 文件名匹配的规则。

我尝试在 CustomRule2 上设置属性以指示 XML 文件,如下所示:

namespace StyleCop.CustomRules
{
    [SourceAnalyzer(typeof(CsParser), "CustomRule1.xml")]
    public class CustomRule2 : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            Param.RequireNotNull(document, "document");
            CsDocument csDocument = document as CsDocument;
            if ((csDocument.RootElement != null) && !csDocument.RootElement.Generated)
            {
                // Do nothing.
            }
        }
    }
}

将上面所示的属性设置为 XML 文件也没有解决此问题。这两个规则都显示在 StyleCop 设置中,但只会执行 CustomRule1

我该如何解决这个问题?

更新:

根据接受的答案,我采用了在单个分析器中检查所有自定义规则的方法。

据我了解,每个表达式树步行者都在自己的线程上运行,因此在此过程中无法轻松共享状态。如果我采用使用单个分析仪的方法,是否可以安全地执行以下操作?

[SourceAnalyzer(typeof(CsParser))]
public class CustomRules : SourceAnalyzer
{
    private enum CustomRuleName
    {
        CustomRule1,
        CustomRule2
    }
    private CustomRuleName currentRule;
    public override void AnalyzeDocument(CodeDocument document)
    {
        Param.RequireNotNull(document, "document");
        CsDocument doc = document as CsDocument;
        // Do not analyze empty documents, code generated files and files that
        // are to be skipped.
        if (doc.RootElement == null || doc.RootElement.Generated)
        {
            return;
        }
        // Check Rule: CustomRule1
        this.currentRule = CustomRuleName.CustomRule1;
        doc.WalkDocument(VisitElement);
        // Check Rule: CustomRule2
        this.currentRule = CustomRuleName.CustomRule2;
        doc.WalkDocument(VisitElement);
    }
    private bool VisitElement(CsElement element, CsElement parentElement, object context)
    {
        if (this.currentRule == CustomRuleName.CustomRule1)
        {
            // Do checks only applicable to custom rule #1
        }
        else if (this.currentRule == CustomRuleName.CustomRule2)
        {
            // Do checks only applicable to custom rule #2
        }
    }
}

更新:

根据进一步的测试,上述是不安全的。不能使用实例字段来维护状态。

  1. 在具有多个源代码文件的项目上运行 StyleCop 时,多个线程将共享分析器的同一实例。

  2. 此外,给定以下代码,当调用方法时,多个线程和并发性也会在正在分析的每个源代码文档中发挥作用doc.WalkDocument(...)。每个表达式树步行者都在自己的线程上运行。

换句话说,除了可以在多个线程上同时分析多个源代码文件之外,回调VisitElementStatementWalkerExpressionWalker也在单独的线程上执行。

[SourceAnalyzer(typeof(CsParser))]
public class CustomRules : SourceAnalyzer
{
    public override void AnalyzeDocument(CodeDocument document)
    {
        Param.RequireNotNull(document, "document");
        CsDocument doc = document as CsDocument;
        // Do not analyze empty documents, code generated files and files that
        // are to be skipped.
        if (doc.RootElement == null || doc.RootElement.Generated)
        {
            return;
        }
        IDictionary<string, Field> fields = new Dictionary<string, Field>();
        doc.WalkDocument(VisitElement, StatementWalker, ExpressionWalker, fields);
    }
    private bool VisitElement(CsElement element, CsElement parentElement, object context)
    {
        // Do something...
        return true;
    }
    private bool StatementWalker(Statement statement, Expression parentExpression, Statement parentStatement, CsElement parentElement, object context)
    {
        // Do something...
        return true;
    }
    private bool ExpressionWalker(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, object context)
    {
        // Do something...
        return true;
    }
}

通常分析器包含多个规则(否则会很奇怪)。每个分析器在设置 UI 中显示为单独的节点。

如果要在设置 UI 中使用单个节点,则肯定只需要一个分析器,该分析器将执行这两项检查。

namespace StyleCop.CustomRules
{
    [SourceAnalyzer(typeof(CsParser))]
    public class MyCustomRules : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            // ...
            // code here can raise CR1001 as well as CR1002
        }
    }
}

文件:我的自定义规则.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="My Custom Rules">
  <Description>
    These custom rules provide extensions to the ones provided with StyleCop.
  </Description>
  <Rules>
    <Rule Name="CustomRule1" CheckId="CR1001">
      <Context>Test rule 1.</Context>
      <Description>Test rule 1.</Description>
    </Rule>
    <Rule Name="CustomRule2" CheckId="CR1002">
      <Context>Test rule 2.</Context>
      <Description>Test rule 2.</Description>
    </Rule>
  </Rules>
</SourceAnalyzer>

如果有人对此主题感兴趣,我已经制定了一些自定义规则,并且我能够对这些规则进行分组。事实上,如果有人有兴趣将参数传递给警告消息,我的规则也会:)这样做。

规则: 1. 命名空间必须以允许的公司名称开始 2. 字段名称必须以下划线开头

文件名: 样式扩展.cs

private bool VisitElement(CsElement element, CsElement parentElement, object context)
    {
        #region Namespace rules
        if (!element.Generated && element.ElementType == ElementType.Namespace)
        {
            var @namespace = element.Declaration.Name;
            var companyName = NamespaceUtils.GetNamespaceToken(@namespace, NamespaceTokenType.Company);
            //Flag a "Violation" is the element doesn't start with an allowed company name
            if (!NamespaceUtils.ValidateNamespaceCompany(companyName))
            {
                AddViolation(parentElement, element.LineNumber, "NamespaceMustBeginWithAllowedCompanyName", companyName);
            }
        #endregion
        #region Fields rules
        if (!element.Generated && element.ElementType == ElementType.Field)
        {
            var fieldName = element.Declaration.Name;
            // Flag a violation if the instance variables are not prefixed with an underscore.
            if (element.ActualAccess != AccessModifierType.Public &&
                element.ActualAccess != AccessModifierType.Internal &&
                fieldName.ToCharArray()[0] != '_')
            {
                AddViolation(parentElement, element.LineNumber, "FieldNamesMustBeginWithUnderscore", fieldName);
            }
        }

        #endregion
        return true;
    }

Xml 文件名: StyleCopExtensions.xml- 类似于下面发布的其他xml文件。- 您可以以相同的方式使用消息中发送的参数"字符串。Format()' 确实:只需在 xml 文件中包含 {0}、{1}、{N} 即可。

最新更新