From 42242e9786b445fae90fa8c41c846f62faff2cb1 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Thu, 17 Oct 2024 11:43:35 -0700 Subject: [PATCH 1/5] Acccept malformed inherits directives for intellisense. --- .../ComponentCodeGenerationTestBase.cs | 73 +++++++++++++++++++ .../TestComponent.codegen.cs | 32 ++++++++ .../TestComponent.ir.txt | 11 +++ .../TestComponent.mappings.txt | 5 ++ .../TestComponent.codegen.cs | 24 ++++++ .../TestComponent.ir.txt | 11 +++ .../Extensions/InheritsDirectivePass.cs | 4 +- .../DocumentIntermediateNodeExtensions.cs | 20 ++++- 8 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.codegen.cs create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.ir.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.mappings.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteType/TestComponent.codegen.cs create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteType/TestComponent.ir.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index b64479e99a5..f635af541f8 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -5649,6 +5649,79 @@ @inherits BaseComponent CompileToAssembly(generated); } + [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10963")] + public void InheritsDirective_IncompleteType() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(""" + namespace Test; + + public abstract class BaseComponent : Microsoft.AspNetCore.Components.ComponentBase + { + } + """)); + + // Act + var generated = CompileToCSharp(""" + @inherits + """, + // (22,33): error CS0115: 'TestComponent.BuildRenderTree(RenderTreeBuilder)': no suitable method found to override + // protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + Diagnostic(ErrorCode.ERR_OverrideNotExpected, "BuildRenderTree").WithArguments("Test.TestComponent.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder)").WithLocation(22, 33) + ); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated, + DesignTime + ? [] + : [ + // (18,33): error CS0115: 'TestComponent.BuildRenderTree(RenderTreeBuilder)': no suitable method found to override + // protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + Diagnostic(ErrorCode.ERR_OverrideNotExpected, "BuildRenderTree").WithArguments("Test.TestComponent.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder)").WithLocation(18, 33) + ] + ); + } + + [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10963")] + public void InheritsDirective_IncompleteGenericType() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(""" + namespace Test; + + public abstract class BaseComponent : Microsoft.AspNetCore.Components.ComponentBase + { + } + """)); + + // Act + var generated = CompileToCSharp(""" + @inherits BaseComponent' expected + // BaseComponent").WithLocation(1, 31) + ); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated, + DesignTime?[ + // x:\dir\subdir\Test\TestComponent.cshtml(1,22): error CS1003: Syntax error, '>' expected + // BaseComponent").WithLocation(1, 22) + ] : [ + // x:\dir\subdir\Test\TestComponent.cshtml(1,31): error CS1003: Syntax error, '>' expected + // BaseComponent").WithLocation(1, 31) + ]); + } + [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/7169")] public void InheritsDirective_NullableReferenceType() { diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.codegen.cs new file mode 100644 index 00000000000..b9ef5a543c4 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/InheritsDirective_IncompleteGenericType/TestComponent.codegen.cs @@ -0,0 +1,32 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line default + using global::System; + using global::System.Collections.Generic; + using global::System.Linq; + using global::System.Threading.Tasks; + using global::Microsoft.AspNetCore.Components; + #line default + #line hidden + #nullable restore + public partial class TestComponent : +#nullable restore +#line (1,11)-(1,31) "x:\dir\subdir\Test\TestComponent.cshtml" +BaseComponent