Useful resources to write Roslyn Analyzers
This post is part of the series 'Roslyn Analyzers'. Be sure to check out the rest of the blog posts of the series!
- Writing a Roslyn analyzer
- Writing language-agnostic Roslyn Analyzers using IOperation
- Working with types in a Roslyn analyzer
- Referencing an analyzer from a project
- Packaging a Roslyn Analyzer with NuGet package references
- Multi-targeting a Roslyn analyzer
- Roslyn analyzers: How to
- How to test a Roslyn analyzer
- Useful resources to write Roslyn Analyzers (this post)
Roslyn analyzers and source generators are useful but it's not easy to start writing them. The documentation is not at the same level as .NET or ASP.NET Core. There are some good resources on the internet. In this post, I share some resources to help you write Roslyn analyzers and source generators.
#View Syntax tree and IOperation tree
When you write a Roslyn analyzer, you need to understand the syntax tree and the IOperation tree to see what you should analyze. Here are some tools to help you visualize them:
- Syntax Visualizer: Visual Studio extension
- .NET Compiler Developer SDK: Visual Studio Code extension
- Sharplab.io
#Writing a syntax tree
When you write a Roslyn analyzer, you often need to write a code fix. It's not easy to write a code fix because you need to understand how to create a syntax tree. Roslyn Quoter (GitHub) transforms a given C# snippet into the syntax factory API calls to construct its syntax tree.
#Finding analyzer examples
The Roslyn documentation is not extensive. Sometimes, you can look at other Roslyn analyzer's implementations to find how to write your own. Here are some resources to find Roslyn analyzers:
- Roslyn Source Browser: Navigate into Roslyn source code.
- grep.app: Find code on GitHub. You can search for specific Roslyn methods or attributes to see how other projects use them.
- dotnet/roslyn-analyzers: Contains all the CA rules. There are lots of analyzers to look at!
#Which version of Roslyn is associated with which version of Visual Studio?
If you want to determine the minimal version of Roslyn to use in your analyzer, you can refer to the Roslyn version support documentation.
#Other resources
- Write your first analyzer and code fix
- Talk to the Roslyn team on the C# Discord
Do you have a question or a suggestion about this post? Contact me!