Skip to content
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

Dig through nested csharp expressions for design time generation #8777

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -717,16 +717,25 @@ private void WritePropertyAccess(CodeRenderingContext context, ComponentAttribut
context.CodeWriter.Write(", ");
}

foreach (var token in typeArgumentNode.Children.OfType<IntermediateToken>())
writeTypeArgument(typeArgumentNode.Children);

void writeTypeArgument(IntermediateNodeCollection typeArgumentComponents)
{
// As per WriteComponentTypeArgument, we expect every token to be C#, but check just in case
if (token.IsCSharp)
{
context.CodeWriter.Write(token.Content);
}
else
foreach (var typeArgumentNodeComponent in typeArgumentComponents)
{
Debug.Fail($"Unexpected non-C# content in a generic type parameter: '{token.Content}'");
switch (typeArgumentNodeComponent)
{
case IntermediateToken { IsCSharp: true } token:
context.CodeWriter.Write(token.Content);
break;
case CSharpExpressionIntermediateNode cSharpExpression:
writeTypeArgument(cSharpExpression.Children);
break;
default:
// As per WriteComponentTypeArgument, we expect every token to be C#, but check just in case
Debug.Fail($"Unexpected non-C# content in a generic type parameter: '{typeArgumentNodeComponent}'");
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5886,6 +5886,27 @@ public class MyComponent<TItem> : ComponentBase
CompileToAssembly(generated);
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/8467")]
public void ChildComponent_AtSpecifiedInRazorFileForTypeParameter()
{
AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components;
namespace Test
{
public class C<T> : ComponentBase
{
[Parameter] public int Item { get; set; }
}
}
"""));

var generated = CompileToCSharp("""<C T="@string" Item="1" />""");

AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void GenericComponent_NonPrimitiveType()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o = typeof(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
string

#line default
#line hidden
#nullable disable
);
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
1

#line default
#line hidden
#nullable disable
);
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#pragma warning disable BL0005
((global::Test.C<string>)default).
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Item

#line default
#line hidden
#nullable disable
= default;
#pragma warning restore BL0005
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.C<>);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [20] ) - global::System
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [25] ) - global::System.Linq
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [26] x:\dir\subdir\Test\TestComponent.cshtml) - C
ComponentTypeArgument - (6:0,6 [7] x:\dir\subdir\Test\TestComponent.cshtml) - T
CSharpExpression - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - string
ComponentAttribute - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - Item - AttributeStructure.DoubleQuotes
LazyIntermediateToken - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Source Location: (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml)
|string|
Generated Location: (935:25,7 [6] )
|string|

Source Location: (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|1|
Generated Location: (1224:34,21 [1] )
|1|

Source Location: (15:0,15 [4] x:\dir\subdir\Test\TestComponent.cshtml)
|Item|
Generated Location: (1633:47,15 [4] )
|Item|

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Test.C<string>>(0);
__builder.AddComponentParameter(1, "Item", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
1

#line default
#line hidden
#nullable disable
));
__builder.CloseComponent();
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [22] ) - global::System
UsingDirective - (26:2,1 [42] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [27] ) - global::System.Linq
UsingDirective - (97:4,1 [38] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [47] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [26] x:\dir\subdir\Test\TestComponent.cshtml) - C
ComponentTypeArgument - (6:0,6 [7] x:\dir\subdir\Test\TestComponent.cshtml) - T
CSharpExpression - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - string
ComponentAttribute - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - Item - AttributeStructure.DoubleQuotes
LazyIntermediateToken - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1