| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 8 | | |
| | | 9 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 10 | | public sealed class TypeNotSupportedForCallbackAttributeAnalyzer : DiagnosticAnalyzer |
| | | 11 | | { |
| | 0 | 12 | | private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = |
| | 0 | 13 | | ImmutableArray.Create(DiagnosticRules.TypeNotSupportedForCallbackAttribute); |
| | | 14 | | |
| | 0 | 15 | | private static readonly SyntaxKind[] supportedSyntaxKinds = [SyntaxKind.Attribute]; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| | | 19 | | { |
| | 0 | 20 | | get { return supportedDiagnostics; } |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 0 | 26 | | context.EnableConcurrentExecution(); |
| | 0 | 27 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | | 28 | | |
| | 0 | 29 | | context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private static void Action(SyntaxNodeAnalysisContext context) |
| | | 33 | | { |
| | 0 | 34 | | if (context.Node is not AttributeSyntax attributeSyntax) |
| | | 35 | | { |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | context.CancellationToken.ThrowIfCancellationRequested(); |
| | | 40 | | |
| | 0 | 41 | | INamedTypeSymbol? attributeSymbol = attributeSyntax.GetAttributeSymbol(context.SemanticModel, context.Cancellati |
| | 0 | 42 | | if (attributeSymbol == null) |
| | | 43 | | { |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | if (!attributeSymbol.TryGetCallbackAttribute(out CallbackType callbackType)) |
| | | 48 | | { |
| | | 49 | | // This is not a callback attribute, so we don't need to do anything. |
| | 0 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Get member declaration |
| | 0 | 54 | | MemberDeclarationSyntax? memberDeclaration = attributeSyntax.FirstAncestorOrSelf<MemberDeclarationSyntax>(); |
| | 0 | 55 | | if (memberDeclaration == null) |
| | | 56 | | { |
| | 0 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // Get wrapper |
| | 0 | 61 | | if (!memberDeclaration.TryGetFieldOrPropertyWrapper(context.SemanticModel, out ImmutableArray<FieldOrPropertyWra |
| | 0 | 62 | | wrappers.IsDefaultOrEmpty) |
| | | 63 | | { |
| | 0 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | TypeDeclarationSyntax? containingTypeDeclaration = memberDeclaration.FirstAncestorOrSelf<TypeDeclarationSyntax>( |
| | 0 | 68 | | if (containingTypeDeclaration == null) |
| | | 69 | | { |
| | 0 | 70 | | return; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Make sure the containing type has the marker attribute |
| | 0 | 74 | | if (context.SemanticModel.GetDeclaredSymbol(containingTypeDeclaration) is not INamedTypeSymbol containingTypeSym |
| | 0 | 75 | | !containingTypeSymbol.HasAttribute(Types.GLOBAL_MARKER_ATTRIBUTE)) |
| | | 76 | | { |
| | 0 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | FieldOrPropertyWrapper wrapper = wrappers.ItemRef(0); |
| | 0 | 81 | | if (!ScriptableValueHelper.TryGetScriptableType(context.SemanticModel, (INamedTypeSymbol) wrapper.Type, out Scri |
| | | 82 | | { |
| | 0 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // The correct callback attribute was used. |
| | 0 | 87 | | if (ScriptableValueHelper.IsSupportedType(in callbackType, in wrapperType)) |
| | | 88 | | { |
| | 0 | 89 | | return; |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | string correctAttributeName = GetCorrectAttributeName(wrapperType); |
| | | 93 | | |
| | 0 | 94 | | ImmutableDictionary<string, string?>.Builder propertiesBuilder = ImmutableDictionary.CreateBuilder<string, strin |
| | 0 | 95 | | propertiesBuilder.Add("correctAttributeName", correctAttributeName); |
| | | 96 | | |
| | 0 | 97 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.TypeNotSupportedForCallbackAttribute, attributeSyntax |
| | 0 | 98 | | propertiesBuilder.ToImmutable(), attributeSymbol.Name, correctAttributeName)); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | private static string GetCorrectAttributeName(in ScriptableType scriptableType) |
| | | 102 | | { |
| | 0 | 103 | | switch (scriptableType) |
| | | 104 | | { |
| | | 105 | | case ScriptableType.Event: |
| | | 106 | | case ScriptableType.GenericEvent: |
| | 0 | 107 | | return "GenerateEventCallbackAttribute"; |
| | | 108 | | case ScriptableType.Value: |
| | 0 | 109 | | return "GenerateValueCallbackAttribute"; |
| | | 110 | | case ScriptableType.Pool: |
| | 0 | 111 | | return "GeneratePoolCallbackAttribute"; |
| | | 112 | | case ScriptableType.List: |
| | | 113 | | case ScriptableType.Dictionary: |
| | 0 | 114 | | return "GenerateCollectionCallbackAttribute"; |
| | | 115 | | default: |
| | 0 | 116 | | return "UNKNOWN"; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |