Skip to content

Commit b8b69d1

Browse files
chsienkijjonescz
andauthored
[FUSE] Layout mapping (#11567)
* Add layout test * Emit source location for runtime * Update tests Co-authored-by: Jan Jones <jan.jones.cz@gmail.com> --------- Co-authored-by: Jan Jones <jan.jones.cz@gmail.com>
1 parent 1c75d3c commit b8b69d1

File tree

12 files changed

+160
-10
lines changed

12 files changed

+160
-10
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,20 @@ public void ExplicitExpression_Whitespace()
23602360
CompileToAssembly(generated);
23612361
}
23622362

2363+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11551")]
2364+
public void LayoutDirective()
2365+
{
2366+
// Act
2367+
var generated = CompileToCSharp("""
2368+
@layout System.Object
2369+
""");
2370+
2371+
// Assert
2372+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
2373+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
2374+
CompileToAssembly(generated);
2375+
}
2376+
23632377
#endregion
23642378

23652379
#region Bind
@@ -10568,7 +10582,7 @@ @layout MainLayout
1056810582
Diagnostic(ErrorCode.ERR_NameNotInContext, "Foo").WithArguments("Foo").WithLocation(5, 2),
1056910583
// (33,13): error CS0103: The name '__builder' does not exist in the current context
1057010584
// __builder.AddContent(0,
10571-
Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(33, 13)]);
10585+
Diagnostic(ErrorCode.ERR_NameNotInContext, "__builder").WithArguments("__builder").WithLocation(41, 13)]);
1057210586
}
1057310587

1057410588
[IntegrationTestFact]

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ComponentImports/_Imports.ir.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
UsingDirective - (1:0,1 [17] x:\dir\subdir\Test\_Imports.razor) - System.Text
99
UsingDirective - (21:1,1 [23] x:\dir\subdir\Test\_Imports.razor) - System.Reflection
1010
CSharpCode -
11-
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(MainLayout))]
11+
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
12+
IntermediateToken - - CSharp - MainLayout
13+
IntermediateToken - - CSharp - ))]
1214
ClassDeclaration - - public partial - _Imports - object -
1315
DesignTimeDirective -
1416
DirectiveToken - (56:3,8 [10] x:\dir\subdir\Test\_Imports.razor) - MainLayout
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+
[global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(System.Object))]
14+
#nullable restore
15+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
16+
#nullable disable
17+
{
18+
#pragma warning disable 219
19+
private void __RazorDirectiveTokenHelpers__() {
20+
((global::System.Action)(() => {
21+
#nullable restore
22+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
23+
System.Object __typeHelper = default!;
24+
25+
#line default
26+
#line hidden
27+
#nullable disable
28+
}
29+
))();
30+
}
31+
#pragma warning restore 219
32+
#pragma warning disable 0414
33+
private static object __o = null;
34+
#pragma warning restore 0414
35+
#pragma warning disable 1998
36+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
37+
{
38+
}
39+
#pragma warning restore 1998
40+
}
41+
}
42+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
CSharpCode -
9+
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
10+
IntermediateToken - - CSharp - System.Object
11+
IntermediateToken - - CSharp - ))]
12+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
13+
DesignTimeDirective -
14+
DirectiveToken - (8:0,8 [13] x:\dir\subdir\Test\TestComponent.cshtml) - System.Object
15+
CSharpCode -
16+
IntermediateToken - - CSharp - #pragma warning disable 0414
17+
CSharpCode -
18+
IntermediateToken - - CSharp - private static object __o = null;
19+
CSharpCode -
20+
IntermediateToken - - CSharp - #pragma warning restore 0414
21+
MethodDeclaration - - protected override - void - BuildRenderTree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Source Location: (8:0,8 [13] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|System.Object|
3+
Generated Location: (769:22,0 [13] )
4+
|System.Object|
5+

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ComponentImports/_Imports.codegen.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ namespace Test
2222
;
2323
#line default
2424
#line hidden
25-
[global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(MainLayout))]
25+
[global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
26+
#nullable restore
27+
#line (4,9)-(4,19) "x:\dir\subdir\Test\_Imports.razor"
28+
MainLayout
29+
30+
#line default
31+
#line hidden
32+
#nullable disable
33+
))]
2634
#nullable restore
2735
public partial class _Imports : object
2836
#nullable disable

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ComponentImports/_Imports.ir.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
UsingDirective - (1:0,1 [17] x:\dir\subdir\Test\_Imports.razor) - System.Text
99
UsingDirective - (21:1,1 [23] x:\dir\subdir\Test\_Imports.razor) - System.Reflection
1010
CSharpCode -
11-
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(MainLayout))]
11+
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
12+
IntermediateToken - (56:3,8 [10] x:\dir\subdir\Test\_Imports.razor) - CSharp - MainLayout
13+
IntermediateToken - - CSharp - ))]
1214
ClassDeclaration - - public partial - _Imports - object -
1315
MethodDeclaration - - protected - void - Execute
1416
CSharpExpression - (69:4,1 [3] x:\dir\subdir\Test\_Imports.razor)

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ComponentImports/_Imports.mappings.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ Source Location: (21:1,1 [23] x:\dir\subdir\Test\_Imports.razor)
88
Generated Location: (488:18,0 [23] )
99
|using System.Reflection|
1010

11+
Source Location: (56:3,8 [10] x:\dir\subdir\Test\_Imports.razor)
12+
|MainLayout|
13+
Generated Location: (723:27,0 [10] )
14+
|MainLayout|
15+
1116
Source Location: (69:4,1 [3] x:\dir\subdir\Test\_Imports.razor)
1217
|Foo|
13-
Generated Location: (953:35,0 [3] )
18+
Generated Location: (1086:43,0 [3] )
1419
|Foo|
1520

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
[global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
14+
#nullable restore
15+
#line (1,9)-(1,22) "x:\dir\subdir\Test\TestComponent.cshtml"
16+
System.Object
17+
18+
#line default
19+
#line hidden
20+
#nullable disable
21+
))]
22+
#nullable restore
23+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
24+
#nullable disable
25+
{
26+
#pragma warning disable 1998
27+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
28+
{
29+
}
30+
#pragma warning restore 1998
31+
}
32+
}
33+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
CSharpCode -
9+
IntermediateToken - - CSharp - [global::Microsoft.AspNetCore.Components.LayoutAttribute(typeof(
10+
IntermediateToken - (8:0,8 [13] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - System.Object
11+
IntermediateToken - - CSharp - ))]
12+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
13+
MethodDeclaration - - protected override - void - BuildRenderTree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Source Location: (8:0,8 [13] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|System.Object|
3+
Generated Location: (479:15,0 [13] )
4+
|System.Object|
5+

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Components/ComponentLayoutDirectivePass.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
3333
}
3434

3535
var attributeNode = new CSharpCodeIntermediateNode();
36-
attributeNode.Children.Add(new IntermediateToken()
37-
{
38-
Kind = TokenKind.CSharp,
39-
Content = $"[global::{ComponentsApi.LayoutAttribute.FullTypeName}(typeof({token.Content}))]",
40-
});
36+
attributeNode.Children.AddRange([
37+
IntermediateToken.CreateCSharpToken($"[global::{ComponentsApi.LayoutAttribute.FullTypeName}(typeof("),
38+
IntermediateToken.CreateCSharpToken(token.Content, documentNode.Options.DesignTime ? null : token.Source),
39+
IntermediateToken.CreateCSharpToken("))]")
40+
]);
4141

4242
// Insert the new attribute on top of the class
4343
for (var i = 0; i < @namespace.Children.Count; i++)

0 commit comments

Comments
 (0)