Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Warning
titleWarning: This documentation concerns the outdated .NET ecosystem plugins version 2.1

New users as well as users of the recent versions of the C# plugins should consult this page.

 

 

 

 

 

 

The C# Ecosystem can be extended with custom coding rules either by:

  • defining your own rules them directly through the Sonar SonarQube web interface using XPath expressions
  • or by reusing custom rules defined in external tools such as FxCop, Gendarme or StyleCop

Extending Coding Rules using XPath

...

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

To navigate the AST, download the SSLR CSharp Toolkit.

Note
titleMigrating XPath rules from version 2.0 to 2.1+

Since version 2.1, grammar rule names are in uppercase, and each word in seperated by an underscore, such as: "COMPILATION_UNIT"
In version 2.0, camel cases was used, such as in "compilationUnit".
To convert XPath expressions written for version 2.0 to ones valid in 2.1, simply add underscores between words and uppercase.

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

To navigate the AST, download the SSLR CSharp Toolkit.

Reusing Custom Rules from External Tools

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

...

You can reference your custom rules from FxCop and/or Gendarme and/or StyleCop in SonarQube:

  1. Write a piece of XML to reference your custom coding rules. See formats below.
  2. Go to Settings > Configuration > General Settings > .Net FxCop or .Net Gendarme or C# StyleCop
  3. Paste your XML in .NET/C# XXX custom rules
  4. Click on Save .NET/C# XXX settings
  5. Restart your SonarQube server
  6. Activate your custom coding rules in your C# quality profiles

FxCop Format

Code Block
<?xml version="1.0" encoding="UTF-8"?><rules>

  <rule key="RULE_CLASS">
    <!-- name, configKey and description are mandatory -->
    <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>

  <rule key="... >
    ...
  </rule>

</rules>

RULE_CLASS being the : name of the .net NET rule class.
ASSEMBLY.DLL being the : name of the dll assembly file containing the rule class.
The XML fragment above assumes that the dll is located in the same directory as the standard fxcop FxCop assembly rules files.

Note: to help write this XML file, "vladonemo" has written a C# program that generates this file based on your custom rules DLL. You can check this out on GitHub, but be aware that it comes with no guarantee.

Example

Let's assume that you have implemented a rule that checks if whether a variable name follows a specific naming convention or not. You have implemented it in a class called "VariableConventionNameCheck", and you have compiled it into a "MyNamingConvetions.dll" assembly file. Then, you should write the following code snippet in the XML filepiece of XML:

Code Block
<?xml version="1.0" encoding="UTF-8"?>
<rules>
  <rule key="VariableConventionNameCheck">
    <name>Variable<!-- name, configKey and description are mandatory -->
    <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

...

Format

Code Block
<?xml version="1.0" encoding="UTF-8"?>
<rules>

 <rule <rule key="RULE_CLASS">
    <name><!<!-- name, configKey and description are mandatory -->
    <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>

  <rule key="... >
    ...
  </rule>

</rules> 

RULE_CLASS being the : name of the .net NET rule class.
ASSEMBLY.DLL being the : name of the dll assembly file containing the rule class. This file should be located in the installation directory of Gendarme, among the dll files provided by Gendarme out of the box. See the example of FxCop XML format, this it is the same principle.

StyleCop

...

Format

Example:

Code Block
Code Block
<?xml version="1.0" encoding="UTF-8"?>
<rules>

 <rule <rule key="ElementMustBeginWithUpperCaseLetter">
    <!-- name, configKey and description are mandatory -->
    <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>

  <rule key="... >
    ...
  </rule>

</rules>