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

Fix parsing of type arguments in arrays #9115

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable
Expand Down Expand Up @@ -5397,6 +5397,36 @@ public class MyType
Assert.Collection(result.Diagnostics, d => { Assert.Equal("CS0411", d.Id); });
}

[Fact, WorkItem("https://github.com/dotnet/aspnetcore/issues/48526")]
public void EventCallbackOfT_Array()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components.Forms;

namespace Test;

public class MyComponent<TItem> : InputBase<TItem[]>
{
protected override bool TryParseValueFromString(string value, out TItem[] result, out string validationErrorMessage) => throw null;
}
"""));

// Act
var generated = CompileToCSharp("""
<MyComponent @bind-Value="Selected" />

@code {
string[] Selected { get; set; } = Array.Empty<string>();
}
""");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
333fred marked this conversation as resolved.
Show resolved Hide resolved
}

#endregion

#region Event Handlers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// <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)
{
var __typeInference_CreateMyComponent_0 = global::__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(__builder, -1, -1,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Selected

#line default
#line hidden
#nullable disable
, -1, global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create(this,
global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => Selected = __value, Selected)), -1, () => Selected);
#pragma warning disable BL0005
__typeInference_CreateMyComponent_0.
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Value

#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.MyComponent<>);

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

string[] Selected { get; set; } = Array.Empty<string>();

#line default
#line hidden
#nullable disable
}
}
namespace __Blazor.Test.TestComponent
{
#line hidden
internal static class TypeInference
{
public static global::Test.MyComponent<TItem> CreateMyComponent_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TItem[] __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<TItem[]> __arg1, int __seq2, global::System.Linq.Expressions.Expression<global::System.Func<TItem[]>> __arg2)
{
__builder.OpenComponent<global::Test.MyComponent<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddComponentParameter(__seq2, "ValueExpression", __arg2);
__builder.CloseComponent();
return default;
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 [38] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Selected
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this,
IntermediateToken - - CSharp - __value => Selected = __value
IntermediateToken - - CSharp - , Selected)
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - ValueExpression - ValueExpression - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - () => Selected
HtmlContent - (38:0,38 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (38:0,38 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n string[] Selected { get; set; } = Array.Empty<string>();\n
NamespaceDeclaration - - __Blazor.Test.TestComponent
ClassDeclaration - - internal static - TypeInference - -
ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Source Location: (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml)
|Selected|
Generated Location: (1072:25,26 [8] )
|Selected|

Source Location: (19:0,19 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|Value|
Generated Location: (1593:36,19 [5] )
|Value|

Source Location: (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml)
|
string[] Selected { get; set; } = Array.Empty<string>();
|
Generated Location: (2008:54,7 [64] )
|
string[] Selected { get; set; } = Array.Empty<string>();
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <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)
{
global::__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(__builder, 0, 1,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Selected

#line default
#line hidden
#nullable disable
, 2, global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => Selected = __value, Selected)), 3, () => Selected);
}
#pragma warning restore 1998
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"

string[] Selected { get; set; } = Array.Empty<string>();

#line default
#line hidden
#nullable disable
}
}
namespace __Blazor.Test.TestComponent
{
#line hidden
internal static class TypeInference
{
public static void CreateMyComponent_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TItem[] __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<TItem[]> __arg1, int __seq2, global::System.Linq.Expressions.Expression<global::System.Func<TItem[]>> __arg2)
{
__builder.OpenComponent<global::Test.MyComponent<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddComponentParameter(__seq2, "ValueExpression", __arg2);
__builder.CloseComponent();
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 [38] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Selected
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this,
IntermediateToken - - CSharp - __value => Selected = __value
IntermediateToken - - CSharp - , Selected)
ComponentAttribute - (26:0,26 [8] x:\dir\subdir\Test\TestComponent.cshtml) - ValueExpression - ValueExpression - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - () => Selected
CSharpCode - (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n string[] Selected { get; set; } = Array.Empty<string>();\n
NamespaceDeclaration - - __Blazor.Test.TestComponent
ClassDeclaration - - internal static - TypeInference - -
ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Source Location: (49:2,7 [64] x:\dir\subdir\Test\TestComponent.cshtml)
|
string[] Selected { get; set; } = Array.Empty<string>();
|
Generated Location: (1215:28,7 [64] )
|
string[] Selected { get; set; } = Array.Empty<string>();
|

Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,45 @@ public override IReadOnlyList<string> ParseTypeParameters(string typeName)
}

var parsed = SyntaxFactory.ParseTypeName(typeName);

if (parsed is IdentifierNameSyntax identifier)
{
return Array.Empty<string>();
}
else if (parsed is ArrayTypeSyntax array)

if (TryParseCore(parsed) is { } list)
{
return new[] { array.ElementType.ToString() };
return list;
}
else if (parsed is TupleTypeSyntax tuple)

return parsed.DescendantNodesAndSelf()
.OfType<TypeArgumentListSyntax>()
.SelectMany(arg => arg.Arguments)
.SelectMany(ParseCore).ToArray();

static IReadOnlyList<string> TryParseCore(TypeSyntax parsed)
{
return tuple.Elements.Select(a => a.ToString()).ToList();
if (parsed is ArrayTypeSyntax array)
{
return ParseCore(array.ElementType);
}

if (parsed is TupleTypeSyntax tuple)
{
return tuple.Elements.SelectMany(a => ParseCore(a.Type)).ToList();
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
}
else

static IReadOnlyList<string> ParseCore(TypeSyntax parsed)
{
return parsed.DescendantNodesAndSelf()
.OfType<TypeArgumentListSyntax>()
.SelectMany(arg => arg.Arguments)
.Select(a => a.ToString()).ToList();
if (TryParseCore(parsed) is { } list)
{
return list;
}

return new[] { parsed.ToString() };
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Xunit;

namespace Microsoft.CodeAnalysis.Razor;

public class DefaultTypeNameFeatureTest
{
[Theory]
[InlineData("C", 0)]
[InlineData("T", 0)]
[InlineData("T[]", 1)]
[InlineData("T[][]", 1)]
[InlineData("(T, T)[]", 2)]
[InlineData("(T X, T Y)[]", 2)]
[InlineData("(T[], T)[]", 2)]
[InlineData("(T[] X, T Y)[]", 2)]
[InlineData("C<T>", 1)]
[InlineData("C<T[]>", 1)]
[InlineData("C<T[][]>", 1)]
[InlineData("C<(T, T)[]>", 2)]
[InlineData("C<(T X, T Y)[]>", 2)]
[InlineData("C<(T[], T)[]>", 2)]
[InlineData("C<(T[] X, T Y)[]>", 2)]
public void ParseTypeParameters(string input, int expectedNumberOfTs)
{
// Arrange.
var feature = new DefaultTypeNameFeature();

// Act.
var parsed = feature.ParseTypeParameters(input);

// Assert.
Assert.Equal(Enumerable.Repeat("T", expectedNumberOfTs), parsed);
}
}