Analyze This, Analyze That, Writing .NET Code Analyzers and Fixers
Today I'm highlighting two different posts on the same subject, one from Dave Fancher the other, Bill Wagner (whom I met and hung out with for a bit at the MVP Summit. He, and the other Language MVP's are quite a crew! lol)
Both focus on one of the cooler things coming with the new .NET Compiler Platform (fka Roslyn), the capability of creating diagnostic code analyzers and fixers.
Dave's post is a great tutorial on the what and how, Bill's is about a new community effort that aims to bring together a number of analyzers, fixers and refactorings.
Over the past several years, Microsoft has been hard at work on the .NET Compiler Platform (formerly Roslyn). Shipping with Visual Studio 2015 the .NET Compiler Platform includes a complete rewrite of the C# and Visual Basic compilers intended to bring features that developers have come to expect from modern compilers. In addition to the shiny new compilers the .NET Compiler Platform introduces a rich set of APIs that we can harness to build custom code diagnostic analyzers and code fix providers thus allowing us to detect and correct issues in our code.
When trying to decide on a useful demonstration for these features I thought of one of my coding pet peeves: using an if..else statement to conditionally set a variable or return a value. I prefer treating these scenarios as an expression via the conditional (ternary) operator and I often find myself refactoring these patterns in legacy code. It certainly would be nice to automate that process. It turns out that this is exactly the type of task at which diagnostic analyzers and code fix providers excel and we’ll walk through the process of creating such components in this post.
Before we can start, you’ll need to make sure you have a few things installed:
Once the above items are installed fire up Visual Studio 2015 and create a new project. The .NET Compiler Platform SDK Templates installer adds several extensibility templates for both C# and Visual Basic. For the purposes of this exercise, the template we’re interested in is the C# Diagnostic and Code Fix (NuGet + VSIX). This template includes everything we need to not only create the diagnostic analyzer and code fix provider, but also to test and deploy the assemblies. I named the solution “UseConditionalOperatorAnalyzer” but you may select the name of your choise.
The newly created solution should include the three projects listed below. If you’d prefer to download the full source, you can find it on Github.
- A Portable Class Library (PCL) that contains the code analyzer and code fix provider
- A unit test project
- A VSIX project that defines the Visual Studio extension
Once you’ve created the project (or cloned it from Github), turn your attention to the PCL project.
I won’t be addressing the unit test project in this article. The examples within the code are rather straightforward and should give you a good starting point without requiring any guidance. I do recommend playing with the test project a bit since it will greatly speed up detecting and resolving any problems you may encounter as you build your own analyzers and fix providers.
Writing the Diagnostic Analyzer
The .NET Compiler Services are a great addition to the .NET ecosystem. It has been my goal to demonstrate how, through aspects like the code analysis API and the syntax visualizer, we can create Visual Studio extensions with relative ease. The extensions we create can then guide us toward writing better code, uncovering patterns within our code, or even enforcing internal coding standards.
Now that you have been introduced to creating them, here's a project and repo for your next step.
The preview of Visual Studio 2015 is public, and the Roslyn APIs are stabilizing. A group of language MVPs have begun working on a series of code analyzers and code fix tools that will help you write better code. To facilitate this work, we’ve created an organization on Github, .NET Analyzers, that contain the repositories for the analyzers, code fix projects, and refactorings that we’ve created.
We’ve got seven code repositories in place already. There’s also a Proposals repository that has potential ideas for new code fix or analyzers projects.
The code repositories are varied, both in completeness and in scope. Some are for a single refactoring. Others will grow to a full suite of analyzers. Some are nearing completion. Others haven’t had any code uploaded yet.
Mark Michaelis and I started this group while at the MVP summit. Since then, I’ve been very impressed with the response. In particular, Sam Harwell, Tugberk Ugurlu, and Adam Speight have been working very diligently on several of the analyzers and code fix algorithms. There have been many others that have contributed and offered suggestions and help. I’m singling those three out because they have really spent significant time working on this.
We’d love to see other people get involved as well.
If you want to join us, check out the organization on GitHub. Look at the issues in the code repositories and contribute. Make your modifications, and submit a pull request. We are generally following Github flow, but that’s not a firm requirement. If you are new to Github, just ask one of us via Github messaging, and we’ll help walk you through it.
If you have an idea for something you want to implement, please add it as an issue to the Proposals repository...
Special note for the VB.NET community: We’re interested in having support for important VB idioms as well. However, we need the VB community to help us. Most of us are more involved in the C# community than the VB.NET community
An organization for the development of analyzers (diagnostics, code fixes, and refactorings) using the .NET Compiler Platform
This conversation has been locked by the site admins. No new comments can be made.