Skip to content

How to write my own Razor Analyzer #11400

Open
@tesar-tech

Description

@tesar-tech

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 the g.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-compilerUmbrella for all compiler issuesfeature requestLarge improvement request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions