如何为 SonarQube 创建自己的 C# 自定义规则? [英] How can I create my own C# custom rules for SonarQube?

查看:54
本文介绍了如何为 SonarQube 创建自己的 C# 自定义规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些研究.我发现了一个非常好的示例列表,但对于 其他 语言 这里.

I've been doing some research on it. What I found is a list of quite nice samples but for other languages here.

我还查看了 sonar-dotnet.但它看起来与其他实现不相似.

I also looked at sonar-dotnet. But it doesn't look similar to the other implementations.

最后,老实说,这可能是我最后一次机会了,我快速浏览了 FxCop 自定义规则,但我不确定哪种方法是正确的.

Finally, and to be honest probably my last chance, I took a quick look at FxCop Custom Rules and I'm not sure what would be the right way.

我想要做的只是一个基本的 c# 规则,可以像 this由声纳预定义.

What I'm trying to do is just a basic c# rule that can be reviewed like this predefined by sonar.

我的意思是,不合规代码合规解决方案.

推荐答案

您所指的sonar-custom-rules-examples都是用Java编写的,并且针对各种目标使用Java编写的解析器语言.C# 和 VB.NET 的 sonar-dotnet 分析器是使用 C# 编写的,使用 Roslyn 框架由微软提供.

The sonar-custom-rules-examples you pointed at are all written in Java and use parsers written in Java for the various target languages. The sonar-dotnet analyzers for C# and VB.NET are written in C# using the Roslyn framework provided by Microsoft.

如果您想为 C# 编写自己的自定义规则,那么编写 Roslyn 分析器绝对是最简单的方法(Roslyn 替换了现在已过时的 FxCop).但是,有许多免费的第三方 Roslyn 分析器可用,因此可能有人已经编写了至少一些您想要的规则.查看 NuGet 了解可用的内容.

If you want to write your own custom rules for C# then writing a Roslyn analyzer is definitely the easiest way to do it (Roslyn replaced FxCop, which is now obsolete). However, there are dozens of free third-party Roslyn analyzers available, so it's possible that someone has already written at least some of the rules you want. Have a look on NuGet to see what's available.

接下来,您希望 Roslyn 分析器提出的问题出现在 SonarQube 中.如果您使用的是新版本的 SonarQube (v7.4+)、SonarScanner for MSBuild (v4.4+) 和 SonarC# 插件 (v7.6+),那么第三方 Roslyn 分析器引发的问题将自动被导入为一般问题.有关详细信息,请参阅文档.

Next, you want issues raised by a Roslyn analyzer to appear in SonarQube. If you are using new-ish versions of SonarQube (v7.4+), the SonarScanner for MSBuild (v4.4+) and the SonarC# plugin (v7.6+), then issues raised by third-party Roslyn analyzers will automatically be imported as generic issues. See the docs for more info.

一般问题有几个重大限制,就像无法选择在 SonarQube UI 中运行的规则一样.如果您想要更全功能的体验(或者如果您使用的是旧版本的 SonarQube),您可以使用 SonarQube Roslyn SDK 生成一个自定义的 SonarQube 插件,该插件包装了 Roslyn 分析器.使用 SDK 很简单:它是一个针对 Roslyn 分析器运行的 exe,它会为您生成一个 SonarQube 插件 jar.

Generic issues have a couple of significant limitations, just as not being able to select which rules to run in the SonarQube UI. If you want a more full-featured experience (or if you are using an older version of SonarQube), you can use the SonarQube Roslyn SDK to generate a custom SonarQube plugin that wraps the Roslyn analyzer. Using the SDK is straightforward: it's an exe that you run against the Roslyn analyzer, and it generates a SonarQube plugin jar for you.

这篇关于如何为 SonarQube 创建自己的 C# 自定义规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