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

[release/6.0] Support generic array parameters in components #35980

Merged
merged 1 commit into from
Aug 31, 2021
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 @@ -231,6 +231,65 @@ @typeparam TItem2
CompileToAssembly(generated);
}

[Fact]
public void ComponentWithTypeParameterArray()
{
// Arrange
var classes = @"
public class Tag
{
public string description { get; set; }
}
";

AdditionalSyntaxTrees.Add(Parse(classes));

// Act
var generated = CompileToCSharp(@"
@using Microsoft.AspNetCore.Components;
@typeparam TItem

<h1>Item</h1>

<p>@ChildContent(Items1)</p>

@foreach (var item in Items2)
{
<p>@ChildContent(item)</p>
}

<p>@ChildContent(Items3())</p>

@code {
[Parameter] public TItem[] Items1 { get; set; }
[Parameter] public List<TItem[]> Items2 { get; set; }
[Parameter] public Func<TItem[]> Items3 { get; set; }
[Parameter] public RenderFragment<TItem[]> ChildContent { get; set; }
}");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);

AdditionalSyntaxTrees.Add(Parse(generated.CodeDocument.GetCSharpDocument().GeneratedCode));
var useGenerated = CompileToCSharp("UseTestComponent.cshtml", @"
@using Test
<TestComponent Items1=items1 Items2=items2 Items3=items3>
<p>@context[0].description</p>
</TestComponent>

@code {
static Tag tag = new Tag() { description = ""A description.""};
Tag[] items1 = new [] { tag };
List<Tag[]> items2 = new List<Tag[]>() { new [] { tag } };
Tag[] items3() => new [] { tag };
}");
AssertDocumentNodeMatchesBaseline(useGenerated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(useGenerated.CodeDocument);
CompileToAssembly(useGenerated);
}

[Fact]
public void ComponentWithConstrainedTypeParameters()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
using Microsoft.AspNetCore.Components;

#line default
#line hidden
#nullable disable
public partial class TestComponent<TItem> : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
((System.Action)(() => {
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
global::System.Object TItem = null!;

#line default
#line hidden
#nullable disable
}
))();
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
#nullable restore
#line 6 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = ChildContent(Items1);

#line default
#line hidden
#nullable disable
#nullable restore
#line 8 "x:\dir\subdir\Test\TestComponent.cshtml"
foreach (var item in Items2)
{


#line default
#line hidden
#nullable disable
#nullable restore
#line 10 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = ChildContent(item);

#line default
#line hidden
#nullable disable
#nullable restore
#line 10 "x:\dir\subdir\Test\TestComponent.cshtml"

}

#line default
#line hidden
#nullable disable
#nullable restore
#line 13 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = ChildContent(Items3());

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 15 "x:\dir\subdir\Test\TestComponent.cshtml"

[Parameter] public TItem[] Items1 { get; set; }
[Parameter] public List<TItem[]> Items2 { get; set; }
[Parameter] public Func<TItem[]> Items3 { get; set; }
[Parameter] public RenderFragment<TItem[]> ChildContent { get; set; }

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (1:0,1 [38] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - - TItem
DesignTimeDirective -
DirectiveToken - (52:1,11 [5] x:\dir\subdir\Test\TestComponent.cshtml) - TItem
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
HtmlContent - (39:0,39 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (39:0,39 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
HtmlContent - (59:2,0 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (59:2,0 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
MarkupElement - (61:3,0 [13] x:\dir\subdir\Test\TestComponent.cshtml) - h1
HtmlContent - (65:3,4 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (65:3,4 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Item
HtmlContent - (74:3,13 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (74:3,13 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
MarkupElement - (78:5,0 [28] x:\dir\subdir\Test\TestComponent.cshtml) - p
CSharpExpression - (82:5,4 [20] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (82:5,4 [20] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ChildContent(Items1)
HtmlContent - (106:5,28 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (106:5,28 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (111:7,1 [37] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (111:7,1 [37] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - foreach (var item in Items2)\n{\n
MarkupElement - (148:9,4 [26] x:\dir\subdir\Test\TestComponent.cshtml) - p
CSharpExpression - (152:9,8 [18] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (152:9,8 [18] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ChildContent(item)
CSharpCode - (174:9,30 [3] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (174:9,30 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n}
HtmlContent - (177:10,1 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (177:10,1 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
MarkupElement - (181:12,0 [30] x:\dir\subdir\Test\TestComponent.cshtml) - p
CSharpExpression - (185:12,4 [22] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (185:12,4 [22] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ChildContent(Items3())
HtmlContent - (211:12,30 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (211:12,30 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (222:14,7 [248] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (222:14,7 [248] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter] public TItem[] Items1 { get; set; }\n [Parameter] public List<TItem[]> Items2 { get; set; }\n [Parameter] public Func<TItem[]> Items3 { get; set; }\n [Parameter] public RenderFragment<TItem[]> ChildContent { get; set; }\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Source Location: (1:0,1 [38] x:\dir\subdir\Test\TestComponent.cshtml)
|using Microsoft.AspNetCore.Components;|
Generated Location: (276:11,0 [38] )
|using Microsoft.AspNetCore.Components;|

Source Location: (52:1,11 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|TItem|
Generated Location: (688:23,22 [5] )
|TItem|

Source Location: (82:5,4 [20] x:\dir\subdir\Test\TestComponent.cshtml)
|ChildContent(Items1)|
Generated Location: (1199:40,6 [20] )
|ChildContent(Items1)|

Source Location: (111:7,1 [37] x:\dir\subdir\Test\TestComponent.cshtml)
|foreach (var item in Items2)
{
|
Generated Location: (1343:47,1 [37] )
|foreach (var item in Items2)
{
|

Source Location: (152:9,8 [18] x:\dir\subdir\Test\TestComponent.cshtml)
|ChildContent(item)|
Generated Location: (1511:56,8 [18] )
|ChildContent(item)|

Source Location: (174:9,30 [3] x:\dir\subdir\Test\TestComponent.cshtml)
|
}|
Generated Location: (1683:63,30 [3] )
|
}|

Source Location: (185:12,4 [22] x:\dir\subdir\Test\TestComponent.cshtml)
|ChildContent(Items3())|
Generated Location: (1815:71,6 [22] )
|ChildContent(Items3())|

Source Location: (222:14,7 [248] x:\dir\subdir\Test\TestComponent.cshtml)
|
[Parameter] public TItem[] Items1 { get; set; }
[Parameter] public List<TItem[]> Items2 { get; set; }
[Parameter] public Func<TItem[]> Items3 { get; set; }
[Parameter] public RenderFragment<TItem[]> ChildContent { get; set; }
|
Generated Location: (2017:80,7 [248] )
|
[Parameter] public TItem[] Items1 { get; set; }
[Parameter] public List<TItem[]> Items2 { get; set; }
[Parameter] public Func<TItem[]> Items3 { get; set; }
[Parameter] public RenderFragment<TItem[]> ChildContent { get; set; }
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "x:\dir\subdir\Test\UseTestComponent.cshtml"
using Test;

#line default
#line hidden
#nullable disable
public partial class UseTestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__Blazor.Test.UseTestComponent.TypeInference.CreateTestComponent_0(__builder, -1, -1,
#nullable restore
#line 2 "x:\dir\subdir\Test\UseTestComponent.cshtml"
items1

#line default
#line hidden
#nullable disable
, -1,
#nullable restore
#line 2 "x:\dir\subdir\Test\UseTestComponent.cshtml"
items2

#line default
#line hidden
#nullable disable
, -1,
#nullable restore
#line 2 "x:\dir\subdir\Test\UseTestComponent.cshtml"
items3

#line default
#line hidden
#nullable disable
, -1, (context) => (__builder2) => {
#nullable restore
#line 3 "x:\dir\subdir\Test\UseTestComponent.cshtml"
__o = context[0].description;

#line default
#line hidden
#nullable disable
}
);
#nullable restore
#line 2 "x:\dir\subdir\Test\UseTestComponent.cshtml"
__o = typeof(TestComponent<>);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 6 "x:\dir\subdir\Test\UseTestComponent.cshtml"

static Tag tag = new Tag() { description = "A description."};
Tag[] items1 = new [] { tag };
List<Tag[]> items2 = new List<Tag[]>() { new [] { tag } };
Tag[] items3() => new [] { tag };

#line default
#line hidden
#nullable disable
}
}
namespace __Blazor.Test.UseTestComponent
{
#line hidden
internal static class TypeInference
{
public static void CreateTestComponent_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TItem[] __arg0, int __seq1, global::System.Collections.Generic.List<TItem[]> __arg1, int __seq2, global::System.Func<TItem[]> __arg2, int __seq3, global::Microsoft.AspNetCore.Components.RenderFragment<TItem[]> __arg3)
{
__builder.OpenComponent<global::Test.TestComponent<TItem>>(seq);
__builder.AddAttribute(__seq0, "Items1", __arg0);
__builder.AddAttribute(__seq1, "Items2", __arg1);
__builder.AddAttribute(__seq2, "Items3", __arg2);
__builder.AddAttribute(__seq3, "ChildContent", __arg3);
__builder.CloseComponent();
}
}
}
#pragma warning restore 1591
Loading