From 956606ee5d3270c364e9fbaf20e94c488c5156ce Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Fri, 19 Apr 2019 10:06:13 -0700 Subject: [PATCH] Fix TypeInference code gen for non-generic parameter (dotnet/aspnetcore-tooling#433) \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/4233bc593c9b0685deb591f314969482159b141e --- .../src/Components/ComponentNodeWriter.cs | 15 +++- .../ComponentCodeGenerationTestBase.cs | 41 ++++++++++ .../TestComponent.codegen.cs | 80 +++++++++++++++++++ .../TestComponent.ir.txt | 34 ++++++++ .../TestComponent.mappings.txt | 24 ++++++ .../TestComponent.codegen.cs | 44 ++++++++++ .../TestComponent.ir.txt | 21 +++++ .../TestComponent.mappings.txt | 9 +++ 8 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentNodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentNodeWriter.cs index 0de9d18a89..0629f7dc69 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentNodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentNodeWriter.cs @@ -182,16 +182,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Components var p = new List<(string seqName, string typeName, string parameterName)>(); foreach (var attribute in node.Component.Attributes) { - p.Add(($"__seq{p.Count}", attribute.TypeName, $"__arg{p.Count}")); + var typeName = attribute.TypeName; + if (attribute.BoundAttribute != null && !attribute.BoundAttribute.IsGenericTypedProperty()) + { + typeName = "global::" + typeName; + } + p.Add(($"__seq{p.Count}", typeName, $"__arg{p.Count}")); } foreach (var childContent in node.Component.ChildContents) { - p.Add(($"__seq{p.Count}", childContent.TypeName, $"__arg{p.Count}")); + var typeName = childContent.TypeName; + if (childContent.BoundAttribute != null && !childContent.BoundAttribute.IsGenericTypedProperty()) + { + typeName = "global::" + typeName; + } + p.Add(($"__seq{p.Count}", typeName, $"__arg{p.Count}")); } foreach (var capture in node.Component.Captures) { + // The capture type name should already contain the global:: prefix. p.Add(($"__seq{p.Count}", capture.TypeName, $"__arg{p.Count}")); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 41e534523a..43381c1a29 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -2788,6 +2788,47 @@ namespace Test CompileToAssembly(generated); } + [Fact] + public void GenericComponent_NonGenericParameter_TypeInference() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(@" +using Microsoft.AspNetCore.Components; +using Test.Shared; + +namespace Test +{ + public class MyComponent : ComponentBase + { + [Parameter] TItem Item { get; set; } + [Parameter] MyClass Foo { get; set; } + } +} + +namespace Test.Shared +{ + public class MyClass + { + } +} +")); + + // Act + var generated = CompileToCSharp(@" +@using Test.Shared + + +@functions { + MyClass Hello = new MyClass(); +} +"); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + #endregion #region Ref diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs new file mode 100644 index 0000000000..467b5e6fc4 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs @@ -0,0 +1,80 @@ +// +#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\TestComponent.cshtml" +using Test.Shared; + +#line default +#line hidden +#nullable disable + public class TestComponent : 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.RenderTree.RenderTreeBuilder builder) + { + __Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(builder, -1, -1, +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + 3 + +#line default +#line hidden +#nullable disable + , -1, +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + Hello + +#line default +#line hidden +#nullable disable + ); +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = typeof(MyComponent<>); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 +#nullable restore +#line 4 "x:\dir\subdir\Test\TestComponent.cshtml" + + MyClass Hello = new MyClass(); + +#line default +#line hidden +#nullable disable + } +} +namespace __Blazor.Test.TestComponent +{ + #line hidden + internal static class TypeInference + { + public static void CreateMyComponent_0(global::Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int seq, int __seq0, TItem __arg0, int __seq1, global::Test.Shared.MyClass __arg1) + { + builder.OpenComponent>(seq); + builder.AddAttribute(__seq0, "Item", __arg0); + builder.AddAttribute(__seq1, "Foo", __arg1); + builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt new file mode 100644 index 0000000000..36a1b2e204 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt @@ -0,0 +1,34 @@ +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 - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + UsingDirective - (1:0,1 [17] x:\dir\subdir\Test\TestComponent.cshtml) - Test.Shared + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + DesignTimeDirective - + 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 - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + Component - (20:1,0 [37] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentAttribute - (39:1,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + IntermediateToken - (39:1,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 + ComponentAttribute - (47:1,27 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Foo - AttributeStructure.DoubleQuotes + CSharpExpression - (48:1,28 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (48:1,28 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Hello + HtmlContent - (57:1,37 [4] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (57:1,37 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n + HtmlContent - (112:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (112:5,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + CSharpCode - (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyClass Hello = new MyClass();\n + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt new file mode 100644 index 0000000000..ccc603195e --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt @@ -0,0 +1,24 @@ +Source Location: (1:0,1 [17] x:\dir\subdir\Test\TestComponent.cshtml) +|using Test.Shared| +Generated Location: (320:12,0 [17] ) +|using Test.Shared| + +Source Location: (39:1,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) +|3| +Generated Location: (1095:32,19 [1] ) +|3| + +Source Location: (48:1,28 [5] x:\dir\subdir\Test\TestComponent.cshtml) +|Hello| +Generated Location: (1266:40,28 [5] ) +|Hello| + +Source Location: (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) +| + MyClass Hello = new MyClass(); +| +Generated Location: (1620:57,12 [38] ) +| + MyClass Hello = new MyClass(); +| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs new file mode 100644 index 0000000000..4595839aa7 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.codegen.cs @@ -0,0 +1,44 @@ +// +#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; + using Test.Shared; + public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder) + { + __Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(builder, 0, 1, 3, 2, Hello); + } + #pragma warning restore 1998 +#nullable restore +#line 4 "x:\dir\subdir\Test\TestComponent.cshtml" + + MyClass Hello = new MyClass(); + +#line default +#line hidden +#nullable disable + } +} +namespace __Blazor.Test.TestComponent +{ + #line hidden + internal static class TypeInference + { + public static void CreateMyComponent_0(global::Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int seq, int __seq0, TItem __arg0, int __seq1, global::Test.Shared.MyClass __arg1) + { + builder.OpenComponent>(seq); + builder.AddAttribute(__seq0, "Item", __arg0); + builder.AddAttribute(__seq1, "Foo", __arg1); + builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt new file mode 100644 index 0000000000..a827ee32c7 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.ir.txt @@ -0,0 +1,21 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + UsingDirective - (1:0,1 [19] x:\dir\subdir\Test\TestComponent.cshtml) - Test.Shared + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (20:1,0 [37] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentAttribute - (39:1,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + IntermediateToken - (39:1,19 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 3 + ComponentAttribute - (47:1,27 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Foo - AttributeStructure.DoubleQuotes + CSharpExpression - (48:1,28 [5] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (48:1,28 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Hello + CSharpCode - (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n MyClass Hello = new MyClass();\n + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt new file mode 100644 index 0000000000..31ad391b84 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/GenericComponent_NonGenericParameter_TypeInference/TestComponent.mappings.txt @@ -0,0 +1,9 @@ +Source Location: (73:3,12 [38] x:\dir\subdir\Test\TestComponent.cshtml) +| + MyClass Hello = new MyClass(); +| +Generated Location: (764:21,12 [38] ) +| + MyClass Hello = new MyClass(); +| +