Configuring StyleCop
Cpt S 321 Washington State University
Let’s see what is our current configuration
• Right click on the project -> Properties -> Code Analysis
• By default you probably have the “Microsoft Managed Recommended
Rules” (that’s a good start!)
• Click on Open to see what rules are selected:
Don’t forget to commit that one with your HWs!
• Any change will result in the creation of a new rule set file that will be used for the project
• Warning • Error
• None
• Info
• Hidden • Inherit
The different areas of rules
• https://github.com/DotNetAnalyzers/StyleCopAnalyzer s/blob/master/DOCUMENTATION.md
Organize your error list in a way that make sense to you
Click right in the error list view and explore:
– “ Grouping” and – “Show Columns”
Fixing problems
1. Double-click on the warning/error to go on the source code line – the problematic part of the line will be underlined
2. Bring your cursor to the beginning of the line; a lightbulb will appear – click on the arrow to see possible automatic fixes
3. Fix the warning/error either by selecting one of the automatic fixes or by manually changing the code
stylecop.json
• Specify project-specific text, such as the name of the company, author name, and the structure to use for copyright headers
• For this class, we want to have your name and WSU ID • Fine-tune the behavior of certain rules
• Check the following slides to learn how to create and configure this file
Add stylecop.json configuration – option 1
• The easiest way to add a stylecop.json configuration file to a new project is using a code fix provided by the project.
• To invoke the code fix, open any file where SA1633 is reported and press Ctrl+. to bring up the Quick Fix menu.
• From the menu, select Add StyleCop settings file to the project.
Add stylecop.json configuration – option 2
• Add the following in your project: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/StyleCop.Analyzers/stylecop.json
• Then enable it (in Visual Studio 2017 and 2019; check here for other versions):
1. SelectthefileinSolutionExplorer.
2. Rightclick->Properties->setthevalueforBuildActionto:
• For most projects: ”AdditionalFiles” or “C# analyzer additional file”
An example of how to configure the stylecop.json
{
// ACTION REQUIRED: This file was automatically added to your project, but it will not take effect until additional steps are taken to enable it. See the // following page for additional information: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md
“$schema”: “https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json”, “settings”: {
“documentationRules”: {
“companyName”: “Venera Arnaoudova”,
“copyrightText”: “Copyright (c) {companyName}. All rights reserved.”, “documentPrivateElements”: true,
“documentPrivateFields”: true
}, “orderingRules”: {
“usingDirectivesPlacement”: “outsideNamespace” }
} }
More on: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Configuration.md
Exceptions to the rules: GlobalSuppressions.cs • An example:
• Justification is very important! You must explain the rationale behind making an exception.
Configuring the solution?
• Make sure StyleCop.Analyzers is activated/installed for all projects in your solution (Right click on solution -> Manage packages for the solution -> Installed -> All projects must be selected)
• Move the .ruleset file in the solution (next to the .sln file)
• Add the .ruleset file to the solution (Right click on solution -> Add ->
Existing item ->
IMPORTANT
• For ALL HWs:
• You must commit the 3 configuration files:
• rule set file (mandatory if you have changed the “Microsoft Managed Recommended Rules” configuration)
• stylecop.json (mandatory)
• GlobalSuppressions.cs (mandatory if you have made exceptions; if you
haven’t made exceptions this file does not exist)
• You must resolve all errors and warnings of your configuration before you submit your HWs!