|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | + |
| 8 | +namespace AutoMap; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Re-reports AM004 diagnostics with real property syntax locations so that |
| 12 | +/// IDE code-fix lightbulbs appear on the affected destination property. |
| 13 | +/// </summary> |
| 14 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 15 | +public sealed class AutoMapAnalyzer : DiagnosticAnalyzer |
| 16 | +{ |
| 17 | + private static readonly DiagnosticDescriptor AM004 = new( |
| 18 | + id: "AM004", |
| 19 | + title: "Property skipped due to type incompatibility", |
| 20 | + messageFormat: "Property '{0}' on '{1}' was skipped: source property '{2}' on '{3}' has an incompatible type with no registered mapping. Use [MapIgnore] to suppress, or add [Map] on the source type.", |
| 21 | + category: "AutoMap", |
| 22 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 23 | + isEnabledByDefault: true, |
| 24 | + helpLinkUri: "https://github.com/Swevo/AutoMap.Generator#am004"); |
| 25 | + |
| 26 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 27 | + ImmutableArray.Create(AM004); |
| 28 | + |
| 29 | + public override void Initialize(AnalysisContext context) |
| 30 | + { |
| 31 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 32 | + context.EnableConcurrentExecution(); |
| 33 | + context.RegisterSymbolAction(AnalyzeType, SymbolKind.NamedType); |
| 34 | + } |
| 35 | + |
| 36 | + private static void AnalyzeType(SymbolAnalysisContext ctx) |
| 37 | + { |
| 38 | + var typeSymbol = (INamedTypeSymbol)ctx.Symbol; |
| 39 | + |
| 40 | + // [MapFrom(typeof(Src))] — dest type decorated |
| 41 | + foreach (var attr in typeSymbol.GetAttributes()) |
| 42 | + { |
| 43 | + if (attr.AttributeClass?.ToDisplayString() != "AutoMap.MapFromAttribute") continue; |
| 44 | + if (attr.ConstructorArguments.Length == 0) continue; |
| 45 | + if (attr.ConstructorArguments[0].Value is not INamedTypeSymbol srcType) continue; |
| 46 | + CheckProperties(ctx, srcType, typeSymbol); |
| 47 | + } |
| 48 | + |
| 49 | + // [Map(typeof(Dest))] — source type decorated |
| 50 | + foreach (var attr in typeSymbol.GetAttributes()) |
| 51 | + { |
| 52 | + if (attr.AttributeClass?.ToDisplayString() != "AutoMap.MapAttribute") continue; |
| 53 | + if (attr.ConstructorArguments.Length == 0) continue; |
| 54 | + if (attr.ConstructorArguments[0].Value is not INamedTypeSymbol destType) continue; |
| 55 | + CheckProperties(ctx, typeSymbol, destType); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static void CheckProperties( |
| 60 | + SymbolAnalysisContext ctx, |
| 61 | + INamedTypeSymbol srcType, |
| 62 | + INamedTypeSymbol destType) |
| 63 | + { |
| 64 | + // Build source property lookup |
| 65 | + var srcProps = new Dictionary<string, IPropertySymbol>(System.StringComparer.OrdinalIgnoreCase); |
| 66 | + foreach (var p in GetAllPublicProperties(srcType)) |
| 67 | + srcProps[p.Name] = p; |
| 68 | + |
| 69 | + var csharp = ctx.Compilation as CSharpCompilation; |
| 70 | + |
| 71 | + foreach (var destProp in GetAllPublicProperties(destType)) |
| 72 | + { |
| 73 | + // Skip if opted out or has custom mapping |
| 74 | + if (HasAttr(destProp, "AutoMap.MapIgnoreAttribute")) continue; |
| 75 | + if (HasAttr(destProp, "AutoMap.MapWithAttribute")) continue; |
| 76 | + if (HasAttr(destProp, "AutoMap.MapFormatAttribute")) continue; |
| 77 | + if (HasAttr(destProp, "AutoMap.MapWhenAttribute")) continue; |
| 78 | + |
| 79 | + // Resolve source name (possibly via [MapProperty]) |
| 80 | + string lookupName = destProp.Name; |
| 81 | + foreach (var a in destProp.GetAttributes()) |
| 82 | + { |
| 83 | + if (a.AttributeClass?.ToDisplayString() == "AutoMap.MapPropertyAttribute" |
| 84 | + && a.ConstructorArguments.Length > 0) |
| 85 | + { |
| 86 | + lookupName = a.ConstructorArguments[0].Value as string ?? destProp.Name; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (!srcProps.TryGetValue(lookupName, out var srcProp)) continue; |
| 92 | + |
| 93 | + // Enum-to-enum: generator handles this with switch expressions — skip |
| 94 | + if (srcProp.Type.TypeKind == TypeKind.Enum && destProp.Type.TypeKind == TypeKind.Enum) continue; |
| 95 | + |
| 96 | + // Collection types: generator handles via registered mappings — skip |
| 97 | + if (IsCollection(srcProp.Type) || IsCollection(destProp.Type)) continue; |
| 98 | + |
| 99 | + // Check type compatibility |
| 100 | + bool compatible; |
| 101 | + if (csharp != null) |
| 102 | + { |
| 103 | + var conv = csharp.ClassifyConversion(srcProp.Type, destProp.Type); |
| 104 | + compatible = conv.IsIdentity || conv.IsImplicit; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + compatible = SymbolEqualityComparer.Default.Equals(srcProp.Type, destProp.Type); |
| 109 | + } |
| 110 | + if (compatible) continue; |
| 111 | + |
| 112 | + // Report on the property declaration syntax |
| 113 | + foreach (var synRef in destProp.DeclaringSyntaxReferences) |
| 114 | + { |
| 115 | + var syntax = synRef.GetSyntax(ctx.CancellationToken); |
| 116 | + ctx.ReportDiagnostic(Diagnostic.Create( |
| 117 | + AM004, syntax.GetLocation(), |
| 118 | + destProp.Name, destType.Name, |
| 119 | + srcProp.Name, srcType.Name)); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static IEnumerable<IPropertySymbol> GetAllPublicProperties(INamedTypeSymbol type) |
| 125 | + { |
| 126 | + var seen = new System.Collections.Generic.HashSet<string>(System.StringComparer.Ordinal); |
| 127 | + var current = (INamedTypeSymbol?)type; |
| 128 | + while (current != null && current.SpecialType != SpecialType.System_Object) |
| 129 | + { |
| 130 | + foreach (var m in current.GetMembers()) |
| 131 | + if (m is IPropertySymbol p && |
| 132 | + p.DeclaredAccessibility == Accessibility.Public && |
| 133 | + seen.Add(p.Name)) |
| 134 | + yield return p; |
| 135 | + current = current.BaseType; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static bool HasAttr(ISymbol symbol, string fqn) => |
| 140 | + symbol.GetAttributes().Any(a => a.AttributeClass?.ToDisplayString() == fqn); |
| 141 | + |
| 142 | + private static bool IsCollection(ITypeSymbol type) |
| 143 | + { |
| 144 | + if (type is IArrayTypeSymbol) return true; |
| 145 | + if (type is INamedTypeSymbol named && named.IsGenericType && named.TypeArguments.Length == 1) |
| 146 | + { |
| 147 | + var def = named.OriginalDefinition.ToDisplayString(); |
| 148 | + return def is "System.Collections.Generic.List<T>" |
| 149 | + or "System.Collections.Generic.IEnumerable<T>" |
| 150 | + or "System.Collections.Generic.ICollection<T>" |
| 151 | + or "System.Collections.Generic.IList<T>" |
| 152 | + or "System.Collections.Generic.IReadOnlyList<T>" |
| 153 | + or "System.Collections.Generic.IReadOnlyCollection<T>"; |
| 154 | + } |
| 155 | + return false; |
| 156 | + } |
| 157 | +} |
0 commit comments