Skip to content

Commit db666ee

Browse files
committed
Refactor: Simplify InjectableData creation, adjust location handling in InjectableAnalyzer, and update COCDI001 severity
1 parent a5322d5 commit db666ee

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
; Shipped analyzer releases
2-
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
3-
41
## Release 0.81.1
52

63
### New Rules
74
Rule ID | Category | Severity | Notes
85
----------|------------|----------|----------------------------------------------------------------
9-
COCDI001 | Injectable | Warning | Service does not implement the correct Injectable service type
6+
COCDI001 | Injectable | Error | Service does not implement the correct Injectable service type
107

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Release 0.81.1
2+
3+
### New Rules
4+
Rule ID | Category | Severity | Notes
5+
----------|------------|----------|----------------------------------------------------------------
6+

src/CodeOfChaos.Extensions.DependencyInjection.Generators/InjectableAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) {
4949
if (serviceTypeSymbol is null) continue;
5050
if (DoesClassImplementOrInherit(classSymbol, serviceTypeSymbol)) continue;
5151

52+
Location location = classDeclaration.Identifier.GetLocation();
5253
var diagnostic = Diagnostic.Create(
5354
Rule001,
54-
context.Node.GetLocation(),
55-
// injectableData.LocationInfo?.ToLocation(),
55+
location,
5656
injectableData.ClassName,
5757
injectableData.ServiceTypeName
5858
);

src/CodeOfChaos.Extensions.DependencyInjection.Generators/InjectableData.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4-
using CodeOfChaos.GeneratorTools;
5-
using JetBrains.Annotations;
64
using Microsoft.CodeAnalysis;
75
using Microsoft.CodeAnalysis.CSharp.Syntax;
86
using Microsoft.CodeAnalysis.Diagnostics;
@@ -20,8 +18,7 @@ public record InjectableData(
2018
ServiceLifetime ServiceLifetime,
2119
string? ServiceTypeName,
2220
object? ServiceKey,
23-
bool ServiceKeyIsString,
24-
[UsedImplicitly] LocationInfo? LocationInfo
21+
bool ServiceKeyIsString
2522
) {
2623
[MemberNotNullWhen(true, nameof(ServiceKey))] public bool HasServiceKey => ServiceKey is not null;
2724

@@ -34,7 +31,7 @@ public static IEnumerable<InjectableData> FromInjectableAttribute(GeneratorAttri
3431

3532
foreach (AttributeData attributeData in context.Attributes) {
3633
int lifetime = attributeData.ConstructorArguments[0].Value as int? ?? -1;
37-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, ServiceLifetimeUtlities.ToLifetime(lifetime), firstArgumentIndex: 1);
34+
yield return ExtractServiceData(classSymbol, attributeData, ServiceLifetimeUtlities.ToLifetime(lifetime), firstArgumentIndex: 1);
3835
}
3936
}
4037

@@ -45,7 +42,7 @@ public static IEnumerable<InjectableData> FromInjectableAttribute(GeneratorAttri
4542
if (context.SemanticModel.GetDeclaredSymbol(classDeclaration) is not INamedTypeSymbol classSymbol) yield break;
4643

4744
foreach (AttributeData attributeData in context.Attributes) {
48-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, lifetime, firstArgumentIndex);
45+
yield return ExtractServiceData(classSymbol, attributeData, lifetime, firstArgumentIndex);
4946
}
5047
}
5148

@@ -62,44 +59,42 @@ public static IEnumerable<InjectableData> FromSyntaxAnalyzer(SyntaxNodeAnalysisC
6259
switch (fullMetadataName) {
6360
case SourceCodes.InjectableAttributeMetadataName: {
6461
int lifetime = attributeData.ConstructorArguments[0].Value as int? ?? -1;
65-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, ServiceLifetimeUtlities.ToLifetime(lifetime), 1);
62+
yield return ExtractServiceData(classSymbol, attributeData, ServiceLifetimeUtlities.ToLifetime(lifetime), 1);
6663
break;
6764
}
6865

6966
case SourceCodes.InjectableSingletonAttributeMetadataName: {
70-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, ServiceLifetime.Singleton, 1);
67+
yield return ExtractServiceData(classSymbol, attributeData, ServiceLifetime.Singleton, 1);
7168
break;
7269
}
7370

7471
case SourceCodes.InjectableScopedAttributeMetadataName: {
75-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, ServiceLifetime.Scoped, 1);
72+
yield return ExtractServiceData(classSymbol, attributeData, ServiceLifetime.Scoped, 1);
7673
break;
7774
}
7875

7976
case SourceCodes.InjectableTransientAttributeMetadataName: {
80-
yield return ExtractServiceData(classSymbol, classDeclaration, attributeData, ServiceLifetime.Transient, 1);
77+
yield return ExtractServiceData(classSymbol, attributeData, ServiceLifetime.Transient, 1);
8178
break;
8279
}
8380
}
8481
}
8582
}
8683

87-
private static InjectableData ExtractServiceData(INamedTypeSymbol classSymbol, ClassDeclarationSyntax classDeclaration, AttributeData attributeData, ServiceLifetime lifetime, int firstArgumentIndex) {
84+
private static InjectableData ExtractServiceData(INamedTypeSymbol classSymbol, AttributeData attributeData, ServiceLifetime lifetime, int firstArgumentIndex) {
8885
TypedConstant keyArgument = attributeData.ConstructorArguments.ElementAtOrDefault(firstArgumentIndex);
8986
ITypeSymbol? keyType = keyArgument.Type;
9087
object? keyObject = keyArgument.Value;
9188

9289
string? serviceTypeName = attributeData.AttributeClass?.TypeArguments.First().ToString();
9390
string className = classSymbol.ToDisplayString();
94-
LocationInfo? locationInfo = LocationInfo.From(classDeclaration.GetLocation());
9591

9692
return new InjectableData(
9793
className,
9894
lifetime,
9995
serviceTypeName,
10096
keyObject,
101-
keyType is { SpecialType: SpecialType.System_String },
102-
locationInfo
97+
keyType is { SpecialType: SpecialType.System_String }
10398
);
10499
}
105100
}

0 commit comments

Comments
 (0)