Moved typename and propertyname to extension methods
This commit is contained in:
parent
413c1c919f
commit
7c7bb627b9
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||||
var typeName = $"__Generated__{shortName}ViewComponentTagHelper";
|
var typeName = $"__Generated__{shortName}ViewComponentTagHelper";
|
||||||
var displayName = shortName + "ViewComponentTagHelper";
|
var displayName = shortName + "ViewComponentTagHelper";
|
||||||
var descriptorBuilder = TagHelperDescriptorBuilder.Create(ViewComponentTagHelperConventions.Kind, typeName, assemblyName)
|
var descriptorBuilder = TagHelperDescriptorBuilder.Create(ViewComponentTagHelperConventions.Kind, typeName, assemblyName)
|
||||||
.TypeName(typeName)
|
.SetTypeName(typeName)
|
||||||
.DisplayName(displayName);
|
.DisplayName(displayName);
|
||||||
|
|
||||||
if (TryFindInvokeMethod(type, out var method, out var diagnostic))
|
if (TryFindInvokeMethod(type, out var method, out var diagnostic))
|
||||||
|
|
@ -186,7 +186,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||||
{
|
{
|
||||||
attributeBuilder
|
attributeBuilder
|
||||||
.Name(lowerKebabName)
|
.Name(lowerKebabName)
|
||||||
.PropertyName(parameter.Name)
|
.SetPropertyName(parameter.Name)
|
||||||
.TypeName(typeName)
|
.TypeName(typeName)
|
||||||
.DisplayName($"{simpleName} {containingDisplayName}.{parameter.Name}");
|
.DisplayName($"{simpleName} {containingDisplayName}.{parameter.Name}");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
{
|
{
|
||||||
public abstract class BoundAttributeDescriptorBuilder
|
public abstract class BoundAttributeDescriptorBuilder
|
||||||
{
|
{
|
||||||
public abstract BoundAttributeDescriptorBuilder Name(string name);
|
public abstract IDictionary<string, string> Metadata { get; }
|
||||||
|
|
||||||
public abstract BoundAttributeDescriptorBuilder PropertyName(string propertyName);
|
public abstract BoundAttributeDescriptorBuilder Name(string name);
|
||||||
|
|
||||||
public abstract BoundAttributeDescriptorBuilder TypeName(string typeName);
|
public abstract BoundAttributeDescriptorBuilder TypeName(string typeName);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
{
|
||||||
|
public static class BoundAttributeDescriptorBuilderExtensions
|
||||||
|
{
|
||||||
|
public static BoundAttributeDescriptorBuilder SetPropertyName(this BoundAttributeDescriptorBuilder builder, string propertyName)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (propertyName == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.AddMetadata(TagHelperMetadata.Common.PropertyName, propertyName);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetPropertyName(this BoundAttributeDescriptorBuilder builder)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (builder.Metadata.ContainsKey(TagHelperMetadata.Common.PropertyName))
|
||||||
|
{
|
||||||
|
return builder.Metadata[TagHelperMetadata.Common.PropertyName];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -50,6 +50,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
_metadata = new Dictionary<string, string>();
|
_metadata = new Dictionary<string, string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override IDictionary<string, string> Metadata => _metadata;
|
||||||
|
|
||||||
public override BoundAttributeDescriptorBuilder Name(string name)
|
public override BoundAttributeDescriptorBuilder Name(string name)
|
||||||
{
|
{
|
||||||
_name = name;
|
_name = name;
|
||||||
|
|
@ -57,13 +59,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override BoundAttributeDescriptorBuilder PropertyName(string propertyName)
|
|
||||||
{
|
|
||||||
_metadata[TagHelperMetadata.Common.PropertyName] = propertyName;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override BoundAttributeDescriptorBuilder TypeName(string typeName)
|
public override BoundAttributeDescriptorBuilder TypeName(string typeName)
|
||||||
{
|
{
|
||||||
_typeName = typeName;
|
_typeName = typeName;
|
||||||
|
|
@ -153,9 +148,12 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return _displayName;
|
return _displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var parentTypeName = _parent.GetTypeName();
|
||||||
|
var propertyName = this.GetPropertyName();
|
||||||
|
|
||||||
if (_typeName != null &&
|
if (_typeName != null &&
|
||||||
_metadata.ContainsKey(TagHelperMetadata.Common.PropertyName) &&
|
propertyName != null &&
|
||||||
_parent.Metadata.ContainsKey(TagHelperMetadata.Common.TypeName))
|
parentTypeName != null)
|
||||||
{
|
{
|
||||||
// This looks like a normal c# property, so lets compute a display name based on that.
|
// This looks like a normal c# property, so lets compute a display name based on that.
|
||||||
if (!PrimitiveDisplayTypeNameLookups.TryGetValue(_typeName, out var simpleTypeName))
|
if (!PrimitiveDisplayTypeNameLookups.TryGetValue(_typeName, out var simpleTypeName))
|
||||||
|
|
@ -163,7 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
simpleTypeName = _typeName;
|
simpleTypeName = _typeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{simpleTypeName} {_parent.Metadata[TagHelperMetadata.Common.TypeName]}.{_metadata[TagHelperMetadata.Common.PropertyName]}";
|
return $"{simpleTypeName} {parentTypeName}.{propertyName}";
|
||||||
}
|
}
|
||||||
|
|
||||||
return _name;
|
return _name;
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
_metadata = new Dictionary<string, string>(StringComparer.Ordinal);
|
_metadata = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDictionary<string, string> Metadata => _metadata;
|
public override IDictionary<string, string> Metadata => _metadata;
|
||||||
|
|
||||||
public override TagHelperDescriptorBuilder BindAttribute(Action<BoundAttributeDescriptorBuilder> configure)
|
public override TagHelperDescriptorBuilder BindAttribute(Action<BoundAttributeDescriptorBuilder> configure)
|
||||||
{
|
{
|
||||||
|
|
@ -114,17 +114,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override TagHelperDescriptorBuilder TypeName(string typeName)
|
|
||||||
{
|
|
||||||
if (typeName == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(typeName));
|
|
||||||
}
|
|
||||||
|
|
||||||
_metadata[TagHelperMetadata.Common.TypeName] = typeName;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override TagHelperDescriptor Build()
|
public override TagHelperDescriptor Build()
|
||||||
{
|
{
|
||||||
var validationDiagnostics = Validate();
|
var validationDiagnostics = Validate();
|
||||||
|
|
@ -192,7 +181,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return _displayName;
|
return _displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _metadata.ContainsKey(TagHelperMetadata.Common.TypeName) ? _metadata[TagHelperMetadata.Common.TypeName] : _name;
|
return this.GetTypeName() ?? _name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<RazorDiagnostic> Validate()
|
private IEnumerable<RazorDiagnostic> Validate()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
{
|
{
|
||||||
|
|
@ -41,6 +42,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return new DefaultTagHelperDescriptorBuilder(kind, name, assemblyName);
|
return new DefaultTagHelperDescriptorBuilder(kind, name, assemblyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract IDictionary<string, string> Metadata { get; }
|
||||||
|
|
||||||
public abstract TagHelperDescriptorBuilder BindAttribute(Action<BoundAttributeDescriptorBuilder> configure);
|
public abstract TagHelperDescriptorBuilder BindAttribute(Action<BoundAttributeDescriptorBuilder> configure);
|
||||||
|
|
||||||
public abstract TagHelperDescriptorBuilder TagMatchingRule(Action<TagMatchingRuleDescriptorBuilder> configure);
|
public abstract TagHelperDescriptorBuilder TagMatchingRule(Action<TagMatchingRuleDescriptorBuilder> configure);
|
||||||
|
|
@ -57,8 +60,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
public abstract TagHelperDescriptorBuilder DisplayName(string displayName);
|
public abstract TagHelperDescriptorBuilder DisplayName(string displayName);
|
||||||
|
|
||||||
public abstract TagHelperDescriptorBuilder TypeName(string typeName);
|
|
||||||
|
|
||||||
public abstract TagHelperDescriptor Build();
|
public abstract TagHelperDescriptor Build();
|
||||||
|
|
||||||
public abstract void Reset();
|
public abstract void Reset();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
{
|
||||||
|
public static class TagHelperDescriptorBuilderExtensions
|
||||||
|
{
|
||||||
|
public static TagHelperDescriptorBuilder SetTypeName(this TagHelperDescriptorBuilder builder, string typeName)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeName == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(typeName));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.AddMetadata(TagHelperMetadata.Common.TypeName, typeName);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetTypeName(this TagHelperDescriptorBuilder builder)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (builder.Metadata.ContainsKey(TagHelperMetadata.Common.TypeName))
|
||||||
|
{
|
||||||
|
return builder.Metadata[TagHelperMetadata.Common.TypeName];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
var typeName = GetFullName(type);
|
var typeName = GetFullName(type);
|
||||||
var assemblyName = type.ContainingAssembly.Identity.Name;
|
var assemblyName = type.ContainingAssembly.Identity.Name;
|
||||||
var descriptorBuilder = TagHelperDescriptorBuilder.Create(typeName, assemblyName);
|
var descriptorBuilder = TagHelperDescriptorBuilder.Create(typeName, assemblyName);
|
||||||
descriptorBuilder.TypeName(typeName);
|
descriptorBuilder.SetTypeName(typeName);
|
||||||
|
|
||||||
AddBoundAttributes(type, descriptorBuilder);
|
AddBoundAttributes(type, descriptorBuilder);
|
||||||
AddTagMatchingRules(type, descriptorBuilder);
|
AddTagMatchingRules(type, descriptorBuilder);
|
||||||
|
|
@ -210,7 +210,7 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
var typeName = GetFullName(property.Type);
|
var typeName = GetFullName(property.Type);
|
||||||
builder
|
builder
|
||||||
.TypeName(typeName)
|
.TypeName(typeName)
|
||||||
.PropertyName(property.Name);
|
.SetPropertyName(property.Name);
|
||||||
|
|
||||||
if (hasPublicSetter)
|
if (hasPublicSetter)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
{
|
||||||
|
public static class TestBoundAttributeDescriptorBuilderExtensions
|
||||||
|
{
|
||||||
|
public static BoundAttributeDescriptorBuilder PropertyName(this BoundAttributeDescriptorBuilder builder, string propertyName)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.SetPropertyName(propertyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
{
|
||||||
|
public static class TestTagHelperDescriptorBuilderExtensions
|
||||||
|
{
|
||||||
|
public static TagHelperDescriptorBuilder TypeName(this TagHelperDescriptorBuilder builder, string typeName)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.SetTypeName(typeName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
|
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
|
||||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Runtime\Microsoft.AspNetCore.Razor.Runtime.csproj" />
|
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Runtime\Microsoft.AspNetCore.Razor.Runtime.csproj" />
|
||||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor\Microsoft.AspNetCore.Razor.csproj" />
|
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor\Microsoft.AspNetCore.Razor.csproj" />
|
||||||
|
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue