Formatter & Linter for C#
Tags: opensource
Published: 2021-11-04 08:29:39

This week, I have added **Static Analysis Tool** on my [Static Site Generator(ssg)](https://github.com/mkim219/kimchi-ssg) & [commit](https://github.com/mkim219/kimchi-ssg/commits/main). Using static analysis tool is critical in collaboration with other developers in same project because static analysis tool helps to maintain the quality of source code. The static analysis tools that I used for my ssg are: 

* [dotnet-format](https://github.com/dotnet/format)
* [StyleCopAnalyzers](https://github.com/DotNetAnalyzers/StyleCopAnalyzers)

### dotnet-format
`dotnet-format` is a code formatter for applying style to solution to `dotnet`. 

#### Installation
The installation of `dotnet-format` can be done multiple ways. 

* `Nuget packages for solution`
* `Nuget package manager console`
* `cmd`

Installation via `Nuget packages for solution` in `Visual Studio 2019` has versioning issue that requires `.NET6.0` which is latest version and I am using `.NET5.0`. Therefore I have installed `dotnet-format` via `Nuget package manager console` in `Visual Studio 2019` with `Install-Package StyleCop.Analyzers -Version 1.1.118`. Installation for `cmd` can be done with `dotnet tool install --global dotnet-format --version 5.1.250801`.

#### Implementation 
The implementation is very straightforward. The user can create `.editorconfig` where should places same directory of `sln` file which helps users to visualize the formatting and code style in `visual studio 2019`.
![editorconfig](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3x9w62bqbfpk4e4ro0kx.PNG)

After I have verified the configuration on ` .editorconfig`. I have to create simple "one-step" solution for running formmater on the entire project form the command line. 
```
public static void FixFormat()
        {
            var directory = TryGetSolutionDirectoryInfo();
            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                FileName = "dotnet-format",
                Arguments = $"{directory}/kimchi-ssg.sln",
            };
            try
            {
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
```
The command for diagnose and fix the format is `dotnet-format <workspace>`. The `workspace` is the directory where the `sln` places in. Therfore `TryGetSolutionDirectoryInfo()` is iterative method for searching `sln` file in parent directory. The `FixFormat()` function places inside of `main` for invoking corresponding formatting command.

### StyleCopAnalyzers
`StyleCop` is a C# source code analyzer that allows to diagnose lint. The `styleCop` analyzer itself does not have auto-linting with command line. However, the `dotnet-format` supports to diagnose and fix the error with 3rd party analyzer.

#### Installation 
The installation of `StyleCopAnalyzers` can be done via

* `Nuget packages for solution`
* `Nuget package manager console`

On the `Visual studio 2019`, you can search the `StyleCopAnalyzers` in the `Nuget packages for solution` and enter the command `Install-Package StyleCop.Analyzers -Version 1.1.118` on `Nuget package manager console`

#### Implementation
The command in `dontet-format` that support 3rd party analyzer to diagnose the lint and fix is `dotnet-format -a <severity> <workspace>` where `<workspace>` is the directory that has `sln` file and `--severity` can be refer this [page](https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019) for explanation. Since the error detects from IDE, I have used ```severity warn`. 

```
public static void FixLint()
        {
            var directory = TryGetSolutionDirectoryInfo();
            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                FileName = "dotnet-format",
                Arguments = $"-a warn {directory}/kimchi-ssg.sln",
            };
            try
            {
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
```
The `FixLint()` function has similar logic with `FixFormat()`.

### Editor/IDE Development Environment
For the sharing development environment with all contributors that work with the project, the `Visual Studio 2019` provides the `vssettings` file. On the `Visual Studio 2019`, user can export their environment setting to share with other contributors. The contributors just import `vssettings` file to work same development enviroment. 

### Reflection 
Throughout this weekly assignment, as I mentioned many time previous blog post, it is very important to read document that involves project and package. I thougt C# does not have the tool and package that supports to auto-linting with command line. However I carefully read the document of `donet-format and there is a feature to support for auto-linting with 3rd part analyzer. Again, it is best practice to read the document when I lost. 

After auto-linting and auto-formatting, I realized I have a lot of formatting and linting issue in my source code. I think it is very important to write the code with formatter and linter for clean code.