StyleCop 可以自动修复任何问题吗?

发布于 2024-07-19 05:50:12 字数 111 浏览 7 评论 0原文

我决定开始在一个中型项目上运行 StyleCop,但收到了 1000 多个警告。

有没有一种快速简单的方法来修复大多数这些警告? 大多数警告都可以很容易地自动解决,这会节省我很多时间。

I've decided to start running StyleCop on a medium sized project and am getting over 1000 warnings.

Is there a quick easy way to fix most of these warnings? Most of the warnings could easily have been automated to resolve and would save me a lot of time.

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

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

发布评论

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

评论(9

迷迭香的记忆 2024-07-26 05:50:13

不,令人沮丧。 这就是为什么 Style Cop 会适得其反。 一致的格式看起来不错,但对生产力没有什么好处(真正使代码难以阅读的是长度和间接< /a>)。 完美的间距不值得花费数小时手动调整线条。

当然,如果有一个自动化工具来实现一致的格式,我会在签入后将其连接起来,并让它帮助我。 但《Style Cop》是由虐待狂制作的。 他们的对话大概是这样的:

Bob:使用模式匹配,软件可以识别丑陋的代码
查理:——并修复它!
鲍勃:不,它会打印有关每次违规的消息
查理:——然后用户右键单击即可修复所有问题?
鲍勃:不,它责备他们“逗号周围的间距无效”
查理:——然后解释如何解决它?
鲍勃:不,那是在单独的文档中
查理:——在 Google 上?
鲍勃:没有。

我讨厌风格警察。 它低于最低可行产品。

No, frustratingly. That's why Style Cop is counter-productive. Consistent formatting looks nice, but there's little productivity benefit to it (what really makes code hard to read is length and indirection). Perfect spacing is not worth spending hours adjusting lines by hand.

Of course, if there were an automated tool to achieve consistent formatting, I'd hook it up post-check-in, and let it help me. But Style Cop is made by sadists. Presumably their conversation went like this:

Bob: Using pattern matching, the software identifies ugly code
Charlie: —and fixes it!
Bob: No, it prints a message about each transgression
Charlie: —and then the user right-clicks to fix them all?
Bob: No, it scolds them 'invalid spacing around the comma'
Charlie: —and then explains how to fix it?
Bob: No, that's in a separate document
Charlie: —on Google?
Bob: No.

I hate Style Cop. It's below a minimum viable product.

追星践月 2024-07-26 05:50:13

现在,您可以在项目中使用 StyleCop.Analyzers NuGet 包。

NuGet 包管理器available StyleCop.Analyzers update

更新包可能会导致大量新警告。

Visual Studio 错误列出“文档文本应以句点结尾”的警告

Ctrl + 显示可用的快速修复。 修复可以立即应用于整个文档项目解决方案

Visual Studio 快速标记为“修复所有出现的问题”选项

“预览更改”窗口可让您取消选择要手动解决的行。

窗口预览更改- 修复所有出现的问题

CodeMaid 是一个很好的修复工具批量或自动格式化错误。

Nowadays you can use the StyleCop.Analyzers NuGet package in your projects.

NuGet Package Manager available StyleCop.Analyzers update

Updating the package can result in a lot of new warnings.

Visual Studio Error List warnings of 'Documentation text should end with a period'

Ctrl + . shows available quick fixes. A fix can be applied to the whole document, project or solution at once.

Visual Studio quick fix. Marked 'Fix all occurrences in' options

A Preview Changes window lets you deselect the lines to resolve manually.

Window Preview Changes - Fix all occurrences

CodeMaid is a good tool to fix formatting errors in batch or automatically.

溺渁∝ 2024-07-26 05:50:13

这并不完全“自动修复任何问题”,但有一个 用于 Github 会自动突出显示代码中的样式错误。

更新:根据 StyleCop for ReSharper 网站, ReSharper 现在为 StyleCop 提供了越来越多的快速修复错误。 您从列表中选择一个快速修复,问题将自动解决。

This doesn't exactly "automatically fix anything" but there is a StyleCop plug-in for Resharper on Github that will automatically highlight the style errors in your code.

Update: According to the StyleCop for ReSharper website, ReSharper now provides an increasing number of Quick-Fixes for StyleCop errors. You select a quick-fix from the list and the problem will be resolved automatically.

鹊巢 2024-07-26 05:50:13

StyleCopFixer 自动修复 StyleCop 异常在 Visual Studio IDE 中,仅限 C#

当前满足的错误为:

  1. SA1001
  2. SA1516
  3. SA1513
  4. SA1508
  5. SA1507
  6. SA1507 SA1505
  7. SA1309
  8. SA1210
  9. SA1210
  10. SA11202 SA11200
  11. SA11106
  12. SA1025
  13. SA1025
  14. SA1013 SA1013
  15. SA11012
  16. SA11009 SA1009
  17. SA1009
  18. SA1633 SA1633
  19. SA1003 SA1005
  20. SAS1600
  21. 适用于物业)

