This week, I learned about Static Analysis Tool, and I practice applying it to my Shinny-SSG application. The two tools I used are Dotnet Format and StyleCopAnalyzers. I chose them because they are widely used and they are both analyzing source code based on the .editorconfig file.
Installation
EditorConfig
In the first step, I generated the .editorconfig file for my project. This Editorconfig file is where you define your coding style in Visual Studio, such as Indentation and Spacing or Wrapping Preference.
DotnetFormat
The second step is installing DotnetFormat tools in my local machine by this command:
tool install -g dotnet-format
This tool will format my code based on the style preferences on the .editorconfig file.
StyleCop.Analyzers
The third step is installing StyleCop Analyzers. As specified in the documentation, I followed the preferable way is to add the NuGet package StyleCop.Analyzers. I also found this blog is helping, and I followed step by step which the author recommended. In the Project File extension, I added this line to the project so it can trigger StyleCop at the build time:
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" Condition="$(MSBuildProjectExtension)=='.csproj'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
The last step is to edit my .editorconfig file to disable the rule that I don't want to my project, such as: Using directive should appear within a namespace declaration.
dotnet_diagnostic.SA1200.severity = None
However, at the build time of my project, it still showed a warning for this rule, so maybe I should research more about it.
Build Event
I was struggling to find a way to fix the warning from StyleCop for my project automatically. I had 360 warnings, and it will be time-consuming to fix them manually. Luckily, I read the blog of my classmate, Minsu Kim, he mentioned that dotnet format can be used with third-party analyzers. It is a great find, and I can use this command to fix all the warnings from style-cop:
-a warn ./shinny-ssg.sln
By using this command, I did not need to modify my Project file extension to trigger StyleCop.Analyzers at the build time and I can leave all the works to dotnet-format.
Microsoft Visual Studio has build events configuration for projects, and I used it to trigger auto-format code action at the time of building project. I have set up my project as follows:
After I ran the dotnet build in my project, two dotnet-format commands have fixed six formatting errors, and 360 warnings.
Editor/IDE Integration
Visual Studio provides a great way to replicate the development environment through a vsssetings file. New contributors to my project can have the same development environment as me by importing the ShinnySSG-2021-11-06.vssettings, which is available in the Project repository.
Conclusion
Formatting code is the first way to improve code quality, and it makes easier for reviewers to spot bugs and give suggestions. I am happy that I have found great tools to make this task much more easier.
Top comments (0)