Skip to content

Commit 6fcd12e

Browse files
author
N. Taylor Mullen
committed
Add Razor component TagHelper detection.
- WTE needs a way to detect if a `TagHelperDescriptor` is a component based `TagHelper` in order to turn features like validation, completion etc. on/off. - Updated tests to add verification points for the new `IsRazorComponentTagHelper` method.
1 parent f8d7f4c commit 6fcd12e

File tree

7 files changed

+32
-1
lines changed

7 files changed

+32
-1
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.Language/Components/TagHelperDescriptorExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public static bool IsComponentTagHelper(this TagHelperDescriptor tagHelper)
124124
throw new ArgumentNullException(nameof(tagHelper));
125125
}
126126

127-
return !tagHelper.Metadata.ContainsKey(ComponentMetadata.SpecialKindKey);
127+
return
128+
string.Equals(tagHelper.Kind, ComponentMetadata.Component.TagHelperKind) &&
129+
!tagHelper.Metadata.ContainsKey(ComponentMetadata.SpecialKindKey);
128130
}
129131

130132
public static bool IsEventHandlerTagHelper(this TagHelperDescriptor tagHelper)

src/Razor/src/Microsoft.AspNetCore.Razor.Language/TagHelperDescriptorExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.AspNetCore.Razor.Language.Components;
56

67
namespace Microsoft.AspNetCore.Razor.Language
78
{
@@ -38,5 +39,25 @@ public static bool KindUsesDefaultTagHelperRuntime(this TagHelperDescriptor tagH
3839
tagHelper.Metadata.TryGetValue(TagHelperMetadata.Runtime.Name, out var value);
3940
return string.Equals(TagHelperConventions.DefaultKind, value, StringComparison.Ordinal);
4041
}
42+
43+
public static bool IsComponentOrChildContentTagHelper(this TagHelperDescriptor tagHelper)
44+
{
45+
if (tagHelper == null)
46+
{
47+
throw new ArgumentNullException(nameof(tagHelper));
48+
}
49+
50+
if (tagHelper.IsComponentTagHelper())
51+
{
52+
return true;
53+
}
54+
55+
if (tagHelper.IsChildContentTagHelper())
56+
{
57+
return true;
58+
}
59+
60+
return false;
61+
}
4162
}
4263
}

src/Razor/test/Microsoft.CodeAnalysis.Razor.Test/BindTagHelperDescriptorProviderTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void SetParameters(ParameterCollection parameters) { }
7171
Assert.Equal(ComponentMetadata.Bind.RuntimeName, bind.Metadata[TagHelperMetadata.Runtime.Name]);
7272
Assert.False(bind.IsDefaultKind());
7373
Assert.False(bind.KindUsesDefaultTagHelperRuntime());
74+
Assert.False(bind.IsComponentOrChildContentTagHelper());
7475

7576
Assert.Equal("MyProperty", bind.Metadata[ComponentMetadata.Bind.ValueAttribute]);
7677
Assert.Equal("MyPropertyChanged", bind.Metadata[ComponentMetadata.Bind.ChangeAttribute]);
@@ -187,6 +188,7 @@ public void SetParameters(ParameterCollection parameters) { }
187188
Assert.Equal(ComponentMetadata.Bind.RuntimeName, bind.Metadata[TagHelperMetadata.Runtime.Name]);
188189
Assert.False(bind.IsDefaultKind());
189190
Assert.False(bind.KindUsesDefaultTagHelperRuntime());
191+
Assert.False(bind.IsComponentOrChildContentTagHelper());
190192

191193
Assert.Equal("MyProperty", bind.Metadata[ComponentMetadata.Bind.ValueAttribute]);
192194
Assert.Equal("MyPropertyChanged", bind.Metadata[ComponentMetadata.Bind.ChangeAttribute]);
@@ -334,6 +336,7 @@ public class BindAttributes
334336
Assert.Equal(ComponentMetadata.Bind.RuntimeName, bind.Metadata[TagHelperMetadata.Runtime.Name]);
335337
Assert.False(bind.IsDefaultKind());
336338
Assert.False(bind.KindUsesDefaultTagHelperRuntime());
339+
Assert.False(bind.IsComponentOrChildContentTagHelper());
337340

338341
Assert.Equal("myprop", bind.Metadata[ComponentMetadata.Bind.ValueAttribute]);
339342
Assert.Equal("myevent", bind.Metadata[ComponentMetadata.Bind.ChangeAttribute]);
@@ -698,6 +701,7 @@ public void Execute_BindFallback_CreatesDescriptor()
698701
Assert.Equal(ComponentMetadata.Bind.RuntimeName, bind.Metadata[TagHelperMetadata.Runtime.Name]);
699702
Assert.False(bind.IsDefaultKind());
700703
Assert.False(bind.KindUsesDefaultTagHelperRuntime());
704+
Assert.False(bind.IsComponentOrChildContentTagHelper());
701705

702706
Assert.False(bind.Metadata.ContainsKey(ComponentMetadata.Bind.ValueAttribute));
703707
Assert.False(bind.Metadata.ContainsKey(ComponentMetadata.Bind.ChangeAttribute));

src/Razor/test/Microsoft.CodeAnalysis.Razor.Test/ComponentTagHelperDescriptorProviderTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void SetParameters(ParameterCollection parameters) { }
6060
Assert.Equal(ComponentMetadata.Component.TagHelperKind, component.Kind);
6161
Assert.False(component.IsDefaultKind());
6262
Assert.False(component.KindUsesDefaultTagHelperRuntime());
63+
Assert.True(component.IsComponentOrChildContentTagHelper());
6364

6465
// No documentation in this test
6566
Assert.Null(component.Documentation);

src/Razor/test/Microsoft.CodeAnalysis.Razor.Test/EventHandlerTagHelperDescriptorProviderTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class EventHandlers
5656
Assert.Equal(ComponentMetadata.EventHandler.RuntimeName, item.Metadata[TagHelperMetadata.Runtime.Name]);
5757
Assert.False(item.IsDefaultKind());
5858
Assert.False(item.KindUsesDefaultTagHelperRuntime());
59+
Assert.False(item.IsComponentOrChildContentTagHelper());
5960

6061
Assert.Equal(
6162
"Sets the '@onclick' attribute to the provided string or delegate value. " +

src/Razor/test/Microsoft.CodeAnalysis.Razor.Test/KeyTagHelperDescriptorProviderTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void Execute_CreatesDescriptor()
3535
Assert.Equal(ComponentMetadata.Key.RuntimeName, item.Metadata[TagHelperMetadata.Runtime.Name]);
3636
Assert.False(item.IsDefaultKind());
3737
Assert.False(item.KindUsesDefaultTagHelperRuntime());
38+
Assert.False(item.IsComponentOrChildContentTagHelper());
3839

3940
Assert.Equal(
4041
"Ensures that the component or element will be preserved across renders if (and only if) the supplied key value matches.",

src/Razor/test/Microsoft.CodeAnalysis.Razor.Test/RefTagHelperDescriptorProviderTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void Execute_CreatesDescriptor()
3535
Assert.Equal(ComponentMetadata.Ref.RuntimeName, item.Metadata[TagHelperMetadata.Runtime.Name]);
3636
Assert.False(item.IsDefaultKind());
3737
Assert.False(item.KindUsesDefaultTagHelperRuntime());
38+
Assert.False(item.IsComponentOrChildContentTagHelper());
3839

3940
Assert.Equal(
4041
"Populates the specified field or property with a reference to the element or component.",

0 commit comments

Comments
 (0)