Skip to content

Commit 0d6cac2

Browse files
committed
refactor: eliminate duplicate GetAllDirectlyAccessibleMethods query in SourceValueBuilder
1 parent df92d7a commit 0d6cac2

1 file changed

Lines changed: 8 additions & 29 deletions

File tree

src/Riok.Mapperly/Descriptors/MappingBodyBuilders/SourceValueBuilder.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Microsoft.CodeAnalysis.CSharp;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
55
using Riok.Mapperly.Configuration;
6-
using Riok.Mapperly.Configuration.MethodReferences;
76
using Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;
87
using Riok.Mapperly.Descriptors.Mappings;
98
using Riok.Mapperly.Descriptors.Mappings.MemberMappings;
@@ -160,20 +159,25 @@ private static bool TryBuildMethodProvidedSourceValue(
160159
var methodReferenceConfiguration = memberMappingInfo.ValueConfiguration!.Use!;
161160
var targetSymbol = methodReferenceConfiguration.GetTargetType(ctx.BuilderContext);
162161
var scope = ctx.BuilderContext.ParameterScope;
163-
var namedMethodCandidates = targetSymbol is null
162+
var allNamedMethods = targetSymbol is null
164163
? []
165164
: ctx
166165
.BuilderContext.SymbolAccessor.GetAllDirectlyAccessibleMethods(targetSymbol)
167166
.Where(m =>
168167
m is { IsAsync: false, ReturnsVoid: false, IsGenericMethod: false }
169-
&& scope.CanMatchParameters(m)
170168
&& ctx.BuilderContext.AttributeAccessor.IsMappingNameEqualTo(m, methodReferenceConfiguration.Name)
171169
)
172170
.ToList();
173171

172+
var namedMethodCandidates = allNamedMethods.Where(m => scope.CanMatchParameters(m)).ToList();
173+
174174
if (namedMethodCandidates.Count == 0)
175175
{
176-
ReportMethodNotFoundDiagnostic(ctx.BuilderContext, targetSymbol, methodReferenceConfiguration);
176+
var descriptor =
177+
allNamedMethods.Count > 0
178+
? DiagnosticDescriptors.MapValueMethodParametersUnsatisfied
179+
: DiagnosticDescriptors.MapValueReferencedMethodNotFound;
180+
ctx.BuilderContext.ReportDiagnostic(descriptor, methodReferenceConfiguration.FullName);
177181
sourceValue = null;
178182
return false;
179183
}
@@ -215,29 +219,4 @@ private static bool TryBuildMethodProvidedSourceValue(
215219
);
216220
return true;
217221
}
218-
219-
private static void ReportMethodNotFoundDiagnostic(
220-
MappingBuilderContext builderCtx,
221-
ITypeSymbol? targetSymbol,
222-
IMethodReferenceConfiguration methodRef
223-
)
224-
{
225-
// Check if a method by name exists but with unsatisfiable parameters
226-
if (
227-
targetSymbol is not null
228-
&& builderCtx
229-
.SymbolAccessor.GetAllDirectlyAccessibleMethods(targetSymbol)
230-
.Any(m =>
231-
m is { IsAsync: false, ReturnsVoid: false, IsGenericMethod: false, Parameters.Length: > 0 }
232-
&& builderCtx.AttributeAccessor.IsMappingNameEqualTo(m, methodRef.Name)
233-
)
234-
)
235-
{
236-
builderCtx.ReportDiagnostic(DiagnosticDescriptors.MapValueMethodParametersUnsatisfied, methodRef.FullName);
237-
}
238-
else
239-
{
240-
builderCtx.ReportDiagnostic(DiagnosticDescriptors.MapValueReferencedMethodNotFound, methodRef.FullName);
241-
}
242-
}
243222
}

0 commit comments

Comments
 (0)