Skip to content

Commit 985a0a4

Browse files
Copilotdavidwengier
andcommitted
Add compiler integration test for @section directive in components
Added SectionDirective_NotAllowed integration test to verify code generation when @section is used in .razor files. The test confirms that @section is no longer recognized as a directive and is instead treated as regular content, preventing compilation errors. Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent c8a4e63 commit 985a0a4

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12968,5 +12968,21 @@ @inject float Value5
1296812968
CompileToAssembly(generated);
1296912969
}
1297012970

12971+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11273")]
12972+
public void SectionDirective_NotAllowed()
12973+
{
12974+
// Verify that @section is not recognized in components and produces appropriate code-gen
12975+
// Act
12976+
var generated = CompileToCSharp("""
12977+
@{ var section = "Section"; }
12978+
@section One { <p>Content</p> }
12979+
""");
12980+
12981+
// Assert
12982+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
12983+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
12984+
CompileToAssembly(generated);
12985+
}
12986+
1297112987
#endregion
1297212988
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line default
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
#line default
12+
#line hidden
13+
#nullable restore
14+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
15+
#nullable disable
16+
{
17+
#pragma warning disable 219
18+
private void __RazorDirectiveTokenHelpers__() {
19+
}
20+
#pragma warning restore 219
21+
#pragma warning disable 0414
22+
private static object __o = null;
23+
#pragma warning restore 0414
24+
#pragma warning disable 1998
25+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
26+
{
27+
#nullable restore
28+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
29+
var section = "Section";
30+
31+
#line default
32+
#line hidden
33+
#nullable disable
34+
#nullable restore
35+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
36+
__o = section;
37+
38+
#line default
39+
#line hidden
40+
#nullable disable
41+
}
42+
#pragma warning restore 1998
43+
}
44+
}
45+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [20] ) - global::System
4+
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [25] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
9+
DesignTimeDirective -
10+
CSharpCode -
11+
IntermediateToken - - CSharp - #pragma warning disable 0414
12+
CSharpCode -
13+
IntermediateToken - - CSharp - private static object __o = null;
14+
CSharpCode -
15+
IntermediateToken - - CSharp - #pragma warning restore 0414
16+
MethodDeclaration - - protected override - void - BuildRenderTree
17+
CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
18+
LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section";
19+
CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
20+
LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section
21+
HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml)
22+
LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One {
23+
MarkupElement - (46:1,15 [14] x:\dir\subdir\Test\TestComponent.cshtml) - p
24+
HtmlContent - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml)
25+
LazyIntermediateToken - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Content
26+
HtmlContent - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml)
27+
LazyIntermediateToken - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
2+
| var section = "Section"; |
3+
Generated Location: (987:28,2 [26] )
4+
| var section = "Section"; |
5+
6+
Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
7+
|section|
8+
Generated Location: (1141:35,6 [7] )
9+
|section|
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line default
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
#line default
12+
#line hidden
13+
#nullable restore
14+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
15+
#nullable disable
16+
{
17+
#pragma warning disable 1998
18+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
19+
{
20+
#nullable restore
21+
#line (1,3)-(1,29) "x:\dir\subdir\Test\TestComponent.cshtml"
22+
var section = "Section";
23+
24+
#line default
25+
#line hidden
26+
#nullable disable
27+
28+
#nullable restore
29+
#line (2,2)-(2,9) 24 "x:\dir\subdir\Test\TestComponent.cshtml"
30+
__builder.AddContent(0, section
31+
32+
#line default
33+
#line hidden
34+
#nullable disable
35+
);
36+
__builder.AddContent(1, " One { ");
37+
__builder.AddMarkupContent(2, "<p>Content</p> }");
38+
}
39+
#pragma warning restore 1998
40+
}
41+
}
42+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [20] ) - global::System
4+
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [25] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
9+
MethodDeclaration - - protected override - void - BuildRenderTree
10+
CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
11+
LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section";
12+
CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
13+
LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section
14+
HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml)
15+
LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One {
16+
MarkupBlock - - <p>Content</p> }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
2+
| var section = "Section"; |
3+
Generated Location: (735:21,0 [26] )
4+
| var section = "Section"; |
5+
6+
Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
7+
|section|
8+
Generated Location: (922:29,24 [7] )
9+
|section|
10+

0 commit comments

Comments
 (0)