Is there a comprehensive C# linter and formatter like eslint for JS/TS?

Ale*_*tov 11 .net c# lint code-formatting

I'd like to have my C# projects (.NET Core 3.1+) to be linted and formatted on each build both locally and in CI environment. I know that there's new .NET Analyzers feature and dotnet-format tool in .NET 6, but I can't understand from the documentation if I can make a single comprehensive configuration file that both these tools would use so I can enforce certain code style in my team. Could you help me understand if it is possible?

Joh*_*lly 19

是的,有 - Roslyn 分析仪。

在您的 中设置一个EnforceCodeStyleInBuild元素.csproj:

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>

    <!-- this! -->
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
  </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

您项目中的一个.editorconfig文件,您可以这样获取:

dotnet new editorconfig
Run Code Online (Sandbox Code Playgroud)

并更新您的 VS Codesettings.json以包括:

dotnet new editorconfig
Run Code Online (Sandbox Code Playgroud)

你应该去参加比赛了!我在这里更深入地写了这一点:

https://johnnyreilly.com/eslint-your-csharp-in-vs-code-with-roslyn-analyzers

  • @Dorklord如果我没有记错的话最新版本的 Visual Studio(2022 年,也许 2019 年)可以理解 `.editorconfig`。 (3认同)