diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs index a50302a5802..2d149e5702f 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs @@ -1,6 +1,7 @@ // 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 Microsoft.AspNetCore.Razor.Language.Intermediate; using Xunit; using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert; @@ -76,4 +77,47 @@ public void Execute_WrapsStatementInSectionNode() Assert.Equal("Header", section.SectionName); Children(section, c => Html("

Hello World

", c)); } + + [Fact] + public void SectionDirective_IsRecognizedInLegacyFiles() + { + // Arrange + var content = "@section Header {

Hello World

}"; + var source = TestRazorSourceDocument.Create(content, filePath: "test.cshtml", relativePath: "test.cshtml"); + + // Act + var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Legacy); + ProjectEngine.Engine.Process(codeDocument); + + // Assert + var syntaxTree = codeDocument.GetSyntaxTree(); + Assert.NotNull(syntaxTree); + + // The section directive should be recognized without errors + var diagnostics = syntaxTree.Diagnostics; + Assert.Empty(diagnostics); + } + + [Fact] + public void SectionDirective_IsNotRecognizedInComponentFiles() + { + // Arrange + var content = "@section Header {

Hello World

}"; + var source = TestRazorSourceDocument.Create(content, filePath: "test.razor", relativePath: "test.razor"); + + // Act + var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component); + ProjectEngine.Engine.Process(codeDocument); + + // Assert + var syntaxTree = codeDocument.GetSyntaxTree(); + Assert.NotNull(syntaxTree); + + // The section directive should NOT be recognized in component files. + // Verify that no RazorDirective node for 'section' exists in the syntax tree + var directiveNodes = syntaxTree.Root.DescendantNodes() + .OfType(); + + Assert.DoesNotContain(directiveNodes, d => d.DirectiveDescriptor?.Directive == "section"); + } } 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 3f328189d33..577bb12c14c 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -12968,5 +12968,21 @@ @inject float Value5 CompileToAssembly(generated); } + [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11273")] + public void SectionDirective_NotAllowed() + { + // Verify that @section is not recognized in components and produces appropriate code-gen + // Act + var generated = CompileToCSharp(""" + @{ var section = "Section"; } + @section One {

Content

} + """); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + #endregion } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs index be35c04c2d3..8f1345d3b75 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs @@ -300,5 +300,4 @@ public void _WithDoubleTransition2() { ParseDocumentTest("@section s {}", [SectionDirective.Directive]); } - } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs new file mode 100644 index 00000000000..04a0de9e6dc --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs @@ -0,0 +1,45 @@ +// +#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 : global::Microsoft.AspNetCore.Components.ComponentBase + #nullable disable + { + #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) + { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + var section = "Section"; + +#line default +#line hidden +#nullable disable +#nullable restore +#line 2 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = section; + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt new file mode 100644 index 00000000000..2546d98f9e3 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt @@ -0,0 +1,27 @@ +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 + CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section"; + CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section + HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One { + MarkupElement - (46:1,15 [14] x:\dir\subdir\Test\TestComponent.cshtml) - p + HtmlContent - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Content + HtmlContent - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt new file mode 100644 index 00000000000..30f6624dbad --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt @@ -0,0 +1,10 @@ +Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) +| var section = "Section"; | +Generated Location: (987:28,2 [26] ) +| var section = "Section"; | + +Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) +|section| +Generated Location: (1141:35,6 [7] ) +|section| + diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs new file mode 100644 index 00000000000..1ee576f73ae --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs @@ -0,0 +1,42 @@ +// +#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 : global::Microsoft.AspNetCore.Components.ComponentBase + #nullable disable + { + #pragma warning disable 1998 + protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + { +#nullable restore +#line (1,3)-(1,29) "x:\dir\subdir\Test\TestComponent.cshtml" + var section = "Section"; + +#line default +#line hidden +#nullable disable + +#nullable restore +#line (2,2)-(2,9) 24 "x:\dir\subdir\Test\TestComponent.cshtml" +__builder.AddContent(0, section + +#line default +#line hidden +#nullable disable + ); + __builder.AddContent(1, " One { "); + __builder.AddMarkupContent(2, "

Content

}"); + } + #pragma warning restore 1998 + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt new file mode 100644 index 00000000000..e964c486204 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt @@ -0,0 +1,16 @@ +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 - + MethodDeclaration - - protected override - void - BuildRenderTree + CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section"; + CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section + HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One { + MarkupBlock - -

Content

} diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt new file mode 100644 index 00000000000..3f1bbf943f0 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt @@ -0,0 +1,10 @@ +Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) +| var section = "Section"; | +Generated Location: (735:21,0 [26] ) +| var section = "Section"; | + +Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) +|section| +Generated Location: (922:29,24 [7] ) +|section| + diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs index 8a078b433bc..4636c8b2d55 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs @@ -26,7 +26,7 @@ public static void Register(RazorProjectEngineBuilder builder) throw new ArgumentNullException(nameof(builder)); } - builder.AddDirective(Directive, RazorFileKind.Legacy, RazorFileKind.Component); + builder.AddDirective(Directive, RazorFileKind.Legacy); builder.Features.Add(new SectionDirectivePass()); builder.AddTargetExtension(new SectionTargetExtension()); }