Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeWriter and Positional Arguments #1539

Open
DorianGreen opened this issue Jan 10, 2025 · 1 comment
Open

AttributeWriter and Positional Arguments #1539

DorianGreen opened this issue Jan 10, 2025 · 1 comment

Comments

@DorianGreen
Copy link

I have an issue with attributes with positional arguments.
some attributes like https://github.com/SteveDunn/Vogen/blob/main/src/Vogen.SharedTypes/VogenDefaultsAttribute.cs do not contain named arguments.

when using the attribute and providing just one argument as a positional one like

[assembly: VogenDefaults(staticAbstractsGeneration: StaticAbstractsGeneration.ValueObjectsDeriveFromTheInterface)]

The AttributeWriter only detects the explicit named arguments and ignores the other argument's default values.
The generated attribute looks like this:

new global::Vogen.VogenDefaultsAttribute(global::Vogen.StaticAbstractsGeneration.ValueObjectsDeriveFromTheInterface)

This results in compilation errors as it omits the positional name so the arguments are out of order

Copilot suggests the following to fetch the default values when arguments are not explicitly provided:

var constructorArguments = attributeData.ConstructorArguments.Select((arg, index) =>
        {
            var parameter = attributeData.AttributeConstructor.Parameters[index];
            if (arg.IsNull && parameter.HasExplicitDefaultValue)
            {
                return TypedConstantParser.GetTypedConstantValue(context.SemanticModel, parameter.ExplicitDefaultValue, arg.Type);
            }
            return TypedConstantParser.GetTypedConstantValue(context.SemanticModel, attributeSyntax.DescendantNodes().OfType<AttributeArgumentSyntax>().ElementAt(index).Expression, arg.Type);
        });
@thomhurst
Copy link
Owner

Thanks for the report @DorianGreen! I'm not available the next few days, but I'll try and get a fix out for you next week 😄

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

No branches or pull requests

2 participants