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() + .OfTypeContent
} + """); + + // 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 @@ +//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()); }