-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Blazor] Adds support for specifying generic type constraints in Razor files #31800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is now ready for review |
SummaryWe are adding support for specifying generic type constraints in the @typeparam TParameter where TParameter : Base, ISomeInterface, class, new() MotivationCustomers are very vocal in our repository about wanting this feature. There is currently an alternative way to constrain a generic type defined in a razor file which is to create a partial class and apply the constraints there. Goals
Non-goals
Scenarios
Intellisense and colorizationError experienceErrors compared to similar directivesSemantic errorsRisks
Interaction with other parts of the framework
Detailed designThis is implemented by a compiler feature. To begin with, we have introduced a new DirectiveTokenType called GenericTypeConstraint that represents all the code between the We update the typeparam directive to optionally accept a GenericTypeConstraint directive token type to support associating constraints to the type. During parsing we process the GenericTypeConstraint and check that:
We consume all the tokens until the end of the line and create the directive. During the classification of the document we capture the constraint in addition to the generic type parameter name. In this context the constraint is the entire line from the When we generate the code, we pass the type as well as the constraint and after we've written the class name, base class, and implemented interfaces we add each constraint to the document in a new line. For example public class MyCompontent<TData> : ComponentBase
where TData : class
{
...
} The other interesting part of the equation is the generated design time code. In order to provide a proper error experience and intellisense, in design time builds we generate a unique method that we map to the line where the constraint is defined on the razor file. For the generated code above, the design time equivalent looks roughly like this: public class MyCompontent<TData> : ComponentBase
where TData : class
{
Action __RazorDesignTimeHelpers = () =>
{
Action __constraint1 = () =>
{
# pragma warning disable CS...// Generic type parameter hides class type parameter
# pragma warning disable CS...// Method not uses
# line 1 "<<path-to-razor-file.razor>>"
void __TypeParameter_TData<TDAta>() where TData : class
# pragma warning restore CS...
# pragma warning restore CS ..
}
}
} We generate a generic local function per constrained parameter with a generic type parameter of the same name and associate the definition with the one in the app. Drawbacks
Considered alternatives
Open questions
|
|
||
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder) | ||
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder, bool supportConstraints) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing this boolean through, should we just create a new ComponentTypeParamWithGeneric
directive that we then conditionally register in the RazorEngineBuilder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I'm understanding correctly.
The directives map 1 to 1 to language concepts, so it's not clear to me that is desirable to have two of them to represent the same concept. The problem with it is that we would need to litter the codebase with additional checks for the newly introduced directive, when the only difference is what is allowed per language version.
I think it makes more sense for all that to be configured one time in one place since it only varies by language version.
...zor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs
Outdated
Show resolved
Hide resolved
@@ -1327,14 +1337,16 @@ private void ParseExtensibleDirective(in SyntaxListBuilder<RazorSyntaxNode> buil | |||
{ | |||
AcceptWhile(IsSpacingTokenIncludingComments); | |||
|
|||
SpanContext.ChunkGenerator = SpanChunkGenerator.Null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good on spotting this!
AcceptAndMoveNext(); | ||
AcceptWhile(SyntaxKind.Whitespace); | ||
// Check that the type name matches the type name before the where clause. | ||
// Find a better way to do this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a user does something like @typeparam where
without providing the parameter name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is mapped to a generic type parameter with name where and the razor compiler produces an error (which is expected).
src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs
Show resolved
Hide resolved
src/Razor/Microsoft.AspNetCore.Razor.Language/test/Legacy/RazorDirectivesTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't have much context in this area, but LGTM. Just some minor nits;
src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentTypeParamDirective.cs
Show resolved
Hide resolved
...zor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs
Show resolved
Hide resolved
...zor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs
Outdated
Show resolved
Hide resolved
🆙 📅 |
@javiercn This looks awesome! |
This is a work in progress.
However I appreciate feedback from the appropriate folks.
Also note that I haven't tested this E2E nor done any polishing.
I'm looking to check if the approach is sound and what might be missing before this is "complete"
Fixes #8433