Use StyleCop

Answered

Hello! I have some questions:

1. Is is possible to use StyleCop.Analyzers is Unity Rider project? Or unity project is not .NET core project?

2. Where should i put Settings.StyleCop file to override standart rules? And how can i apply this rule to format code?

0
2 comments

Hello!

1. Yes, it is possible to add Roslyn analyzers to a Unity project. However, it is not super easy, as Unity regenerates csproj files, and reference to Analyzer will be lost if you add it there. Please check out this how-to for a workaround

2. StyleCop.Analyzers package doesn't support Setting.StyleCop. You can use a .ruleset file instead. Put it hear the dll with analyzers as suggested in the workaround.

0
Avatar
Permanently deleted user

Your question was answered a year ago, but if anyone find this by googling later. I think a better solution would be to add project configuration in Directory.Build.props, this is where you can set your analyzer references, C# language version, link your stylecop.json and analyzers.ruleset.

 

In your project root folder:

Directory.Build.props

<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)analyzers.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="stylecop.analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="2.3.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>

This will set the above configuration to any csproj in your solution.

2

Please sign in to leave a comment.