From fede53883851a271ac1ed7a8a7520d5186d61c77 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Fri, 3 May 2019 14:02:13 -0700 Subject: [PATCH] Port fixes from master (dotnet/aspnetcore-tooling#562) * Fix invalid cast in non-generic parameterized ChildContent * Fix crash in functions block \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/0acc18b236822510f77f50cea9cb5ae581c36b65 --- .../Components/ComponentGenericTypePass.cs | 3 +- .../src/Extensions/FunctionsDirectivePass.cs | 4 + .../Extensions/FunctionsDirectivePassTest.cs | 4 +- .../ComponentCodeGenerationTestBase.cs | 33 ++++++++ .../TestComponent.codegen.cs | 76 +++++++++++++++++++ .../TestComponent.ir.txt | 29 +++++++ .../TestComponent.mappings.txt | 15 ++++ .../TestComponent.codegen.cs | 66 ++++++++++++++++ .../TestComponent.ir.txt | 22 ++++++ 9 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.mappings.txt create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs create mode 100644 src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentGenericTypePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentGenericTypePass.cs index ee92d484a6..6be200e659 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentGenericTypePass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentGenericTypePass.cs @@ -252,8 +252,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components } else if (childContent.IsParameterized) { - // This is a weakly typed parameterized child content, treat it as RenderFragment - childContent.TypeName = ComponentsApi.RenderFragment.FullTypeName + ""; + // This is a non-generic parameterized child content like RenderFragment, leave it as is. } else { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs index 32f79570cb..a608171881 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs @@ -21,6 +21,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { @class.Children.Add(functions.Node.Children[i]); } + + // We don't want to keep the original directive node around anymore. + // Otherwise this can cause unintended side effects in the subsequent passes. + functions.Remove(); } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs index 712766a5f1..5a0ebdba33 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs @@ -71,9 +71,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions node => CSharpCode(" var value = true; ", node)); var method = @class.Children[0]; - Children( - method, - node => Assert.IsType(node)); + Assert.Empty(method.Children); } private static DocumentIntermediateNode Lower(RazorCodeDocument codeDocument, RazorProjectEngine projectEngine) 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 43381c1a29..3c19003550 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -2611,6 +2611,39 @@ namespace Test CompileToAssembly(generated); } + [Fact] + public void ChildComponent_NonGenericParameterizedChildContent_TypeInference() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(@" +using Microsoft.AspNetCore.Components; + +namespace Test +{ + public class MyComponent : ComponentBase + { + [Parameter] TItem Item { get; set; } + + [Parameter] RenderFragment GenericFragment { get; set; } + + [Parameter] RenderFragment IntFragment { get; set; } + } +} +")); + + // Act + var generated = CompileToCSharp(@" + + @context.ToLower() + @context +"); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + [Fact] public void GenericComponent_WithFullyQualifiedTagName() { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs new file mode 100644 index 0000000000..a6b1234125 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs @@ -0,0 +1,76 @@ +// +#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; + 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 1 "x:\dir\subdir\Test\TestComponent.cshtml" + "hi" + +#line default +#line hidden +#nullable disable + , -1, (context) => (builder2) => { +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + __o = context.ToLower(); + +#line default +#line hidden +#nullable disable + } + , -1, (context) => (builder2) => { +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + __o = context; + +#line default +#line hidden +#nullable disable + } + ); +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = typeof(MyComponent<>); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 + } +} +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::Microsoft.AspNetCore.Components.RenderFragment __arg1, int __seq2, global::Microsoft.AspNetCore.Components.RenderFragment __arg2) + { + builder.OpenComponent>(seq); + builder.AddAttribute(__seq0, "Item", __arg0); + builder.AddAttribute(__seq1, "GenericFragment", __arg1); + builder.AddAttribute(__seq2, "IntFragment", __arg2); + builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt new file mode 100644 index 0000000000..9fc99566f6 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt @@ -0,0 +1,29 @@ +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 + 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 + Component - (0:0,0 [140] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentChildContent - (32:1,2 [53] x:\dir\subdir\Test\TestComponent.cshtml) - GenericFragment - context + CSharpExpression - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context.ToLower() + ComponentChildContent - (89:2,2 [35] x:\dir\subdir\Test\TestComponent.cshtml) - IntFragment - context + CSharpExpression - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context + ComponentAttribute - (19:0,19 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + CSharpExpression - (20:0,20 [6] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "hi" + 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/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.mappings.txt new file mode 100644 index 0000000000..0566a1dd01 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.mappings.txt @@ -0,0 +1,15 @@ +Source Location: (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml) +|"hi"| +Generated Location: (957:25,21 [4] ) +|"hi"| + +Source Location: (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) +|context.ToLower()| +Generated Location: (1151:33,20 [17] ) +|context.ToLower()| + +Source Location: (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) +|context| +Generated Location: (1370:42,16 [7] ) +|context| + diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs new file mode 100644 index 0000000000..3bd44ddf68 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.codegen.cs @@ -0,0 +1,66 @@ +// +#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; + 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, +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + "hi" + +#line default +#line hidden +#nullable disable + , 2, (context) => (builder2) => { + builder2.AddContent(3, +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" + context.ToLower() + +#line default +#line hidden +#nullable disable + ); + } + , 4, (context) => (builder2) => { + builder2.AddContent(5, +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + context + +#line default +#line hidden +#nullable disable + ); + } + ); + } + #pragma warning restore 1998 + } +} +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::Microsoft.AspNetCore.Components.RenderFragment __arg1, int __seq2, global::Microsoft.AspNetCore.Components.RenderFragment __arg2) + { + builder.OpenComponent>(seq); + builder.AddAttribute(__seq0, "Item", __arg0); + builder.AddAttribute(__seq1, "GenericFragment", __arg1); + builder.AddAttribute(__seq2, "IntFragment", __arg2); + builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt new file mode 100644 index 0000000000..62a5b6d11d --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_NonGenericParameterizedChildContent_TypeInference/TestComponent.ir.txt @@ -0,0 +1,22 @@ +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 + ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [140] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ComponentChildContent - (32:1,2 [53] x:\dir\subdir\Test\TestComponent.cshtml) - GenericFragment - context + CSharpExpression - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context.ToLower() + ComponentChildContent - (89:2,2 [35] x:\dir\subdir\Test\TestComponent.cshtml) - IntFragment - context + CSharpExpression - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context + ComponentAttribute - (19:0,19 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes + CSharpExpression - (20:0,20 [6] x:\dir\subdir\Test\TestComponent.cshtml) + IntermediateToken - (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "hi" + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0