< Summary

Information
Class: Hertzole.ScriptableValues.Generator.TypeNotSupportedAnalyzer
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Analyzers/TypeNotSupportedAnalyzer.cs
Tag: 52_28197466029
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 78
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%16160%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Analyzers/TypeNotSupportedAnalyzer.cs

#LineLine coverage
 1using System.Collections.Immutable;
 2using Microsoft.CodeAnalysis;
 3using Microsoft.CodeAnalysis.CSharp;
 4using Microsoft.CodeAnalysis.CSharp.Syntax;
 5using Microsoft.CodeAnalysis.Diagnostics;
 6
 7namespace Hertzole.ScriptableValues.Generator;
 8
 9[DiagnosticAnalyzer(LanguageNames.CSharp)]
 10public sealed class TypeNotSupportedAnalyzer : DiagnosticAnalyzer
 11{
 012    private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = ImmutableArray.Create(Diagnostic
 013    private static readonly ImmutableArray<SyntaxKind> supportedSyntaxKinds = ImmutableArray.Create(SyntaxKind.Attribute
 14
 15    /// <inheritdoc />
 16    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
 17    {
 018        get { return supportedDiagnostics; }
 19    }
 20
 21    /// <inheritdoc />
 22    public override void Initialize(AnalysisContext context)
 23    {
 024        context.EnableConcurrentExecution();
 025        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
 26
 027        context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds);
 028    }
 29
 30    private static void Action(SyntaxNodeAnalysisContext context)
 31    {
 032        if (context.Node is not AttributeSyntax attribute)
 33        {
 034            return;
 35        }
 36
 37        // Get symbol
 038        INamedTypeSymbol? attributeSymbol = attribute.GetAttributeSymbol(context.SemanticModel, context.CancellationToke
 039        if (attributeSymbol == null || !attributeSymbol.StringEquals(Types.GLOBAL_MARKER_ATTRIBUTE))
 40        {
 041            return;
 42        }
 43
 044        TypeDeclarationSyntax? containingType = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>();
 045        if (containingType == null)
 46        {
 047            return;
 48        }
 49
 050        if (context.SemanticModel.GetDeclaredSymbol(containingType) is not ITypeSymbol parentTypeSymbol)
 51        {
 052            return;
 53        }
 54
 055        if (ScriptableValueHelper.DoesTypeSupportCallbacks(in parentTypeSymbol, out NotSupportedReason notSupportedReaso
 56        {
 57            // This type is supported, stop here.
 058            return;
 59        }
 60
 61        string type;
 62
 63        switch (notSupportedReason)
 64        {
 65            case NotSupportedReason.ReadOnlyStruct:
 066                type = "'readonly struct'";
 067                break;
 68            case NotSupportedReason.StaticClass:
 069                type = "'static class'";
 070                break;
 71            default:
 072                type = "ERROR";
 73                break;
 74        }
 75
 076        context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.TypeNotSupported, attribute.GetLocation(), type));
 077    }
 78}