StyleCopFixer automatically fixes StyleCop exceptions within Visual Studio IDE for C# Only.

Errors currently catered for are:

  1. SA1001
  2. SA1516
  3. SA1513
  4. SA1508
  5. SA1507
  6. SA1505
  7. SA1309
  8. SA1210
  9. SA1202
  10. SA1200
  11. SA1106
  12. SA1101
  13. SA1025
  14. SA1013
  15. SA1012
  16. SA1009
  17. SA1008
  18. SA1633
  19. SA1003
  20. SA1005
  21. SA1600 (for properties only)
  22. SA1121
对你再特殊 2024-07-26 05:50:13

codeplex 中有一个项目可以根据 stylecop 警告进行一些自动修复。 但它并不完整......

请参阅:http://stylefix.codeplex.com/

There is a project in codeplex which does some automatic fix based on stylecop warning. It is not complete however...

see: http://stylefix.codeplex.com/

一曲琵琶半遮面シ 2024-07-26 05:50:13

Code Maid 进行了一些 Stylecop 修复。

Code Maid does some Stylecop fixes.

意中人 2024-07-26 05:50:13

不幸的是,现在我不知道有任何自动工具可以完全解决问题。

但是,如果您将 Visual Studio 的格式化选项(以及 Resharper 的,如果有的话)设置为代码格式化以匹配样式警察指南,则清理会变得更容易一些。

完成此操作后,您可以进入文件并执行“编辑”->“高级”->“格式化文档”(或键盘快捷键,这取决于您的快捷键设置 - 我的是 Ctrl+K、Ctrl+D)。 这将修复相当多的 style cop 警告,特别是如果您有 resharper + stylecop 插件。 它不会修复所有问题,但在许多情况下,它可以将文件中的警告数量从数百个减少到数十个。

Unfortunately, right now, I don't know of any automatic tools to fix things entirely.

However, if you setup Visual Studio's formatting options (and Resharper's, if you have that) for code formatting to match the style cop guidelines, cleanups get a bit easier.

Once you've done that, you can go into a file and do Edit->Advanced->Format Document (or the keyboard shortcut, which varies with your shortcut settings - mine is Ctrl+K, Ctrl+D). This will fix quite a few of the style cop warnings, especially if you have resharper + the stylecop plugin. It won't fix all of them, but in many cases, it's taken the number of warnings in a file from hundreds down to tens.

浊酒尽余欢 2024-07-26 05:50:13

如果您有 Resharper,您可以下载 Resharper 的 StyleCop 插件,该插件具有一些自动修复功能。
Codeplex 上的 StyleCop for Resharper。

If you have Resharper you can download a StyleCop plugin for Resharper which features some automatic fixes.
StyleCop for Resharper on codeplex.

薯片软お妹 2024-07-26 05:50:13
  1. 关闭警告! 不推荐,但您可以在短期内使用它,直到解决大部分问题。
  2. 编写一个程序来修复一些更简单的警告,或者使用查找/替换
  3. 使用代码片段来帮助处理文件头文档等问题

StyleCop 关于实现规则自动修复的博客

另请查看 博客上有关集成到现有大型项目的条目

注意:StyleCop 似乎只返回大约 1000 个警告,因此即使在修复堆之后您也会继续得到这个数字。

  1. Turn off the warnings! Not recommended, but you could use this in the short term until you get a bulk of the issues resolved.
  2. Write a program to fix some of the simpler warnings, or use find/replace
  3. Use code snippets to help with things like file header documentation

There was mention on the StyleCop blog regarding implementing auto fixing for rules

Also check out the entry on the blog regarding integrating in to an existing large project.

Note: It seems that StyleCop only returns around 1000 warnings, so you will continue to get this number even after fixing a heap.

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