< Summary

Information
Class: Hertzole.ScriptableValues.Generator.NoMarkerAttributeAnalyzer
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Analyzers/NoMarkerAttributeAnalyzer.cs
Tag: 52_28197466029
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 84
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
Initialize(...)100%110%
Action(...)0%14140%
IsGenerateCallbackAttribute(...)0%660%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Analyzers/NoMarkerAttributeAnalyzer.cs

#LineLine coverage
 1using System;
 2using System.Collections.Immutable;
 3using System.Linq;
 4using Microsoft.CodeAnalysis;
 5using Microsoft.CodeAnalysis.CSharp;
 6using Microsoft.CodeAnalysis.CSharp.Syntax;
 7using Microsoft.CodeAnalysis.Diagnostics;
 8
 9namespace Hertzole.ScriptableValues.Generator;
 10
 11[DiagnosticAnalyzer(LanguageNames.CSharp)]
 12public sealed class NoMarkerAttributeAnalyzer : DiagnosticAnalyzer
 13{
 014    private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = ImmutableArray.Create(Diagnostic
 15
 016    private static readonly SyntaxKind[] supportedSyntaxKinds = [SyntaxKind.Attribute];
 17
 18    /// <inheritdoc />
 19    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
 20    {
 021        get { return supportedDiagnostics; }
 22    }
 23
 24    /// <inheritdoc />
 25    public override void Initialize(AnalysisContext context)
 26    {
 027        context.EnableConcurrentExecution();
 028        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
 29
 030        context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds);
 031    }
 32
 33    private static void Action(SyntaxNodeAnalysisContext context)
 34    {
 035        if (context.Node is not AttributeSyntax attribute)
 36        {
 037            return;
 38        }
 39
 40        // Get symbol
 041        INamedTypeSymbol? attributeSymbol = attribute.GetAttributeSymbol(context.SemanticModel, context.CancellationToke
 042        if (attributeSymbol == null)
 43        {
 044            return;
 45        }
 46
 047        if (!IsGenerateCallbackAttribute(attributeSymbol))
 48        {
 049            return;
 50        }
 51
 52        // Get the type declaration syntax in parent
 053        TypeDeclarationSyntax? typeDeclaration = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>();
 054        if (typeDeclaration == null)
 55        {
 056            return;
 57        }
 58
 59        // Get the symbol for the type
 060        if (context.SemanticModel.GetDeclaredSymbol(typeDeclaration) is not INamedTypeSymbol parentTypeSymbol)
 61        {
 062            return;
 63        }
 64
 65        // Check if the class has the marker attribute
 066        if (parentTypeSymbol.GetAttributes().Any(static a =>
 067                a.AttributeClass != null && a.AttributeClass.StringEquals("global::Hertzole.ScriptableValues.GenerateScr
 68        {
 069            return;
 70        }
 71
 072        context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.NoMarkerAttribute, attribute.GetLocation()));
 073    }
 74
 75    private static bool IsGenerateCallbackAttribute(ITypeSymbol arg)
 76    {
 077        string name = arg.ToDisplayString(NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat);
 78
 079        return string.Equals(name, "global::Hertzole.ScriptableValues.GenerateValueCallbackAttribute", StringComparison.
 080               string.Equals(name, "global::Hertzole.ScriptableValues.GenerateEventCallbackAttribute", StringComparison.
 081               string.Equals(name, "global::Hertzole.ScriptableValues.GeneratePoolCallbackAttribute", StringComparison.O
 082               string.Equals(name, "global::Hertzole.ScriptableValues.GenerateCollectionCallbackAttribute", StringCompar
 83    }
 84}