如何配置C#解决方案以在任何违反代码样式的情况下失败?

发布于 2025-02-06 01:57:28 字数 3016 浏览 2 评论 0原文

我创建了一个示例Web API项目,并希望在CI管道期间执行编码惯例(如果样式错误在开发过程中出现,则奖励点奖励点)。基本上,我只想为C#项目设置Eslint(来自JavaScript世界)。

我想强制执行这些规则解决方案,因此我添加了a directory.build.props.props root目录中的文件中有以下内容(我想使用 Roslyn Analyzers 和在警告中失败)

<Project>
  <PropertyGroup Label="Compiler Settings">
     <EnableNETAnalyzers>true</EnableNETAnalyzers>
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <AnalysisMode>AllEnabledByDefault</AnalysisMode>
     <AnalysisLevel>latest</AnalysisLevel>
     <WarningLevel>5</WarningLevel>
  </PropertyGroup>
</Project>

接下来,我安装了 dotnet格式要访问dotnet格式./mysln.sln-verify-no-no-changes命令。

我将生成的 WeatherForeCastController 修改为

using Microsoft.AspNetCore.Mvc;
using System;

namespace LinterCheckSample.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        string unused = "";

        return
            new
                List
                <
                    WeatherForecast
                >
                ()
                {
new                                                 WeatherForecast() {}
                };
    }
    
    private void Do() { }
}

运行格式命令时,我期望以下内容,有些人被识别为

  • Sytem system
  • summaries 的冗余导入。 y
  • 从未使用摘要
  • _logger 的冗余数组创建表达式从不使用
  • 未使用的从不使用✅
  • weatherforecast ❌
  • 空间错误✅
  • 删除get方法中的空行
  • <代码>中的修复凹痕 get 方法❌
  • 方法do从不使用❌

我认为一件事是一件事我缺少一个.editorConfig文件?我复制粘贴 roslyn Analyzers .editorConfig .editorConfig 这是我的项目,这 如果我的方法是正确的方法,

我可以改善一些方法吗? (directory.build.props文件),是否有一个官方的超级严格.editorConfig?

如果我的方法是错误的,那么在C#世界中解决这个问题的正确方法是什么?

提前致谢

I created a sample Web API project and want to enforce coding conventions during my CI pipeline ( bonus points if the style errors come up during development ). Basically I just want to setup eslint ( from the JavaScript world ) for C# projects.

I want to enforce those rules solution wide so I added a Directory.Build.props file in the root directory with the following content ( I want to use the Roslyn Analyzers and fail on warnings )

<Project>
  <PropertyGroup Label="Compiler Settings">
     <EnableNETAnalyzers>true</EnableNETAnalyzers>
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <AnalysisMode>AllEnabledByDefault</AnalysisMode>
     <AnalysisLevel>latest</AnalysisLevel>
     <WarningLevel>5</WarningLevel>
  </PropertyGroup>
</Project>

Next I installed dotnet format to get access to the dotnet format ./MySln.sln --verify-no-changes command.

I modified the generated WeatherForecastController to

using Microsoft.AspNetCore.Mvc;
using System;

namespace LinterCheckSample.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        string unused = "";

        return
            new
                List
                <
                    WeatherForecast
                >
                ()
                {
new                                                 WeatherForecast() {}
                };
    }
    
    private void Do() { }
}

When running the format command I expected the following things, some got identified

  • Redundant import of System
  • Summaries is never used ✅
  • Redundant array creation expression for Summaries
  • _logger is never used ❌
  • unused is never used ✅
  • Redundant type specification for WeatherForecast
  • Whitespace errors ✅
  • Remove empty lines in Get method ❌
  • Fix indentation in Get method ❌
  • Method Do is never used ❌

I think the one thing I'm missing is an .editorconfig file? I copy pasted the Roslyn Analyzers .editorconfig into my project and this helped a little bit ( it didn't catch all of them )

If my approach is the correct way is there something I can improve? ( Directory.Build.props file ) and is there one official super strict .editorconfig?

If my approach is wrong, what is the correct way to solve this in the C# world?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2025-02-13 01:57:29

R#命令行工具不可能。

对于Roslyn分析仪,它可以帮助您在csproj文件中设置treatwarningsaserrors

<PropertyGroup>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

AFAIK that's not possible with R# Command Line Tools.

For Roslyn analyzers, it could help setting TreatWarningsAsErrors within your csproj file:

<PropertyGroup>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
娇柔作态 2025-02-13 01:57:29

不要更改您的.csproj文件。在解决方案目录中,添加a directory.build.props 文件以下文件:

<Project>
  <PropertyGroup Label="Compiler Settings">
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <AnalysisMode>AllEnabledByDefault</AnalysisMode>
     <WarningLevel>5</WarningLevel>
  </PropertyGroup>
</Project>

根据您的需求调整设置。

Don't change your .csproj files. In the solution directory, add a Directory.Build.props file with the following:

<Project>
  <PropertyGroup Label="Compiler Settings">
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <AnalysisMode>AllEnabledByDefault</AnalysisMode>
     <WarningLevel>5</WarningLevel>
  </PropertyGroup>
</Project>

Adjust the settings to your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文