Home > Mobile >  How to force dotnet format or MSBuild to use .editorconfig rules?
How to force dotnet format or MSBuild to use .editorconfig rules?

Time:09-19

When working with JavaScript or TypeScript projects I'm using Eslint to enforce code style and Prettier to enforce code format.

I want to achieve the same behaviour when working with C# projects, I created a new solution with a Web Api project. Inside the .csproj I added the property EnforceCodeStyleInBuild. I added a .editorconfig file to the solution

root = true

[*.cs]
dotnet_analyzer_diagnostic.category-Design.severity = error
dotnet_analyzer_diagnostic.category-Documentation.severity = error
dotnet_analyzer_diagnostic.category-Globalization.severity = error
dotnet_analyzer_diagnostic.category-Interoperability.severity = error
dotnet_analyzer_diagnostic.category-Maintainability.severity = error
dotnet_analyzer_diagnostic.category-Naming.severity = error
dotnet_analyzer_diagnostic.category-Performance.severity = error
dotnet_analyzer_diagnostic.category-SingleFile.severity = error
dotnet_analyzer_diagnostic.category-Reliability.severity = error
dotnet_analyzer_diagnostic.category-Security.severity = error
dotnet_analyzer_diagnostic.category-Style.severity = error
dotnet_analyzer_diagnostic.category-Usage.severity = error
dotnet_analyzer_diagnostic.category-CodeQuality.severity = error

# prefer 'var'
dotnet_diagnostic.IDE0007.severity = error
dotnet_diagnostic.IDE0008.severity = none

# Use expression body for methods
dotnet_diagnostic.IDE0022.severity = suggestion

I also installed dotnet format to inspect the format via dotnet format --verify-no-changes after building the solution.

This setup works pretty solid but there are some things I'm struggling with, for testing purposes I changed the sample class WeatherForecastController to

using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public sealed class WeatherForecastController : ControllerBase
    {
        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<int> Get()
        {
            // use var instead
            string foo = "not using var";
            Console.WriteLine(foo);


            // remove some empty lines






            // add some linebreaks and remove redundant select statements
            return Enumerable.Range(1, 5).Select(x => x).Select(x => x).Select(x => x).Select(x => x).Select(x => x).Select(x => x).ToArray();
        }
    }
}

I added some comments to show the expected behaviour. Does someone know if I'm missing something or how to achieve it? At least I would expect it to fail because it should use var for the variable.

As a sidenote: I'm using Rider instead of Visual Studio but I think this shouldn't matter ( some people also use Visual Studio Code ).

CodePudding user response:

Try to configure this rule with the following options in your .editorconfig:

csharp_style_var_for_built_in_types = true 
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true

See the reference:
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0007-ide0008

  •  Tags:  
  • c#
  • Related