diff --git a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ModelDirectiveTest.cs b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ModelDirectiveTest.cs index 8769138a008..20d023d87d6 100644 --- a/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ModelDirectiveTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ModelDirectiveTest.cs @@ -249,6 +249,77 @@ @model SomeType Assert.Equal($"TModel = global::System.Object", usingNode.Content); } + [Fact] + public void ModelDirective_Uses_IncompleteGenericType() + { + // Arrange + var codeDocument = CreateDocument(@" +@model Type1< +"); + var engine = CreateRuntimeEngine(); + var irDocument = CreateIRDocument(engine, codeDocument); + + // Act + var result = ModelDirective.GetModelType(irDocument); + + // Assert + Assert.Equal(""" + Type1< + + """, result); + } + + [Fact] + public void ModelDirective_Uses_IncompleteGenericType2() + { + // Arrange + var codeDocument = CreateDocument(@" +@model Type1 +@model /*comment*/ Type1 +"); + + var engine = CreateRuntimeEngine(); + var pass = new ModelDirective.Pass() + { + Engine = engine, + }; + + var irDocument = CreateIRDocument(engine, codeDocument); + + // Act + pass.Execute(codeDocument, irDocument); + + // Assert + var @class = FindClassNode(irDocument); + var baseType = @class.BaseType; + + Assert.Equal("BaseType", baseType.BaseType.Content); + Assert.NotNull(baseType.BaseType.Source); + + Assert.Equal("Type1", baseType.ModelType.Content); + Assert.NotNull(baseType.ModelType.Source); + } + private RazorCodeDocument CreateDocument(string content) { var source = RazorSourceDocument.Create(content, "test.cshtml"); 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/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.codegen.cs index c9c21931222..780b0074b74 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.codegen.cs @@ -18,7 +18,7 @@ namespace AspNetCoreGeneratedDocument [global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("Identifier", "/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml")] [global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute] #nullable restore - internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage + internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives #nullable disable { #pragma warning disable 1998 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.cs-diagnostics.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.cs-diagnostics.txt new file mode 100644 index 00000000000..0338e65f2e4 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.cs-diagnostics.txt @@ -0,0 +1,30 @@ +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(25,67): error CS0115: 'TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives.ExecuteAsync()': no suitable method found to override +// public async override global::System.Threading.Tasks.Task ExecuteAsync() +Diagnostic(ErrorCode.ERR_OverrideNotExpected, "ExecuteAsync").WithArguments("AspNetCoreGeneratedDocument.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives.ExecuteAsync()").WithLocation(25, 67), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(27,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(27, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(28,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(28, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(29,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(29, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(30,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(30, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(31,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(31, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(32,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(32, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(33,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(33, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(34,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("\r\n\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(34, 13), +// TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(35,13): error CS0103: The name 'WriteLiteral' does not exist in the current context +// WriteLiteral("{\r\n"); +Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLiteral").WithArguments("WriteLiteral").WithLocation(35, 13) \ No newline at end of file diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.ir.txt index 7274c0398a5..1e07ddafaa0 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.ir.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_Runtime.ir.txt @@ -11,7 +11,7 @@ RazorSourceChecksumAttribute - RazorCompiledItemMetadataAttribute - CreateNewOnMetadataUpdateAttribute - - ClassDeclaration - - internal sealed - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives - global::Microsoft.AspNetCore.Mvc.Razor.RazorPage - + ClassDeclaration - - internal sealed - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives - - MethodDeclaration - - public async override - global::System.Threading.Tasks.Task - ExecuteAsync HtmlContent - (85:1,0 [2] IncompleteDirectives.cshtml) LazyIntermediateToken - (85:1,0 [2] IncompleteDirectives.cshtml) - Html - \n 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