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

Allow generate method to handle delegates #11402

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ryzngard
Copy link
Contributor

Fixes #11380

@ryzngard ryzngard requested review from a team as code owners January 18, 2025 01:24
{
eventParameterType = argument.ToString();
if (attribute.IsGenericTypedProperty())
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to have an extra out parameter here to prevent "Generate Async Event Handler" showing up, or does the runtime happily call Action<T> delegates in an async context?

Comment on lines 63 to 66
/// <param name="tagHelper"></param>
/// <param name="binding"></param>
/// <param name="typeName"></param>
/// <returns></returns>
Copy link
Member

Choose a reason for hiding this comment

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

Consider removing these empty docstring tags.

/// <returns></returns>
public static bool TryGetGenericTypeName(this TagHelperDescriptor tagHelper, TagHelperBinding binding, [NotNullWhen(true)] out string typeName)
{
if (!tagHelper.IsComponentTagHelper)
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the method only works for components, not normal "tag helpers", consider renaming it and updating its doc string to make that clearer.

/// <param name="binding"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public static bool TryGetGenericTypeName(this TagHelperDescriptor tagHelper, TagHelperBinding binding, [NotNullWhen(true)] out string typeName)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think [NotNullWhen(true)] has any effect since this whole file is #nullable disabled. Consider nullable-enabling the new code.

// it has to be looked up in the bindingAttributes to find the value for that type. This assumes that the type is valid because the user
// provided it, and if it's not the calling context probably doesn't care.
if (boundAttribute.Metadata.TryGetValue(ComponentMetadata.Component.TypeParameterKey, out var value) &&
string.Equals(bool.TrueString, value) &&
Copy link
Member

Choose a reason for hiding this comment

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

Consider using TagHelperBoundAttributeDescriptorExtensions.IsTypeParameterProperty extension method.

string.Equals(bool.TrueString, value) &&
boundAttribute.Metadata.TryGetValue(TagHelperMetadata.Common.PropertyName, out var propertyName) &&
!string.IsNullOrEmpty(propertyName) &&
binding.Attributes.FirstOrDefault(kvp => kvp.Key == propertyName) is { Value: var bindingTypeName})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
binding.Attributes.FirstOrDefault(kvp => kvp.Key == propertyName) is { Value: var bindingTypeName})
binding.Attributes.FirstOrDefault(propertyName, static (kvp, propertyName) => kvp.Key == propertyName) is { Value: var bindingTypeName })

// provided it, and if it's not the calling context probably doesn't care.
if (boundAttribute.Metadata.TryGetValue(ComponentMetadata.Component.TypeParameterKey, out var value) &&
string.Equals(bool.TrueString, value) &&
boundAttribute.Metadata.TryGetValue(TagHelperMetadata.Common.PropertyName, out var propertyName) &&
Copy link
Member

Choose a reason for hiding this comment

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

Consider using BoundAttributeDescriptorExtensions.GetPropertyName extension method.

Comment on lines +275 to +295
// Strip 'global::' from the candidate.
if (candidate.Span.StartsWith("global::".AsSpan()))
{
candidate = candidate["global::".Length..];
}

if (!candidate.Span.StartsWith("System.Action".AsSpan()))
{
argument = default;
return false;
}


candidate = candidate["System.Action".Length..];
if (candidate.Span is not ['<', .., '>'])
{
argument = default;
return false;
}

candidate = candidate[1..^1];
Copy link
Member

Choose a reason for hiding this comment

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

Can't you reuse the method above for all this code?

return false;
}

internal static bool TryGetGenericActionArgument(ReadOnlyMemory<char> candidate, string genericType, [NotNullWhen(true)] out string argument)
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a doc comment with an example to this method, because it's not obvious what it's doing.

///
/// <code>
/// &lt;MyTagHelper
/// TItem="string"
Copy link
Member

Choose a reason for hiding this comment

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

If there are multiple generic arguments, this method will return just the first one, right? Is that okay (and should it be mentioned in the doc comment) or would it be better if the method returned false in that case?

@@ -94,6 +91,7 @@ public static bool TryGetGenericTypeName(this TagHelperDescriptor tagHelper, Tag
typeName = null;
return false;
}
#nullable restore
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#nullable restore
#nullable disable

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.

Generate handler not working for FluentDragContainer OnDrag*
3 participants