| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Linq; |
| | | 7 | | |
| | | 8 | | using Microsoft.CodeAnalysis; |
| | | 9 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 11 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 12 | | |
| | | 13 | | namespace NexusLabs.Needlr.Generators; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Analyzer that warns when [Options] classes use DataAnnotation validation attributes |
| | | 17 | | /// that cannot be source-generated. |
| | | 18 | | /// |
| | | 19 | | /// NDLRGEN030: DataAnnotation attribute cannot be source-generated |
| | | 20 | | /// </summary> |
| | | 21 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 22 | | public sealed class UnsupportedDataAnnotationAnalyzer : DiagnosticAnalyzer |
| | | 23 | | { |
| | | 24 | | private const string OptionsAttributeName = "OptionsAttribute"; |
| | | 25 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 26 | | private const string DataAnnotationsNamespace = "System.ComponentModel.DataAnnotations"; |
| | | 27 | | |
| | 1 | 28 | | private static readonly HashSet<string> SupportedDataAnnotations = new() |
| | 1 | 29 | | { |
| | 1 | 30 | | "RequiredAttribute", |
| | 1 | 31 | | "RangeAttribute", |
| | 1 | 32 | | "StringLengthAttribute", |
| | 1 | 33 | | "MinLengthAttribute", |
| | 1 | 34 | | "MaxLengthAttribute", |
| | 1 | 35 | | "RegularExpressionAttribute", |
| | 1 | 36 | | "EmailAddressAttribute", |
| | 1 | 37 | | "PhoneAttribute", |
| | 1 | 38 | | "UrlAttribute" |
| | 1 | 39 | | }; |
| | | 40 | | |
| | | 41 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 9 | 42 | | ImmutableArray.Create(DiagnosticDescriptors.UnsupportedDataAnnotation); |
| | | 43 | | |
| | | 44 | | public override void Initialize(AnalysisContext context) |
| | | 45 | | { |
| | 9 | 46 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 9 | 47 | | context.EnableConcurrentExecution(); |
| | | 48 | | |
| | 9 | 49 | | context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration); |
| | 9 | 50 | | context.RegisterSyntaxNodeAction(AnalyzeRecordDeclaration, SyntaxKind.RecordDeclaration); |
| | 9 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) |
| | | 54 | | { |
| | 9 | 55 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 9 | 56 | | AnalyzeTypeDeclaration(context, classDeclaration, classDeclaration.AttributeLists, classDeclaration.Members); |
| | 9 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static void AnalyzeRecordDeclaration(SyntaxNodeAnalysisContext context) |
| | | 60 | | { |
| | 1 | 61 | | var recordDeclaration = (RecordDeclarationSyntax)context.Node; |
| | 1 | 62 | | AnalyzeTypeDeclaration(context, recordDeclaration, recordDeclaration.AttributeLists, recordDeclaration.Members); |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static void AnalyzeTypeDeclaration( |
| | | 66 | | SyntaxNodeAnalysisContext context, |
| | | 67 | | TypeDeclarationSyntax typeDeclaration, |
| | | 68 | | SyntaxList<AttributeListSyntax> attributeLists, |
| | | 69 | | SyntaxList<MemberDeclarationSyntax> members) |
| | | 70 | | { |
| | | 71 | | // Check if this type has [Options] attribute |
| | 10 | 72 | | if (!HasOptionsAttribute(context, attributeLists)) |
| | 2 | 73 | | return; |
| | | 74 | | |
| | 8 | 75 | | var typeSymbol = context.SemanticModel.GetDeclaredSymbol(typeDeclaration); |
| | 8 | 76 | | if (typeSymbol == null) |
| | 0 | 77 | | return; |
| | | 78 | | |
| | | 79 | | // Check all properties for unsupported DataAnnotations |
| | 52 | 80 | | foreach (var member in members) |
| | | 81 | | { |
| | 18 | 82 | | if (member is PropertyDeclarationSyntax property) |
| | | 83 | | { |
| | 18 | 84 | | AnalyzeProperty(context, typeSymbol, property); |
| | | 85 | | } |
| | | 86 | | } |
| | 8 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static bool HasOptionsAttribute(SyntaxNodeAnalysisContext context, SyntaxList<AttributeListSyntax> attribute |
| | | 90 | | { |
| | 28 | 91 | | foreach (var attributeList in attributeLists) |
| | | 92 | | { |
| | 24 | 93 | | foreach (var attribute in attributeList.Attributes) |
| | | 94 | | { |
| | 8 | 95 | | var symbol = context.SemanticModel.GetSymbolInfo(attribute).Symbol?.ContainingType; |
| | 8 | 96 | | if (symbol != null && |
| | 8 | 97 | | symbol.Name == OptionsAttributeName && |
| | 8 | 98 | | symbol.ContainingNamespace.ToDisplayString() == GeneratorsNamespace) |
| | | 99 | | { |
| | 8 | 100 | | return true; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |
| | 2 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private static void AnalyzeProperty( |
| | | 108 | | SyntaxNodeAnalysisContext context, |
| | | 109 | | INamedTypeSymbol containingType, |
| | | 110 | | PropertyDeclarationSyntax property) |
| | | 111 | | { |
| | 18 | 112 | | var propertySymbol = context.SemanticModel.GetDeclaredSymbol(property); |
| | 18 | 113 | | if (propertySymbol == null) |
| | 0 | 114 | | return; |
| | | 115 | | |
| | 74 | 116 | | foreach (var attribute in propertySymbol.GetAttributes()) |
| | | 117 | | { |
| | 19 | 118 | | var attrClass = attribute.AttributeClass; |
| | 19 | 119 | | if (attrClass == null) |
| | | 120 | | continue; |
| | | 121 | | |
| | 19 | 122 | | var attrNamespace = attrClass.ContainingNamespace?.ToDisplayString() ?? ""; |
| | 19 | 123 | | var attrTypeName = attrClass.Name; |
| | | 124 | | |
| | | 125 | | // Only check DataAnnotations namespace |
| | 19 | 126 | | if (attrNamespace != DataAnnotationsNamespace) |
| | | 127 | | continue; |
| | | 128 | | |
| | | 129 | | // Check if this is a ValidationAttribute (or subclass) |
| | 18 | 130 | | if (!IsValidationAttribute(attrClass)) |
| | | 131 | | continue; |
| | | 132 | | |
| | | 133 | | // Check if this is a supported attribute |
| | 18 | 134 | | if (SupportedDataAnnotations.Contains(attrTypeName)) |
| | | 135 | | continue; |
| | | 136 | | |
| | | 137 | | // Unsupported DataAnnotation - report diagnostic |
| | 7 | 138 | | var location = attribute.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? property.GetLocation(); |
| | 7 | 139 | | var diagnostic = Diagnostic.Create( |
| | 7 | 140 | | DiagnosticDescriptors.UnsupportedDataAnnotation, |
| | 7 | 141 | | location, |
| | 7 | 142 | | attrTypeName, |
| | 7 | 143 | | containingType.Name, |
| | 7 | 144 | | propertySymbol.Name); |
| | | 145 | | |
| | 7 | 146 | | context.ReportDiagnostic(diagnostic); |
| | | 147 | | } |
| | 18 | 148 | | } |
| | | 149 | | |
| | | 150 | | private static bool IsValidationAttribute(INamedTypeSymbol attrClass) |
| | | 151 | | { |
| | 18 | 152 | | var current = attrClass; |
| | 45 | 153 | | while (current != null) |
| | | 154 | | { |
| | 45 | 155 | | var fullName = current.ToDisplayString(); |
| | 45 | 156 | | if (fullName == "System.ComponentModel.DataAnnotations.ValidationAttribute") |
| | 18 | 157 | | return true; |
| | 27 | 158 | | current = current.BaseType; |
| | | 159 | | } |
| | 0 | 160 | | return false; |
| | | 161 | | } |
| | | 162 | | } |