| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 6 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Analyzers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Analyzer that detects lifetime mismatches in service registrations. |
| | | 12 | | /// A lifetime mismatch occurs when a longer-lived service depends on a shorter-lived service. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Examples of mismatches: |
| | | 16 | | /// - Singleton depends on Scoped → captive dependency |
| | | 17 | | /// - Singleton depends on Transient → captive dependency |
| | | 18 | | /// - Scoped depends on Transient → captive dependency |
| | | 19 | | /// </remarks> |
| | | 20 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 21 | | public sealed class LifetimeMismatchAnalyzer : DiagnosticAnalyzer |
| | | 22 | | { |
| | | 23 | | // Lifetime ranking: higher number = longer lifetime |
| | | 24 | | private enum LifetimeRank |
| | | 25 | | { |
| | | 26 | | Unknown = -1, |
| | | 27 | | Transient = 0, |
| | | 28 | | Scoped = 1, |
| | | 29 | | Singleton = 2 |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 360 | 33 | | ImmutableArray.Create(DiagnosticDescriptors.LifetimeMismatch); |
| | | 34 | | |
| | | 35 | | public override void Initialize(AnalysisContext context) |
| | | 36 | | { |
| | 28 | 37 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 28 | 38 | | context.EnableConcurrentExecution(); |
| | | 39 | | |
| | 28 | 40 | | context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration); |
| | 28 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) |
| | | 44 | | { |
| | 122 | 45 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | | 46 | | |
| | | 47 | | // Skip abstract classes |
| | 122 | 48 | | if (classDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword)) |
| | | 49 | | { |
| | 1 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 121 | 53 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 121 | 54 | | if (classSymbol == null) |
| | | 55 | | { |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Get the lifetime of this class from attributes, defaulting to Singleton for injectable types |
| | 121 | 60 | | var consumerLifetime = GetLifetimeFromType(classSymbol); |
| | 121 | 61 | | if (consumerLifetime == LifetimeRank.Unknown) |
| | | 62 | | { |
| | 0 | 63 | | return; // Not an injectable type |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Find constructors and analyze their parameters |
| | 121 | 67 | | var constructors = classDeclaration.Members |
| | 121 | 68 | | .OfType<ConstructorDeclarationSyntax>() |
| | 14 | 69 | | .Where(c => !c.Modifiers.Any(SyntaxKind.StaticKeyword)) |
| | 121 | 70 | | .ToList(); |
| | | 71 | | |
| | | 72 | | // If no explicit constructors, check primary constructor parameters |
| | 121 | 73 | | if (classDeclaration.ParameterList != null) |
| | | 74 | | { |
| | 2 | 75 | | AnalyzeParameters( |
| | 2 | 76 | | context, |
| | 2 | 77 | | classDeclaration.ParameterList.Parameters, |
| | 2 | 78 | | classSymbol, |
| | 2 | 79 | | consumerLifetime, |
| | 2 | 80 | | classDeclaration.Identifier.GetLocation()); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Analyze explicit constructor parameters |
| | 270 | 84 | | foreach (var constructor in constructors) |
| | | 85 | | { |
| | 14 | 86 | | AnalyzeParameters( |
| | 14 | 87 | | context, |
| | 14 | 88 | | constructor.ParameterList.Parameters, |
| | 14 | 89 | | classSymbol, |
| | 14 | 90 | | consumerLifetime, |
| | 14 | 91 | | constructor.Identifier.GetLocation()); |
| | | 92 | | } |
| | 121 | 93 | | } |
| | | 94 | | |
| | | 95 | | private static void AnalyzeParameters( |
| | | 96 | | SyntaxNodeAnalysisContext context, |
| | | 97 | | SeparatedSyntaxList<ParameterSyntax> parameters, |
| | | 98 | | INamedTypeSymbol consumerSymbol, |
| | | 99 | | LifetimeRank consumerLifetime, |
| | | 100 | | Location reportLocation) |
| | | 101 | | { |
| | 70 | 102 | | foreach (var parameter in parameters) |
| | | 103 | | { |
| | 19 | 104 | | if (parameter.Type == null) |
| | | 105 | | { |
| | | 106 | | continue; |
| | | 107 | | } |
| | | 108 | | |
| | 19 | 109 | | var typeInfo = context.SemanticModel.GetTypeInfo(parameter.Type); |
| | 19 | 110 | | var parameterType = typeInfo.Type as INamedTypeSymbol; |
| | 19 | 111 | | if (parameterType == null) |
| | | 112 | | { |
| | | 113 | | continue; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // Get the lifetime of the dependency type |
| | 19 | 117 | | var dependencyLifetime = GetLifetimeFromType(parameterType); |
| | 19 | 118 | | if (dependencyLifetime == LifetimeRank.Unknown) |
| | | 119 | | { |
| | | 120 | | continue; // Unknown lifetime (interface or non-injectable type), skip |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Check for mismatch: consumer lifetime > dependency lifetime |
| | 19 | 124 | | if ((int)consumerLifetime > (int)dependencyLifetime) |
| | | 125 | | { |
| | 14 | 126 | | var diagnostic = Diagnostic.Create( |
| | 14 | 127 | | DiagnosticDescriptors.LifetimeMismatch, |
| | 14 | 128 | | parameter.GetLocation(), |
| | 14 | 129 | | consumerSymbol.Name, |
| | 14 | 130 | | GetLifetimeName(consumerLifetime), |
| | 14 | 131 | | parameterType.Name, |
| | 14 | 132 | | GetLifetimeName(dependencyLifetime)); |
| | | 133 | | |
| | 14 | 134 | | context.ReportDiagnostic(diagnostic); |
| | | 135 | | } |
| | | 136 | | } |
| | 16 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Gets the lifetime for a type, defaulting to Singleton for injectable concrete classes. |
| | | 141 | | /// </summary> |
| | | 142 | | private static LifetimeRank GetLifetimeFromType(INamedTypeSymbol typeSymbol) |
| | | 143 | | { |
| | | 144 | | // Check for explicit lifetime attributes first |
| | 140 | 145 | | var explicitLifetime = GetLifetimeFromAttributes(typeSymbol); |
| | 140 | 146 | | if (explicitLifetime != LifetimeRank.Unknown) |
| | | 147 | | { |
| | 51 | 148 | | return explicitLifetime; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | // For concrete classes, default to Singleton (matching Needlr's default behavior) |
| | 89 | 152 | | if (typeSymbol.TypeKind == TypeKind.Class && !typeSymbol.IsAbstract) |
| | | 153 | | { |
| | 89 | 154 | | return LifetimeRank.Singleton; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // For interfaces and abstract types, we cannot determine lifetime statically |
| | 0 | 158 | | return LifetimeRank.Unknown; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static LifetimeRank GetLifetimeFromAttributes(INamedTypeSymbol typeSymbol) |
| | | 162 | | { |
| | 501 | 163 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 164 | | { |
| | 136 | 165 | | var attributeName = attribute.AttributeClass?.Name; |
| | | 166 | | |
| | | 167 | | // Check for specific lifetime attributes (the only real lifetime attributes in Needlr) |
| | 136 | 168 | | if (attributeName is "SingletonAttribute" or "Singleton") |
| | | 169 | | { |
| | 16 | 170 | | return LifetimeRank.Singleton; |
| | | 171 | | } |
| | | 172 | | |
| | 120 | 173 | | if (attributeName is "ScopedAttribute" or "Scoped") |
| | | 174 | | { |
| | 22 | 175 | | return LifetimeRank.Scoped; |
| | | 176 | | } |
| | | 177 | | |
| | 98 | 178 | | if (attributeName is "TransientAttribute" or "Transient") |
| | | 179 | | { |
| | 13 | 180 | | return LifetimeRank.Transient; |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | |
| | 89 | 184 | | return LifetimeRank.Unknown; |
| | | 185 | | } |
| | | 186 | | |
| | 28 | 187 | | private static string GetLifetimeName(LifetimeRank lifetime) => lifetime switch |
| | 28 | 188 | | { |
| | 12 | 189 | | LifetimeRank.Singleton => "Singleton", |
| | 10 | 190 | | LifetimeRank.Scoped => "Scoped", |
| | 6 | 191 | | LifetimeRank.Transient => "Transient", |
| | 0 | 192 | | _ => "Unknown" |
| | 28 | 193 | | }; |
| | | 194 | | } |