Search code examples
visual-studiostylecop

Customize StyleCop by adding StyleCop.json


I want to customize StyleCop rules, for example, by default StyleCop requires all using directives to be added inside the namespace:

enter image description here

In my project, I have disabled this rule... now I want to to modify this rule (or add a new rule) which would require:

all using directives should be defined outside of namespaces

I have seen this document, which explains this customization can be done by adding StyleCop.json:

The easiest way to add the file is to open a file that violates rule SA1633. This is the rule that requires a file header (e.g., a copyright notice). Press “CTRL + .” and you’ll see an option to add the file:

And then this can be added to StyleCop.json:

"orderingRules": {
    "usingDirectivesPlacement": "outsideNamespace"
}

"CTRL + ." does not open any quick action for me, and I am not clear how/where I should add StyleCop.Json to my project?


Solution

  • The problem was I had installed, StyleCop using StyleCop Installer.

    This document is expecting that StyleCop is installed using StyleCop.Analyzers Nuget package.

    I am not sure how to Customize StyleCop, when installed using StyleCop Installer, but according to Wikipedia, StyleCop.Analyzers is the best option, from VS 2015 and going forward.