5

My Goal

My team decided, that they wanted to incorporate automation to enforce coding guidelines in .razor files. Since we're already using StyleCopAnalyzers, I might implement our own Analyzer to achieve this goal.

Example

To better make you understand what's in my mind, consider the following example.

Let the following be valid code:

<MyGrid>
    <MyItem></MyItem>
    <MyItem></MyItem>
    <MyItem></MyItem>
</MyGrid>

Now, If someone wouldn't encapsulate <MyItem> within <MyGrid> it would cause the Analyzer to report a diagnostic like "MyItem should be direct child of a MyGrid".


What I tried

Firstly, I followed microsoft's tutorial: Write your first analyzer and code fix. This worked perfectly well.

However, my next step was to create a Sample Blazor WASM Application using the default template. When I executed the MakeConstAnalyzer on the Sample Blazor Application, I'd get the following behavior.

Modifying Program.cs like this:

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("#app");

        // I get squiggly lines here with the expected analyzer message from the MakeConstAnalyzer
        int bla = 0; 

        builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

        await builder.Build().RunAsync();
    }
}

But modifying Counter.razor like this:

private void IncrementCount()
{
    int bla = 0; // add this line
    currentCount++;
}

now, this won't show the analyzers message. Of course this will trigger a compiler warning, but mainly this tells me, that for some reason the Analyzer isn't executed on the Counter.razor file.

The Microsoft.CodeAnalysis.Razor Package

I thought, I'd mention it here. I found it on Nuget and tried to integrate it in the sample.

Caution

I have no clue, If the purpose of the package is even remotely connected, to what I want to achieve, since I really didn't find any documentation.

Since the microsoft tutorial clearly states, that any Analyzer must in some way inherit DiagnosticAnalyzer, I also checked the package for an implementation. But in both, the Microsoft.CodeAnalysis.Razor and Microsoft.AspNetCore.Razor.Language Package there is no class, that inherits from DiagnosticAnalyzer.

I've also tried to decorate my Analyzer, to specify, that it should also parse *.razor files, like that:

[DiagnosticAnalyzer(LanguageNames.CSharp, Microsoft.CodeAnalysis.Razor.RazorLanguage.Name)] 

Summary

At this point, I don't know, what else to try. Also I didn't find anything remotely like that in the web.

These are my Questions:

  • How can I instruct my Analyzer to also parse .razor files?
  • Also how would I access the razor code in that approach?
    I can imagine to either use Analyzers Syntax to traverse the Component Tree, but I think I could realize most of my usecases by only having the plain unparsed code.
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • Hi, diving into similar issue - did you manage to get anywhere. Could use some help. – Frank Nov 06 '21 at 17:33
  • 1
    hi Frank, I didn't exactly end up doing "razor analyzers". Instead, we ended up extending our architecture tests (they test stuff like forbidden references between projects). We now have an architecture test that verifies, that all our razor files satisfy our rules. So we simply wrote a generic set of tests, using standard html parsers and ran them on all the razor files we could find in our solution. Using xbehave, we managed to identify pretty well, which rule failed on what file. However certain things are very hard to impossible to check using this approach. Does this help? – LuckyLikey Nov 19 '21 at 16:41

0 Answers0