Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void DisplayName_SetsDescriptorsDisplayName()

var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test");

var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder.DisplayName(expectedDisplayName);

// Act
Expand All @@ -33,7 +33,7 @@ public void DisplayName_DefaultsToPropertyLookingDisplayName()
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test");
tagHelperBuilder.Metadata(TypeName("TestTagHelper"));

var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder
.TypeName(typeof(int).FullName)
.Metadata(PropertyName("SomeProperty"));
Expand All @@ -56,12 +56,12 @@ public void Metadata_Same()

var metadata = MetadataCollection.Create(PropertyName("SomeProperty"));

var builder1 = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind)
var builder1 = new BoundAttributeDescriptorBuilder(tagHelperBuilder)
{
TypeName = typeof(int).FullName
};

var builder2 = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind)
var builder2 = new BoundAttributeDescriptorBuilder(tagHelperBuilder)
{
TypeName = typeof(int).FullName
};
Expand All @@ -86,12 +86,12 @@ public void Metadata_NotSame()
// Arrange
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test");

var builder1 = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind)
var builder1 = new BoundAttributeDescriptorBuilder(tagHelperBuilder)
{
TypeName = typeof(int).FullName
};

var builder2 = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind)
var builder2 = new BoundAttributeDescriptorBuilder(tagHelperBuilder)
{
TypeName = typeof(int).FullName
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void WriteTagHelperProperty_RendersCorrectly()
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test");
tagHelperBuilder.Metadata(TypeName("FooTagHelper"));

var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder
.Name("Foo")
.TypeName("System.String")
Expand Down Expand Up @@ -173,7 +173,7 @@ public void WriteSetPreallocatedTagHelperProperty_IndexerAttribute_RendersCorrec
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test");
tagHelperBuilder.Metadata(TypeName("FooTagHelper"));

var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder
.Name("Foo")
.TypeName("System.Collections.Generic.Dictionary<System.String, System.String>")
Expand Down Expand Up @@ -223,7 +223,7 @@ public void WriteSetPreallocatedTagHelperProperty_IndexerAttribute_MultipleValue
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test");
tagHelperBuilder.Metadata(TypeName("FooTagHelper"));

var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder
.Name("Foo")
.TypeName("System.Collections.Generic.Dictionary<System.String, System.String>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void CanSatisfyBoundAttribute_IndexerAttribute_ReturnsFalseIsNotMatching(
{
// Arrange
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test");
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder.AsDictionary("asp-", typeof(Dictionary<string, string>).FullName);

var boundAttribute = builder.Build();
Expand All @@ -180,7 +180,7 @@ public void CanSatisfyBoundAttribute_IndexerAttribute_ReturnsTrueIfMatching()
{
// Arrange
var tagHelperBuilder = new TagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test");
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind);
var builder = new BoundAttributeDescriptorBuilder(tagHelperBuilder);
builder.AsDictionary("asp-", typeof(Dictionary<string, string>).FullName);

var boundAttribute = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.AspNetCore.Razor.Utilities;

namespace Microsoft.AspNetCore.Razor.Language;

public sealed class AllowedChildTagDescriptor : TagHelperObject<AllowedChildTagDescriptor>
{
private TagHelperDescriptor? _parent;

public string Name { get; }
public string DisplayName { get; }

Expand All @@ -24,6 +27,17 @@ private protected override void BuildChecksum(in Checksum.Builder builder)
builder.AppendData(DisplayName);
}

public TagHelperDescriptor Parent
=> _parent ?? ThrowHelper.ThrowInvalidOperationException<TagHelperDescriptor>(Resources.Parent_has_not_been_set);

internal void SetParent(TagHelperDescriptor parent)
{
Debug.Assert(parent != null);
Debug.Assert(_parent == null);

_parent = parent;
}

public override string ToString()
=> DisplayName ?? base.ToString()!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Utilities;

Expand Down Expand Up @@ -31,7 +32,8 @@ private enum BoundAttributeFlags
private readonly BoundAttributeFlags _flags;
private readonly DocumentationObject _documentationObject;

public string Kind { get; }
private TagHelperDescriptor? _parent;

public string Name { get; }
public string TypeName { get; }
public string DisplayName { get; }
Expand All @@ -54,7 +56,6 @@ private enum BoundAttributeFlags
public MetadataCollection Metadata { get; }

internal BoundAttributeDescriptor(
string kind,
string name,
string typeName,
bool isEnum,
Expand All @@ -71,7 +72,6 @@ internal BoundAttributeDescriptor(
ImmutableArray<RazorDiagnostic> diagnostics)
: base(diagnostics)
{
Kind = kind;
Name = name;
TypeName = typeName;
IndexerNamePrefix = indexerNamePrefix;
Expand All @@ -82,6 +82,11 @@ internal BoundAttributeDescriptor(
Parameters = parameters.NullToEmpty();
Metadata = metadata ?? MetadataCollection.Empty;

foreach (var parameter in Parameters)
{
parameter.SetParent(this);
}

BoundAttributeFlags flags = 0;

if (isEnum)
Expand Down Expand Up @@ -134,7 +139,6 @@ internal BoundAttributeDescriptor(

private protected override void BuildChecksum(in Checksum.Builder builder)
{
builder.AppendData(Kind);
builder.AppendData(Name);
builder.AppendData(TypeName);
builder.AppendData(IndexerNamePrefix);
Expand All @@ -161,6 +165,17 @@ private protected override void BuildChecksum(in Checksum.Builder builder)
builder.AppendData(Metadata.Checksum);
}

public TagHelperDescriptor Parent
=> _parent ?? ThrowHelper.ThrowInvalidOperationException<TagHelperDescriptor>(Resources.Parent_has_not_been_set);

internal void SetParent(TagHelperDescriptor parent)
{
Debug.Assert(parent != null);
Debug.Assert(_parent == null);

_parent = parent;
}

public string? Documentation => _documentationObject.GetText();

internal DocumentationObject DocumentationObject => _documentationObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public sealed partial class BoundAttributeDescriptorBuilder : TagHelperObjectBui

[AllowNull]
private TagHelperDescriptorBuilder _parent;
[AllowNull]
private string _kind;
private DocumentationObject _documentationObject;
private MetadataHolder _metadata;
private bool? _caseSensitive;
Expand All @@ -45,10 +43,9 @@ private BoundAttributeDescriptorBuilder()
{
}

internal BoundAttributeDescriptorBuilder(TagHelperDescriptorBuilder parent, string kind)
internal BoundAttributeDescriptorBuilder(TagHelperDescriptorBuilder parent)
{
_parent = parent;
_kind = kind;
}

[AllowNull]
Expand Down Expand Up @@ -93,7 +90,7 @@ public void BindAttributeParameter(Action<BoundAttributeParameterDescriptorBuild
throw new ArgumentNullException(nameof(configure));
}

var builder = BoundAttributeParameterDescriptorBuilder.GetInstance(this, _kind);
var builder = BoundAttributeParameterDescriptorBuilder.GetInstance(this);
configure(builder);
Parameters.Add(builder);
}
Expand All @@ -111,7 +108,6 @@ internal void SetDocumentation(DocumentationDescriptor? documentation)
private protected override BoundAttributeDescriptor BuildCore(ImmutableArray<RazorDiagnostic> diagnostics)
{
return new BoundAttributeDescriptor(
_kind,
Name ?? string.Empty,
TypeName ?? string.Empty,
IsEnum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ public partial class BoundAttributeDescriptorBuilder
{
internal static readonly ObjectPool<BoundAttributeDescriptorBuilder> Pool = DefaultPool.Create(Policy.Instance);

internal static BoundAttributeDescriptorBuilder GetInstance(TagHelperDescriptorBuilder parent, string kind)
internal static BoundAttributeDescriptorBuilder GetInstance(TagHelperDescriptorBuilder parent)
{
var builder = Pool.Get();

builder._parent = parent;
builder._kind = kind;

return builder;
}

private protected override void Reset()
{
_parent = null;
_kind = null;
_documentationObject = default;
_caseSensitive = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;

namespace Microsoft.AspNetCore.Razor.Language;

public static class BoundAttributeDescriptorExtensions
{
public static string GetPropertyName(this BoundAttributeDescriptor attribute)
public static string? GetPropertyName(this BoundAttributeDescriptor attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
ArgHelper.ThrowIfNull(attribute);

attribute.Metadata.TryGetValue(TagHelperMetadata.Common.PropertyName, out var propertyName);
return propertyName;
}

public static string GetGloballyQualifiedTypeName(this BoundAttributeDescriptor attribute)
public static string? GetGloballyQualifiedTypeName(this BoundAttributeDescriptor attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
ArgHelper.ThrowIfNull(attribute);

attribute.Metadata.TryGetValue(TagHelperMetadata.Common.GloballyQualifiedTypeName, out var propertyName);
return propertyName;
}

public static bool IsDefaultKind(this BoundAttributeDescriptor attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
ArgHelper.ThrowIfNull(attribute);

return attribute.Kind == TagHelperConventions.DefaultKind;
return attribute.Parent.Kind == TagHelperConventions.DefaultKind;
}

internal static bool ExpectsStringValue(this BoundAttributeDescriptor attribute, string name)
Expand All @@ -65,20 +54,14 @@ internal static bool ExpectsBooleanValue(this BoundAttributeDescriptor attribute

public static bool IsDefaultKind(this BoundAttributeParameterDescriptor parameter)
{
if (parameter == null)
{
throw new ArgumentNullException(nameof(parameter));
}
ArgHelper.ThrowIfNull(parameter);

return parameter.Kind == TagHelperConventions.DefaultKind;
return parameter.Parent.Parent.Kind == TagHelperConventions.DefaultKind;
}

public static string GetPropertyName(this BoundAttributeParameterDescriptor parameter)
public static string? GetPropertyName(this BoundAttributeParameterDescriptor parameter)
{
if (parameter == null)
{
throw new ArgumentNullException(nameof(parameter));
}
ArgHelper.ThrowIfNull(parameter);

parameter.Metadata.TryGetValue(TagHelperMetadata.Common.PropertyName, out var propertyName);
return propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.AspNetCore.Razor.Utilities;

namespace Microsoft.AspNetCore.Razor.Language;
Expand All @@ -21,7 +22,8 @@ private enum BoundAttributeParameterFlags
private readonly BoundAttributeParameterFlags _flags;
private readonly DocumentationObject _documentationObject;

public string Kind { get; }
private BoundAttributeDescriptor? _parent;

public string Name { get; }
public string TypeName { get; }
public string DisplayName { get; }
Expand All @@ -34,7 +36,6 @@ private enum BoundAttributeParameterFlags
public MetadataCollection Metadata { get; }

internal BoundAttributeParameterDescriptor(
string kind,
string name,
string typeName,
bool isEnum,
Expand All @@ -45,7 +46,6 @@ internal BoundAttributeParameterDescriptor(
ImmutableArray<RazorDiagnostic> diagnostics)
: base(diagnostics)
{
Kind = kind;
Name = name;
TypeName = typeName;
_documentationObject = documentationObject;
Expand Down Expand Up @@ -79,7 +79,6 @@ internal BoundAttributeParameterDescriptor(

private protected override void BuildChecksum(in Checksum.Builder builder)
{
builder.AppendData(Kind);
builder.AppendData(Name);
builder.AppendData(TypeName);
builder.AppendData(DisplayName);
Expand All @@ -93,6 +92,17 @@ private protected override void BuildChecksum(in Checksum.Builder builder)
builder.AppendData(Metadata.Checksum);
}

public BoundAttributeDescriptor Parent
=> _parent ?? ThrowHelper.ThrowInvalidOperationException<BoundAttributeDescriptor>(Resources.Parent_has_not_been_set);

internal void SetParent(BoundAttributeDescriptor parent)
{
Debug.Assert(parent != null);
Debug.Assert(_parent == null);

_parent = parent;
}

public string? Documentation => _documentationObject.GetText();

internal DocumentationObject DocumentationObject => _documentationObject;
Expand Down
Loading