基于Sonar和.editorconfig的代码lint
在项目根目录下创建Directory.Build.props文件,在里面引入SonarAnalyzer.CSharp包:
<Project> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <LangVersion>latest</LangVersion> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> </PropertyGroup> <ItemGroup> <PackageReference Include="SonarAnalyzer.CSharp" Version="9.28.0.94264"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> </Project>
在项目根目录下创建.editorconfig,里面定义好代码规范,然后执行dotnet build
即可。
Sonar中有些默认规则可能不符合组内规范,可以在.editorconfig中就行自定义配置。这两个工具结合起来就可以在本地执行lint也可以集成到CI流程中。
推荐阅读
enable code-style analysis on build
标签:enable,代码,lint,editorconfig,Sonar,build From: https://www.cnblogs.com/Cwj-XFH/p/18293470