Skip to content

Commit

Permalink
Match component properties as case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Jul 19, 2024
1 parent a2fcfd4 commit 0a6e708
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
Expand Down Expand Up @@ -171,14 +172,28 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(
builder.SetDocumentation(xml);
}

// Collect properties exist with different casing.
using var caseSensitiveProperties = StringHashSetPool.OrdinalIgnoreCase.GetPooledObject();
using (var caseInsensitivePropertyNameMap = StringDictionaryPool<string>.OrdinalIgnoreCase.GetPooledObject())
{
foreach (var (property, _) in properties)
{
var existingPropertyName = caseInsensitivePropertyNameMap.Object.GetOrAdd(property.Name, property.Name);
if (existingPropertyName != property.Name)
{
caseSensitiveProperties.Object.Add(property.Name);
}
}
}

foreach (var (property, kind) in properties)
{
if (kind == PropertyKind.Ignored)
{
continue;
}

CreateProperty(builder, type, property, kind);
CreateProperty(builder, type, property, kind, caseSensitiveProperties.Object);
}

if (builder.BoundAttributes.Any(static a => a.IsParameterizedChildContentProperty()) &&
Expand All @@ -195,7 +210,7 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(
return builder.Build();
}

private static void CreateProperty(TagHelperDescriptorBuilder builder, INamedTypeSymbol containingSymbol, IPropertySymbol property, PropertyKind kind)
private static void CreateProperty(TagHelperDescriptorBuilder builder, INamedTypeSymbol containingSymbol, IPropertySymbol property, PropertyKind kind, ISet<string> caseSensitiveProperties)
{
builder.BindAttribute(pb =>
{
Expand All @@ -207,6 +222,8 @@ private static void CreateProperty(TagHelperDescriptorBuilder builder, INamedTyp
pb.IsEditorRequired = property.GetAttributes().Any(
static a => a.AttributeClass.HasFullName("Microsoft.AspNetCore.Components.EditorRequiredAttribute"));

pb.CaseSensitive = caseSensitiveProperties.Contains(property.Name);

metadata.Add(PropertyName(property.Name));
metadata.Add(GloballyQualifiedTypeName(property.Type.ToDisplayString(GloballyQualifiedFullNameTypeDisplayFormat)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public sealed partial class BoundAttributeDescriptorBuilder : TagHelperObjectBui
private string _kind;
private DocumentationObject _documentationObject;
private MetadataHolder _metadata;
private bool? _caseSensitive;

private BoundAttributeDescriptorBuilder()
{
Expand Down Expand Up @@ -76,7 +77,11 @@ public string? Documentation
public bool TryGetMetadataValue(string key, [NotNullWhen(true)] out string? value)
=> _metadata.TryGetMetadataValue(key, out value);

internal bool CaseSensitive => _parent.CaseSensitive;
internal bool CaseSensitive
{
get => _caseSensitive ?? _parent.CaseSensitive;
set => _caseSensitive = value;
}

private TagHelperObjectBuilderCollection<BoundAttributeParameterDescriptor, BoundAttributeParameterDescriptorBuilder> Parameters { get; }
= new(BoundAttributeParameterDescriptorBuilder.Pool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private protected override void Reset()
_parent = null;
_kind = null;
_documentationObject = default;
_caseSensitive = null;

Name = null;
TypeName = null;
Expand Down

0 comments on commit 0a6e708

Please # to comment.