diff --git a/src/Splat.DependencyInjection.SourceGenerator/MetadataDependencyChecker.cs b/src/Splat.DependencyInjection.SourceGenerator/MetadataDependencyChecker.cs index bbd3010..af067cc 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/MetadataDependencyChecker.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/MetadataDependencyChecker.cs @@ -6,6 +6,7 @@ using System.Linq; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using ReactiveMarbles.RoslynHelpers; @@ -43,10 +44,12 @@ public static List CheckMetadata(GeneratorExecutionContext conte { if (childConstructor.TypeName == metadataMethod.InterfaceTypeName) { + var location = childConstructor.Parameter.GetLocation(metadataMethod.MethodInvocation); + context.ReportDiagnostic( Diagnostic.Create( DiagnosticWarnings.ConstructorsMustNotHaveCircularDependency, - childConstructor.Parameter.Locations.FirstOrDefault(x => x is not null) ?? metadataMethod.MethodInvocation.GetLocation())); + location)); isError = true; } } @@ -65,10 +68,12 @@ public static List CheckMetadata(GeneratorExecutionContext conte if (metadataDependencies.TryGetValue(lazyType.ToDisplayString(RoslynCommonHelpers.TypeFormat), out dependencyMethod) && !dependencyMethod.IsLazy) { + var location = constructorDependency.Parameter.GetLocation(metadataMethod.MethodInvocation); + context.ReportDiagnostic( Diagnostic.Create( DiagnosticWarnings.LazyParameterNotRegisteredLazy, - constructorDependency.Parameter.Locations.FirstOrDefault(x => x is not null) ?? metadataMethod.MethodInvocation.GetLocation(), + location, metadataMethod.ConcreteTypeName, constructorDependency.Parameter.Name)); isError = true; @@ -84,5 +89,17 @@ public static List CheckMetadata(GeneratorExecutionContext conte return methods; } + + private static Location GetLocation(this ISymbol symbol, InvocationExpressionSyntax backupInvocation) + { + var location = symbol.Locations.FirstOrDefault(); + + if (location?.Kind != LocationKind.SourceFile) + { + location = backupInvocation.GetLocation(); + } + + return location; + } } } diff --git a/src/Splat.DependencyInjection.SourceGenerator/Splat.DependencyInjection.SourceGenerator.csproj b/src/Splat.DependencyInjection.SourceGenerator/Splat.DependencyInjection.SourceGenerator.csproj index 77fe4cd..69de455 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/Splat.DependencyInjection.SourceGenerator.csproj +++ b/src/Splat.DependencyInjection.SourceGenerator/Splat.DependencyInjection.SourceGenerator.csproj @@ -1,24 +1,25 @@ - + netstandard2.0 true - false - - true Produces DI registration for both property and constructor injection using the Splat locators. - $(TargetsForTfmSpecificContentInPackage);PackBuildOutputs $(NoWarn);AD0001 + analyzers + $(GetTargetPathDependsOn);GetDependencyTargetPaths - + - + + + + - +