Static Analysis with Roslynator and Dotnet-format
Published: 2023-11-04 03:58:18

This week I used [dotnet-format](https://github.com/dotnet/format) and [Roslynator](https://github.com/dotnet/roslynator) as a formatter and linter for my [project](https://github.com/Amir-Helali/Txt2StaticHtml). Also, since this is an open source project I finally made a [CONTRIBUTING.md](https://github.com/Amir-Helali/Txt2StaticHtml/blob/main/CONTRIBUTING.MD) file for it, which explains all contribution guidelines and the set up process.

## Dotnet-Format
dotnet-format is a code formatter for the .Net environment. I chose it because I used C# for my project. Setting it up was very easy, I installed it by running the command: `dotnet tool install -g dotnet-format --version "8.0.446201" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json`. However, I had to use an older [version](https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet6/NuGet/dotnet-format/versions/6.4.437406) since I had .NET 6 installed. After installation I simply ran it with the command `dotnet format` from the command line in my project's directory and by default it fixed the indentation and white-space formatting issues in my code. It can also be customized to follow formatting preferences by including a _.editorconfig_ file in the project. 

## Roslynator
To choose a linter I had to do some research about which one to use, since I have never used one before. Finally, I decided to go with Roslynator because it has everything I needed. It has extensions for the IDEs I use, it has a [command-line](https://josefpihrt.github.io/docs/roslynator/cli/) tool and it offers more than 200 [analyzers](https://www.nuget.org/packages/Roslynator.Analyzers) for C# which is extremely useful. Like dotnet-format, setting it up was very easy, I installed the command line tool by running the command: `dotnet tool install -g roslynator.dotnet.cli`, then I used the command `roslynator analyze` where it gave me feedback on what I needed to fix, which I did. The feedback it provided can be found in this picture:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnmag6huekylarmvhuj5.png). There is also the `roslynator fix` command that can fix the issues it finds. Also, to integrate the tool with my IDEs I used both extensions for [Visual Studio](https://marketplace.visualstudio.com/items?itemName=josefpihrt.Roslynator2022) and [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=josefpihrt-vscode.roslynator). I used the default configuration, but it can be customized by editing the _.editorconfig_ file, the configuration guide can be found [here](https://josefpihrt.github.io/docs/roslynator/configuration/).

## What I learned
This was my first time using a formatter and linter. I can see how useful they can be specially if there is an open source project with many people working on it. They allow you to automate a set of preferences and standards that applies to everyone's work and that is extremely helpful in a collaborative environment. Also, after adding Roslynator, I realized that I could've just used that as my formatter instead of dotnet-format, but I added 2 new tools to my tool set so no regrets here.