StyleCop 可以自动修复任何问题吗?
我决定开始在一个中型项目上运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不,令人沮丧。 这就是为什么 Style Cop 会适得其反。 一致的格式看起来不错,但对生产力没有什么好处(真正使代码难以阅读的是长度和间接< /a>)。 完美的间距不值得花费数小时手动调整线条。
当然,如果有一个自动化工具来实现一致的格式,我会在签入后将其连接起来,并让它帮助我。 但《Style Cop》是由虐待狂制作的。 他们的对话大概是这样的:
我讨厌风格警察。 它低于最低可行产品。
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:
I hate Style Cop. It's below a minimum viable product.
现在,您可以在项目中使用 StyleCop.Analyzers NuGet 包。
更新包可能会导致大量新警告。
Ctrl + 。 显示可用的快速修复。 修复可以立即应用于整个文档、项目或解决方案。
“预览更改”窗口可让您取消选择要手动解决的行。
CodeMaid 是一个很好的修复工具批量或自动格式化错误。
Nowadays you can use the StyleCop.Analyzers NuGet package in your projects.
Updating the package can result in a lot of new warnings.
Ctrl + . shows available quick fixes. A fix can be applied to the whole document, project or solution at once.
A Preview Changes window lets you deselect the lines to resolve manually.
CodeMaid is a good tool to fix formatting errors in batch or automatically.
这并不完全“自动修复任何问题”,但有一个 用于 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.
StyleCopFixer 自动修复 StyleCop 异常在 Visual Studio IDE 中,仅限 C#。
当前满足的错误为:
StyleCopFixer automatically fixes StyleCop exceptions within Visual Studio IDE for C# Only.
Errors currently catered for are:
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/
Code Maid 进行了一些 Stylecop 修复。
Code Maid does some Stylecop fixes.
不幸的是,现在我不知道有任何自动工具可以完全解决问题。
但是,如果您将 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.
如果您有 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.
StyleCop 关于实现规则自动修复的博客
另请查看 博客上有关集成到现有大型项目的条目。
注意:StyleCop 似乎只返回大约 1000 个警告,因此即使在修复堆之后您也会继续得到这个数字。
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.