Skip to content

Commit

Permalink
feat: Enable support for SupplyFrom cascading attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Feb 4, 2024
1 parent 77e8c54 commit 2722aca
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ static string GetInterceptorFilePath(SyntaxTree tree, Compilation compilation)

static bool IsParameterOrCascadingParameter(ISymbol member)
{
return member.GetAttributes().Any(attr =>
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
}

static StubPropertyInfo CreateFromProperty(IPropertySymbol member)
Expand Down
47 changes: 37 additions & 10 deletions src/bunit.generators/Web.Stubs/AttributeLineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ namespace Bunit.Web.Stubs;

internal static class AttributeLineGenerator
{
private const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute";
private const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute";

public static string GetAttributeLine(ISymbol member)
{
var attribute = member.GetAttributes().First(attr =>
attr.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier ||
attr.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier);
var attribute = member.GetAttributes().First(SupportedAttributes.IsSupportedAttribute);

var attributeLine = new StringBuilder("\t[");
if (attribute.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier)
if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.ParameterAttributeQualifier)
{
attributeLine.Append($"global::{ParameterAttributeQualifier}");
attributeLine.Append($"global::{SupportedAttributes.ParameterAttributeQualifier}");
var captureUnmatchedValuesArg = attribute.NamedArguments
.FirstOrDefault(arg => arg.Key == "CaptureUnmatchedValues").Value;
if (captureUnmatchedValuesArg.Value is bool captureUnmatchedValues)
Expand All @@ -27,9 +22,41 @@ public static string GetAttributeLine(ISymbol member)
attributeLine.Append($"(CaptureUnmatchedValues = {captureString})");
}
}
else if (attribute.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier)
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.CascadingParameterAttributeQualifier)
{
attributeLine.Append($"global::{SupportedAttributes.CascadingParameterAttributeQualifier}");
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
if (!nameArg.IsNull)
{
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
}
}
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier)
{
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}");
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
if (!nameArg.IsNull)
{
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
}
}
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromFormAttributeQualifier)
{
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromFormAttributeQualifier}");
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
if (!nameArg.IsNull)
{
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
}
var formNameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "FormName").Value;
if (!formNameArg.IsNull)
{
attributeLine.Append($", (FormName = \"{formNameArg.Value}\")");
}
}
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier)
{
attributeLine.Append($"global::{CascadingParameterAttributeQualifier}");
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}");
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
if (!nameArg.IsNull)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext co

static bool IsParameterOrCascadingParameter(ISymbol member)
{
return member.GetAttributes().Any(attr =>
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
}

static StubPropertyInfo CreateFromProperty(IPropertySymbol member)
Expand Down
17 changes: 17 additions & 0 deletions src/bunit.generators/Web.Stubs/SupportedAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.CodeAnalysis;

namespace Bunit.Web.Stubs;

internal static class SupportedAttributes
{
public const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute";
public const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute";
public const string SupplyParameterFromQueryAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute";
public const string SupplyParameterFromFormAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute";

public static bool IsSupportedAttribute(AttributeData attribute)
{
var displayString = attribute.AttributeClass?.ToDisplayString();
return displayString is ParameterAttributeQualifier or CascadingParameterAttributeQualifier or SupplyParameterFromQueryAttributeQualifier or SupplyParameterFromFormAttributeQualifier;
}
}

0 comments on commit 2722aca

Please sign in to comment.