Skip to content

Commit 9c2bcbf

Browse files
Copilotdex3r
andauthored
Refactor GeneratesMethod target resolution to use named constructor parameter mapping (#25)
* Initial plan * Refactor GeneratesMethod argument lookup to named parameter matching Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> * Apply explicit parameter-index lookup style cleanup Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent 8f6d90e commit 9c2bcbf

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

MattSourceGenHelpers.Generators/GeneratesMethodGenerationTargetCollector.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using MattSourceGenHelpers.Abstractions;
34
using System.Collections.Immutable;
45
using static MattSourceGenHelpers.Generators.Consts;
56

@@ -54,7 +55,23 @@ internal static List<GeneratesMethodGenerationTarget> Collect(
5455
continue;
5556
}
5657

57-
string? targetMethodName = attribute.ConstructorArguments[0].Value?.ToString();
58+
ImmutableArray<IParameterSymbol> constructorParameters = attribute.AttributeConstructor?.Parameters ?? [];
59+
int targetMethodNameArgumentIndex = -1;
60+
for (int parameterIndex = 0; parameterIndex < constructorParameters.Length; parameterIndex++)
61+
{
62+
IParameterSymbol constructorParameter = constructorParameters[parameterIndex];
63+
if (constructorParameter.Name.Equals(nameof(GeneratesMethod.SameClassMethodName), StringComparison.OrdinalIgnoreCase))
64+
{
65+
targetMethodNameArgumentIndex = parameterIndex;
66+
break;
67+
}
68+
}
69+
if (targetMethodNameArgumentIndex < 0 || targetMethodNameArgumentIndex >= attribute.ConstructorArguments.Length)
70+
{
71+
continue;
72+
}
73+
74+
string? targetMethodName = attribute.ConstructorArguments[targetMethodNameArgumentIndex].Value?.ToString();
5875
if (string.IsNullOrEmpty(targetMethodName))
5976
{
6077
continue;

MattSourceGenHelpers.Tests/UnitTest1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public partial class TestColorsClass
4545
{
4646
public partial string GetAllColorsString();
4747

48-
[GeneratesMethod(nameof(GetAllColorsString))]
48+
[GeneratesMethod(sameClassMethodName: nameof(GetAllColorsString))]
4949
private static string GetAllColorsString_Generator() =>
5050
string.Join(", ", Enum.GetNames<TestColorsEnum>());
5151
}

0 commit comments

Comments
 (0)