Description
Hey,
I would like to write my own Razor analyzer. For example, consider this code:
<MyComponent>
<ComponentThatShouldNotBeHere/>
</MyComponent>
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:
- The code generator doesn't provide
#line
directives for mapping the location of<ComponentThatShouldNotBeHere>
, so the warning is inaccurate or references theg.razor.cs
file. - Without the exact location, I can't produce a code fix to remove
<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 of EditorRequired
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.
Activity