-
Notifications
You must be signed in to change notification settings - Fork 222
Fix @section directive incorrectly allowed in .razor files #12478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(" <p>Hello World</p> ", c)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SectionDirective_IsRecognizedInLegacyFiles() | ||
| { | ||
| // Arrange | ||
| var content = "@section Header { <p>Hello World</p> }"; | ||
| 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 { <p>Hello World</p> }"; | ||
| 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<Microsoft.AspNetCore.Razor.Language.Syntax.RazorDirectiveSyntax>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we similarly verify that such node exists in the test above? |
||
|
|
||
| Assert.DoesNotContain(directiveNodes, d => d.DirectiveDescriptor?.Directive == "section"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { <p>Content</p> } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider also verifying what happens if |
||
| """); | ||
|
|
||
| // Assert | ||
| AssertDocumentNodeMatchesBaseline(generated.CodeDocument); | ||
| AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); | ||
| CompileToAssembly(generated); | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // <auto-generated/> | ||
| #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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 - } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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| | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // <auto-generated/> | ||
| #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, "<p>Content</p> }"); | ||
| } | ||
| #pragma warning restore 1998 | ||
| } | ||
| } | ||
| #pragma warning restore 1591 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 - - <p>Content</p> } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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| | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding WorkItem attributes