diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentConstrainedTypeParamDirective.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentConstrainedTypeParamDirective.cs new file mode 100644 index 000000000000..97522da6110c --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentConstrainedTypeParamDirective.cs @@ -0,0 +1,32 @@ +// 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.Components +{ + internal class ComponentConstrainedTypeParamDirective + { + public static DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective( + "typeparam", + DirectiveKind.SingleLine, + builder => + { + builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description); + builder.AddOptionalGenericTypeConstraintToken(ComponentResources.TypeParamDirective_Constraint_Name, ComponentResources.TypeParamDirective_Constraint_Description); + builder.Usage = DirectiveUsage.FileScopedMultipleOccurring; + builder.Description = ComponentResources.TypeParamDirective_Description; + }); + + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.AddDirective(Directive, FileKinds.Component, FileKinds.ComponentImport); + return builder; + } + } +} diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs index f9796049db4d..af3ee7432fee 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs @@ -100,7 +100,12 @@ protected override void OnDocumentStructureCreated( { @class.BaseType = ComponentsApi.ComponentBase.FullTypeName; - var typeParamReferences = documentNode.FindDirectiveReferences(ComponentTypeParamDirective.Directive); + // Constrained type parameters are only supported in Razor language versions v6.0 + var razorLanguageVersion = codeDocument.GetParserOptions()?.Version ?? RazorLanguageVersion.Latest; + var directiveType = razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0 + ? ComponentConstrainedTypeParamDirective.Directive + : ComponentTypeParamDirective.Directive; + var typeParamReferences = documentNode.FindDirectiveReferences(directiveType); for (var i = 0; i < typeParamReferences.Count; i++) { var typeParamNode = (DirectiveIntermediateNode)typeParamReferences[i].Node; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentTypeParamDirective.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentTypeParamDirective.cs index eb40c33c00e3..55a11851440c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentTypeParamDirective.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentTypeParamDirective.cs @@ -7,45 +7,23 @@ namespace Microsoft.AspNetCore.Razor.Language.Components { internal class ComponentTypeParamDirective { - public static DirectiveDescriptor Directive; + public static DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective( + "typeparam", + DirectiveKind.SingleLine, + builder => + { + builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description); + builder.Usage = DirectiveUsage.FileScopedMultipleOccurring; + builder.Description = ComponentResources.TypeParamDirective_Description; + }); - public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder, bool supportConstraints) + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } - if (Directive == null) - { - // Do nothing and assume the first registration wins. In real life this directive is only ever registered once. - if (supportConstraints) - { - Directive = DirectiveDescriptor.CreateDirective( - "typeparam", - DirectiveKind.SingleLine, - builder => - { - builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description); - builder.AddOptionalGenericTypeConstraintToken(ComponentResources.TypeParamDirective_Constraint_Name, ComponentResources.TypeParamDirective_Constraint_Description); - builder.Usage = DirectiveUsage.FileScopedMultipleOccurring; - builder.Description = ComponentResources.TypeParamDirective_Description; - }); - } - else - { - Directive = DirectiveDescriptor.CreateDirective( - "typeparam", - DirectiveKind.SingleLine, - builder => - { - builder.AddMemberToken(ComponentResources.TypeParamDirective_Token_Name, ComponentResources.TypeParamDirective_Token_Description); - builder.Usage = DirectiveUsage.FileScopedMultipleOccurring; - builder.Description = ComponentResources.TypeParamDirective_Description; - }); - } - } - builder.AddDirective(Directive, FileKinds.Component, FileKinds.ComponentImport); return builder; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectEngine.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectEngine.cs index 2b2e79923d12..27f2c8c6fafa 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectEngine.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectEngine.cs @@ -222,12 +222,16 @@ private static void AddComponentFeatures(RazorProjectEngineBuilder builder, Razo ComponentLayoutDirective.Register(builder); ComponentPageDirective.Register(builder); - // Unconditionally enable the feature for the time being until we flow the SDK with the flow version - // into the repository. - ComponentTypeParamDirective.Register( - builder, - supportConstraints: true); - //supportConstraints: razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0); + + + if (razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0) + { + ComponentConstrainedTypeParamDirective.Register(builder); + } + else + { + ComponentTypeParamDirective.Register(builder); + } if (razorLanguageVersion.CompareTo(RazorLanguageVersion.Version_5_0) >= 0) {