Skip to content

Refactor GeneratesMethod target resolution to use named constructor parameter mapping#25

Merged
dex3r merged 3 commits into
mainfrom
copilot/refactor-generators-implicit-parameter
Feb 28, 2026
Merged

Refactor GeneratesMethod target resolution to use named constructor parameter mapping#25
dex3r merged 3 commits into
mainfrom
copilot/refactor-generators-implicit-parameter

Conversation

Copilot AI commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

[GeneratesMethod] handling in the generator relied on implicit positional access to constructor arguments. This change makes target method resolution explicit by mapping constructor arguments via the SameClassMethodName parameter name (nameof(...)) instead of assuming index 0.

  • Generator: remove implicit constructor argument indexing

    • Updated GeneratesMethodGenerationTargetCollector to resolve the constructor argument index by matching constructor parameter name against nameof(GeneratesMethod.SameClassMethodName).
    • Preserves existing behavior while removing dependence on positional argument ordering.
  • Usage coverage: named argument form

    • Updated a test fixture usage to pass the attribute value with an explicit named argument (sameClassMethodName:), validating compatibility with the refactored resolution path.
[GeneratesMethod(sameClassMethodName: nameof(GetAllColorsString))]
private static string GetAllColorsString_Generator() =>
    string.Join(", ", Enum.GetNames<TestColorsEnum>());
Original prompt

This section details on the original issue you should resolve

<issue_title>In generators, the GeneratesMethod parameter is used implicitly</issue_title>
<issue_description>Somehow, the constructor parameter of GeneratesMethod is used implicitly in Generators. Refactor Generators so it uses nameof(SameClassMethodName) instead.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 28, 2026 22:14
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor Generators to use nameof for GeneratesMethod Refactor GeneratesMethod target resolution to use named constructor parameter mapping Feb 28, 2026
@dex3r dex3r marked this pull request as ready for review February 28, 2026 22:19
Copilot AI review requested due to automatic review settings February 28, 2026 22:19
@dex3r dex3r merged commit 9c2bcbf into main Feb 28, 2026
3 checks passed
@dex3r dex3r deleted the copilot/refactor-generators-implicit-parameter branch February 28, 2026 22:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors how [GeneratesMethod] attribute constructor argument resolution is performed in the source generator. Instead of implicitly accessing ConstructorArguments[0] (positional index), the generator now resolves the index by explicitly finding the constructor parameter named SameClassMethodName using Roslyn's IParameterSymbol. A test fixture usage is also updated to exercise the named-argument form of the attribute.

Changes:

  • Refactored GeneratesMethodGenerationTargetCollector to resolve the target method name argument by matching the constructor parameter name, instead of hard-coding index 0.
  • Added using MattSourceGenHelpers.Abstractions; to support nameof(GeneratesMethod.SameClassMethodName) in the generator.
  • Updated TestColorsClass in UnitTest1.cs to use an explicit named argument (sameClassMethodName:) for the [GeneratesMethod] attribute.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
MattSourceGenHelpers.Generators/GeneratesMethodGenerationTargetCollector.cs Core refactor: adds parameter name-based resolution loop instead of positional index [0]
MattSourceGenHelpers.Tests/UnitTest1.cs Updates attribute usage to named argument form to exercise the refactored path

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

for (int parameterIndex = 0; parameterIndex < constructorParameters.Length; parameterIndex++)
{
IParameterSymbol constructorParameter = constructorParameters[parameterIndex];
if (constructorParameter.Name.Equals(nameof(GeneratesMethod.SameClassMethodName), StringComparison.OrdinalIgnoreCase))

Copilot AI Feb 28, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison uses nameof(GeneratesMethod.SameClassMethodName), which evaluates to "SameClassMethodName" (PascalCase — the property name), while the constructor parameter name is "sameClassMethodName" (camelCase). The match only works because OrdinalIgnoreCase is used. This creates a subtle semantic mismatch: the intent is to find a constructor parameter by name, but the nameof expression refers to the property, not the parameter.

A more precise approach would be to compare directly against the known constructor parameter name using Ordinal comparison (e.g., "sameClassMethodName"), or to restructure the comparison to be case-sensitive with the correct casing. Since C# identifiers are case-sensitive, using OrdinalIgnoreCase for constructor parameter name matching could theoretically match unintended parameters if a different constructor overload were ever added with a similarly-named parameter differing only in case. While not a current bug, it reduces the clarity and robustness of the intent expressed in the code.

Suggested change
if (constructorParameter.Name.Equals(nameof(GeneratesMethod.SameClassMethodName), StringComparison.OrdinalIgnoreCase))
if (string.Equals(constructorParameter.Name, "sameClassMethodName", StringComparison.Ordinal))

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In generators, the GeneratesMethod parameter is used implicitly

3 participants