| | | 1 | | using System; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 10 | | |
| | | 11 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 12 | | public sealed class NoMarkerAttributeAnalyzer : DiagnosticAnalyzer |
| | | 13 | | { |
| | 0 | 14 | | private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = ImmutableArray.Create(Diagnostic |
| | | 15 | | |
| | 0 | 16 | | private static readonly SyntaxKind[] supportedSyntaxKinds = [SyntaxKind.Attribute]; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| | | 20 | | { |
| | 0 | 21 | | get { return supportedDiagnostics; } |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override void Initialize(AnalysisContext context) |
| | | 26 | | { |
| | 0 | 27 | | context.EnableConcurrentExecution(); |
| | 0 | 28 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | | 29 | | |
| | 0 | 30 | | context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static void Action(SyntaxNodeAnalysisContext context) |
| | | 34 | | { |
| | 0 | 35 | | if (context.Node is not AttributeSyntax attribute) |
| | | 36 | | { |
| | 0 | 37 | | return; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // Get symbol |
| | 0 | 41 | | INamedTypeSymbol? attributeSymbol = attribute.GetAttributeSymbol(context.SemanticModel, context.CancellationToke |
| | 0 | 42 | | if (attributeSymbol == null) |
| | | 43 | | { |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | if (!IsGenerateCallbackAttribute(attributeSymbol)) |
| | | 48 | | { |
| | 0 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // Get the type declaration syntax in parent |
| | 0 | 53 | | TypeDeclarationSyntax? typeDeclaration = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>(); |
| | 0 | 54 | | if (typeDeclaration == null) |
| | | 55 | | { |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Get the symbol for the type |
| | 0 | 60 | | if (context.SemanticModel.GetDeclaredSymbol(typeDeclaration) is not INamedTypeSymbol parentTypeSymbol) |
| | | 61 | | { |
| | 0 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Check if the class has the marker attribute |
| | 0 | 66 | | if (parentTypeSymbol.GetAttributes().Any(static a => |
| | 0 | 67 | | a.AttributeClass != null && a.AttributeClass.StringEquals("global::Hertzole.ScriptableValues.GenerateScr |
| | | 68 | | { |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.NoMarkerAttribute, attribute.GetLocation())); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | private static bool IsGenerateCallbackAttribute(ITypeSymbol arg) |
| | | 76 | | { |
| | 0 | 77 | | string name = arg.ToDisplayString(NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 78 | | |
| | 0 | 79 | | return string.Equals(name, "global::Hertzole.ScriptableValues.GenerateValueCallbackAttribute", StringComparison. |
| | 0 | 80 | | string.Equals(name, "global::Hertzole.ScriptableValues.GenerateEventCallbackAttribute", StringComparison. |
| | 0 | 81 | | string.Equals(name, "global::Hertzole.ScriptableValues.GeneratePoolCallbackAttribute", StringComparison.O |
| | 0 | 82 | | string.Equals(name, "global::Hertzole.ScriptableValues.GenerateCollectionCallbackAttribute", StringCompar |
| | | 83 | | } |
| | | 84 | | } |