DEV Community

Minsu Kim
Minsu Kim

Posted on • Updated on

Formatter & Linter for C#

This week, I have added Static Analysis Tool on my Static Site Generator(ssg) & commit. 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

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

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);
            }
        }
Enter fullscreen mode Exit fullscreen mode

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 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);
            }
        }


Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)