-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to write my own Razor Analyzer #11400
Comments
Hi @tesar-tech , thanks for posting an issue for this! Currently writing custom analyzers for Razor is unsupported. We have looked into possibilities for adding them but have not prioritized that work. Unfortunately, Roslyn analyzers/code fixes do not support working on If you have specifics for what you're trying to achieve we might be able to help unblock you, but no promises. |
Hey, thanks for the response. I am working on creating a migration tool for the Blazorise component library to simplify the process of upgrading a codebase from version Examples of Breaking Changes
More examples of breaking changes can be found here: Blazorise Issue #5871 (note that this is not the complete list). GoalUsing Roslyn analyzers seems like a natural fit for this task because it allows users to stay in their IDE and work within an environment they’re already familiar with. Ideally, they could just press While not all breaking changes are automatically fixable, providing at least a warning with a link to relevant documentation would still significantly improve the upgrade experience. Challenges and Current ApproachUnfortunately, implementing the "perfect solution" entirely through Roslyn analyzers doesn’t seem feasible due to:
However, Roslyn analyzers are still helpful—they can accurately point out files where some action is necessary. Based on this, the current best approach appears to be developing a custom CLI tool.
Am I overlooking anything here, or do you see a better way to utilize Roslyn analyzers to make the migration tool? Any suggestions or ideas for improving this process would be greatly appreciated. Thank you for your time—I truly appreciate your help! |
If writing a Roslyn analyzer, then reporting diagnostics on the generated code is the appropriate action, and the IDE tooling will take care of mapping the location to the Razor file, and the Error List and squiggles will follow. Of course, if the location can't be mapped, then unfortunately you're out of luck. Code fixes are currently on a very small allowlist, and would only work for changes to the generated C#, not to the Razor file. Eventually we want to be able to remove this allowlist, but interpreting generated code changes such that Razor component elements change is unlikely to ever work. In the long run, it would be great to have an API for the Razor compiler so syntax trees can be used etc., analyzers and code fixes on additional files could be hooked up via Roslyn, etc. All good things, but sadly nothing we are able to prioritize right now. |
Hey,
I would like to write my own Razor analyzer. For example, consider this code:
My analyzer would detect and warn that the
<ComponentThatShouldNotBeHere>
should not be inside<MyComponent>
.I know I can use a Roslyn analyzer to check the generated code, but there are some challenges:
#line
directives for mapping the location of<ComponentThatShouldNotBeHere>
, so the warning is inaccurate or references theg.razor.cs
file.<ComponentThatShouldNotBeHere>
from the Razor file. I'm not even sure if code fixes for Razor files are possible with Roslyn analyzers.There are several Razor analyzers, such as this one for the
RZ2012
warning. This warning checks for the usage ofEditorRequired
without a specified parameter. It also supports code fixes, which is exactly what I want to achieve.How can I create similar custom behavior? Is it possible to write and use my own analyzer specifically for Razor files?
Additionally, this Stack Overflow question discusses similar topics.
I’m aware of projects like Meziantou.Analyzer, which provides warnings for Razor files. However, these are based on the generated code and don’t really offer mapping back to the Razor files or provide corresponding fixes.
Thank you.
The text was updated successfully, but these errors were encountered: