Skip to content

Commit 3450895

Browse files
authored
Ensure @typeparam directive descriptor is always set (#33456)
* Ensure @typeparam directive descriptor is always set * Set fallback RazorLang value for test scenarios
1 parent 5c51c98 commit 3450895

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNetCore.Razor.Language.Components
7+
{
8+
internal class ComponentConstrainedTypeParamDirective
9+
{
10+
public static DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective(
11+
"typeparam",
12+
DirectiveKind.SingleLine,
13+
builder =>
14+
{
15+
builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description);
16+
builder.AddOptionalGenericTypeConstraintToken(ComponentResources.TypeParamDirective_Constraint_Name, ComponentResources.TypeParamDirective_Constraint_Description);
17+
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
18+
builder.Description = ComponentResources.TypeParamDirective_Description;
19+
});
20+
21+
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
22+
{
23+
if (builder == null)
24+
{
25+
throw new ArgumentNullException(nameof(builder));
26+
}
27+
28+
builder.AddDirective(Directive, FileKinds.Component, FileKinds.ComponentImport);
29+
return builder;
30+
}
31+
}
32+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ protected override void OnDocumentStructureCreated(
100100
{
101101
@class.BaseType = ComponentsApi.ComponentBase.FullTypeName;
102102

103-
var typeParamReferences = documentNode.FindDirectiveReferences(ComponentTypeParamDirective.Directive);
103+
// Constrained type parameters are only supported in Razor language versions v6.0
104+
var razorLanguageVersion = codeDocument.GetParserOptions()?.Version ?? RazorLanguageVersion.Latest;
105+
var directiveType = razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0
106+
? ComponentConstrainedTypeParamDirective.Directive
107+
: ComponentTypeParamDirective.Directive;
108+
var typeParamReferences = documentNode.FindDirectiveReferences(directiveType);
104109
for (var i = 0; i < typeParamReferences.Count; i++)
105110
{
106111
var typeParamNode = (DirectiveIntermediateNode)typeParamReferences[i].Node;

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

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,23 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
77
{
88
internal class ComponentTypeParamDirective
99
{
10-
public static DirectiveDescriptor Directive;
10+
public static DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective(
11+
"typeparam",
12+
DirectiveKind.SingleLine,
13+
builder =>
14+
{
15+
builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description);
16+
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
17+
builder.Description = ComponentResources.TypeParamDirective_Description;
18+
});
1119

12-
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder, bool supportConstraints)
20+
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
1321
{
1422
if (builder == null)
1523
{
1624
throw new ArgumentNullException(nameof(builder));
1725
}
1826

19-
if (Directive == null)
20-
{
21-
// Do nothing and assume the first registration wins. In real life this directive is only ever registered once.
22-
if (supportConstraints)
23-
{
24-
Directive = DirectiveDescriptor.CreateDirective(
25-
"typeparam",
26-
DirectiveKind.SingleLine,
27-
builder =>
28-
{
29-
builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description);
30-
builder.AddOptionalGenericTypeConstraintToken(ComponentResources.TypeParamDirective_Constraint_Name, ComponentResources.TypeParamDirective_Constraint_Description);
31-
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
32-
builder.Description = ComponentResources.TypeParamDirective_Description;
33-
});
34-
}
35-
else
36-
{
37-
Directive = DirectiveDescriptor.CreateDirective(
38-
"typeparam",
39-
DirectiveKind.SingleLine,
40-
builder =>
41-
{
42-
builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description);
43-
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
44-
builder.Description = ComponentResources.TypeParamDirective_Description;
45-
});
46-
}
47-
}
48-
4927
builder.AddDirective(Directive, FileKinds.Component, FileKinds.ComponentImport);
5028
return builder;
5129
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,16 @@ private static void AddComponentFeatures(RazorProjectEngineBuilder builder, Razo
222222
ComponentLayoutDirective.Register(builder);
223223
ComponentPageDirective.Register(builder);
224224

225-
// Unconditionally enable the feature for the time being until we flow the SDK with the flow version
226-
// into the repository.
227-
ComponentTypeParamDirective.Register(
228-
builder,
229-
supportConstraints: true);
230-
//supportConstraints: razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0);
225+
226+
227+
if (razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0)
228+
{
229+
ComponentConstrainedTypeParamDirective.Register(builder);
230+
}
231+
else
232+
{
233+
ComponentTypeParamDirective.Register(builder);
234+
}
231235

232236
if (razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_5_0) >= 0)
233237
{

0 commit comments

Comments
 (0)