From c7bb7a4d04ce2e3fbe9e553664fd024e9c216c16 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 11 Jun 2021 01:39:23 +0000 Subject: [PATCH 1/2] Ensure @typeparam directive descriptor is always set --- .../ComponentConstrainedTypeParamDirective.cs | 32 ++++++++++++++ .../ComponentDocumentClassifierPass.cs | 7 +++- .../Components/ComponentTypeParamDirective.cs | 42 +++++-------------- .../src/RazorProjectEngine.cs | 16 ++++--- 4 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentConstrainedTypeParamDirective.cs 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..9f036118ce1f 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); + var razorLanguageVersion = codeDocument.GetParserOptions().Version; + // Constrained type parameters are support in Razor language versions v6.0 + 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) { From bd08923b51e17da582578e07576f2eb405d1bed5 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 11 Jun 2021 02:26:37 +0000 Subject: [PATCH 2/2] Set fallback RazorLang value for test scenarios --- .../src/Components/ComponentDocumentClassifierPass.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9f036118ce1f..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,8 +100,8 @@ protected override void OnDocumentStructureCreated( { @class.BaseType = ComponentsApi.ComponentBase.FullTypeName; - var razorLanguageVersion = codeDocument.GetParserOptions().Version; - // Constrained type parameters are support in Razor language versions v6.0 + // 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;