Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Just as the xml rules engines, the C# rules engines can be extended with custom rules.
The process is pretty simple. Once you have packaged your custom rule in a dll file and added it to your FxCop/Gendarme installation, you need to add an XML file describing this rule in the "$SONAR_HOME/extensions/rules/ENGINE" where $SONAR_HOME is the root directory of your sonar installation and ENGINE is either "fxcop" or "gendarme".
Below details on the format of these files.

FxCop XML format

Code Block
<rules>
  <rule key="RULE_CLASS">
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@$(FxCopDir)\Rules\ASSEMBLY.DLL]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>
</rules>

...

Code Block
<rules>
  <rule key="VariableConventionNameCheck">
    <name>Variable name conventions</name>
    <configKey><![CDATA[VariableConventionNameCheck@$(FxCopDir)\Rules\MyNamingConvetions.dll]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ This rule checks that variables follow the company naming conventions ]]></description>
  </rule>
</rules>

Gendarme XML format

Code Block
<rules>
  <rule key="RULE_CLASS">
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@ASSEMBLY.DLL]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>
</rules>

...

See the example of FxCop XML format, this is the same principle.

StyleCop XML format

Example:

Code Block
<rules>
  <rule key="ElementMustBeginWithUpperCaseLetter">
    <name><![CDATA[Element must begin with upper case letter]]></name>
    <configKey><![CDATA[Microsoft.StyleCop.CSharp.NamingRules#ElementMustBeginWithUpperCaseLetter]]></configKey>
    <category name="Usability" />
    <description><![CDATA[Element must begin with upper case letter because ....]]></description>
  </rule>
</rules>

Extending Coding Rules using XPath (available in 2.0)

New coding rules can be added using XPath. See the related documentation.

To navigate the AST, download the SSLR CSharp Toolkit.