I can't seem to find a good tutorial to add some custom styling rules that are not already customizable in StyleCop

For example, creating a rule to check for N levels of nested blocks in loops or control flow statements, e.g nested code with 3 levels of depth:
.
I have found an old tutorial using VS 2008 https://subscription.packtpub.com/book/programming/9781782169543/1/ch01lvl1sec16/creating+custom+rules+(intermediate) but I believe it is not possible to do it in a similar fashion nowadays, and I cannot find anything relevant in the docs https://github.com/DotNetAnalyzers/StyleCopAnalyzers. I'm using Visual Studio 2022 in a .NET Core 6 C# project with the above-mentioned package. I would appreciate if you could share some insights about this. Thanks, in advance!
I've tried referencing these tutorials but I believe they don't apply for current VS 2022 setup with .Net 6
https://www.planetgeek.ch/2009/07/19/custom-stylecop-rules-2/
That's becuse the
StyleCop.Analyzerspackage isn't extensible (i.e. you can't):https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2265
But don't get disheartened: you're having trouble finding relevant info because you're searching specifically for "StyleCop", but StyleCop (as in, the original name for the original tooling from 2005-2012) is from before Roslyn was introduced and is entirely obsolete today. Instead search for "roslyn" instead of "stylecop" and you'll get relevant results.
So what you actually want to do is write a Roslyn syntax analyzer that inspects the structure of the syntax-tree of a
.csfile, which is documented here - and here is a quickstart/sample/template project for creating a custom syntax analyzer (permalink). And here's a random guide I found that's still relevant today.StyleCop.Analyzerspackage, you should remove that package reference.Microsoft.CodeAnalysis.CSharp.Workspaces(which in-turn will transitively reference the otherMicrosoft.CodeAnalysis.*packages you'll need)..csfile withSyntaxTree tree = CSharpSyntaxTree.ParseText( await File.ReadAllTextAsync( "FizzBuzz.cs" ) ); CompilationUnitSyntax root = tree.GetCompilationUnitRoot();root.DescendantNodesforSyntaxsubtypes you're interested in, such asifstatements or specific keywords and tokens.[DiagnosticAnalyzerAttribute].editorconfig. If I ever find out I'll edit this answer.