Skip to content

Commit f239b13

Browse files
authored
Allow components in global namespace (#10086)
* Allow components in global namespace * Update existing tests * Fixup baseline * Test formatting of component in global namespace * Add design-time codegen test * Automatically determine where empty root namespace is allowed * Fixup tooling * Test more formatting in global namespace * Fix global namespace testing * Fix formatting in global namespace * Check global namespace flag instead of empty string
1 parent 8376bdb commit f239b13

File tree

41 files changed

+552
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+552
-85
lines changed

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent_DesignTime.codegen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// <auto-generated/>
22
#pragma warning disable 1591
3-
namespace __GeneratedComponent
3+
namespace TestFiles.IntegrationTests.CodeGenerationIntegrationTest
44
{
55
#line default
66
using global::System;
@@ -11,7 +11,7 @@ namespace __GeneratedComponent
1111
#line default
1212
#line hidden
1313
#nullable restore
14-
public partial class AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
14+
public partial class BasicComponent : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
1515
#nullable disable
1616
{
1717
#pragma warning disable 219

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent_DesignTime.ir.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Document -
2-
NamespaceDeclaration - - __GeneratedComponent
2+
NamespaceDeclaration - - TestFiles.IntegrationTests.CodeGenerationIntegrationTest
33
UsingDirective - (1:0,1 [20] ) - global::System
44
UsingDirective - (24:1,1 [40] ) - global::System.Collections.Generic
55
UsingDirective - (67:2,1 [25] ) - global::System.Linq
66
UsingDirective - (95:3,1 [36] ) - global::System.Threading.Tasks
77
UsingDirective - (134:4,1 [45] ) - global::Microsoft.AspNetCore.Components
8-
ClassDeclaration - - public partial - AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
8+
ClassDeclaration - - public partial - BasicComponent - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
99
DesignTimeDirective -
1010
DirectiveToken - (12:0,12 [11] BasicComponent.cshtml) - IDisposable
1111
CSharpCode -

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent_DesignTime.mappings.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
Source Location: (12:0,12 [11] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
22
|IDisposable|
3-
Generated Location: (813:21,0 [11] )
3+
Generated Location: (788:21,0 [11] )
44
|IDisposable|
55

66
Source Location: (38:1,13 [15] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
77
|this.ToString()|
8-
Generated Location: (1413:39,13 [15] )
8+
Generated Location: (1388:39,13 [15] )
99
|this.ToString()|
1010

1111
Source Location: (79:3,5 [29] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
1212
|string.Format("{0}", "Hello")|
13-
Generated Location: (1610:47,6 [29] )
13+
Generated Location: (1585:47,6 [29] )
1414
|string.Format("{0}", "Hello")|
1515

1616
Source Location: (132:6,12 [37] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
1717
|
1818
void IDisposable.Dispose(){ }
1919
|
20-
Generated Location: (1862:56,12 [37] )
20+
Generated Location: (1837:56,12 [37] )
2121
|
2222
void IDisposable.Dispose(){ }
2323
|

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent_Runtime.codegen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd"
22
// <auto-generated/>
33
#pragma warning disable 1591
4-
namespace __GeneratedComponent
4+
namespace TestFiles.IntegrationTests.CodeGenerationIntegrationTest
55
{
66
#line default
77
using global::System;
@@ -12,7 +12,7 @@ namespace __GeneratedComponent
1212
#line default
1313
#line hidden
1414
#nullable restore
15-
public partial class AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
15+
public partial class BasicComponent : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
1616
#nullable disable
1717
{
1818
#pragma warning disable 1998

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent_Runtime.ir.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Document -
2-
NamespaceDeclaration - - __GeneratedComponent
2+
NamespaceDeclaration - - TestFiles.IntegrationTests.CodeGenerationIntegrationTest
33
UsingDirective - (1:0,1 [22] ) - global::System
44
UsingDirective - (24:1,1 [42] ) - global::System.Collections.Generic
55
UsingDirective - (67:2,1 [27] ) - global::System.Linq
66
UsingDirective - (95:3,1 [38] ) - global::System.Threading.Tasks
77
UsingDirective - (134:4,1 [45] ) - global::Microsoft.AspNetCore.Components
8-
ClassDeclaration - - public partial - AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
8+
ClassDeclaration - - public partial - BasicComponent - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
99
MethodDeclaration - - protected override - void - BuildRenderTree
1010
MarkupElement - (25:1,0 [91] BasicComponent.cshtml) - div
1111
HtmlAttribute - (29:1,4 [25] BasicComponent.cshtml) - class=" - "

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/MetadataAttributePassTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,20 @@ public void Execute_NoNamespaceSet_Noops()
133133
pass.Execute(codeDocument, irDocument);
134134

135135
// Assert
136-
SingleChild<NamespaceDeclarationIntermediateNode>(irDocument);
136+
Assert.Equal(2, irDocument.Children.Count);
137+
138+
var item = Assert.IsType<RazorCompiledItemAttributeIntermediateNode>(irDocument.Children[0]);
139+
Assert.Equal("/test.cshtml", item.Identifier);
140+
Assert.Equal("test", item.Kind);
141+
Assert.Equal("Test", item.TypeName);
142+
143+
Assert.Equal(2, @namespace.Children.Count);
144+
var checksum = Assert.IsType<RazorSourceChecksumAttributeIntermediateNode>(@namespace.Children[0]);
145+
Assert.Equal(CodeAnalysis.Text.SourceHashAlgorithm.Sha256, checksum.ChecksumAlgorithm);
146+
Assert.Equal("/test.cshtml", checksum.Identifier);
147+
148+
var foundClass = Assert.IsType<ClassDeclarationIntermediateNode>(@namespace.Children[1]);
149+
Assert.Equal("Test", foundClass.ClassName);
137150
}
138151

139152
[Fact]

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9643,6 +9643,39 @@ @preservewhitespace false
96439643

96449644
#endregion
96459645

9646+
#region Namespace
9647+
9648+
[IntegrationTestFact]
9649+
public void EmptyRootNamespace()
9650+
{
9651+
DefaultRootNamespace = string.Empty;
9652+
9653+
AdditionalSyntaxTrees.Add(Parse("""
9654+
using Microsoft.AspNetCore.Components;
9655+
public class Component1 : ComponentBase { }
9656+
namespace Shared
9657+
{
9658+
public class Component2 : ComponentBase { }
9659+
}
9660+
class C
9661+
{
9662+
void M1(TestComponent t) { }
9663+
void M2(global::TestComponent t) { }
9664+
}
9665+
"""));
9666+
var generated = CompileToCSharp("""
9667+
<h1>Generated</h1>
9668+
<Component1 />
9669+
<Shared.Component2 />
9670+
""");
9671+
9672+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9673+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9674+
CompileToAssembly(generated);
9675+
}
9676+
9677+
#endregion
9678+
96469679
#region "CSS scoping"
96479680
[IntegrationTestFact]
96489681
public void Component_WithCssScope()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
4+
#line default
5+
using global::System;
6+
using global::System.Collections.Generic;
7+
using global::System.Linq;
8+
using global::System.Threading.Tasks;
9+
using global::Microsoft.AspNetCore.Components;
10+
#line default
11+
#line hidden
12+
#nullable restore
13+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
14+
#nullable disable
15+
{
16+
#pragma warning disable 219
17+
private void __RazorDirectiveTokenHelpers__() {
18+
}
19+
#pragma warning restore 219
20+
#pragma warning disable 0414
21+
private static object __o = null;
22+
#pragma warning restore 0414
23+
#pragma warning disable 1998
24+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
25+
{
26+
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
27+
}
28+
));
29+
#nullable restore
30+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
31+
__o = typeof(global::Component1);
32+
33+
#line default
34+
#line hidden
35+
#nullable disable
36+
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
37+
}
38+
));
39+
#nullable restore
40+
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
41+
__o = typeof(global::Shared.Component2);
42+
43+
#line default
44+
#line hidden
45+
#nullable disable
46+
}
47+
#pragma warning restore 1998
48+
}
49+
50+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Document -
2+
NamespaceDeclaration - -
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+
MarkupElement - (0:0,0 [18] x:\dir\subdir\Test\TestComponent.cshtml) - h1
18+
HtmlContent - (4:0,4 [9] x:\dir\subdir\Test\TestComponent.cshtml)
19+
LazyIntermediateToken - (4:0,4 [9] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Generated
20+
HtmlContent - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml)
21+
LazyIntermediateToken - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
22+
Component - (20:1,0 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Component1
23+
HtmlContent - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml)
24+
LazyIntermediateToken - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
25+
Component - (36:2,0 [21] x:\dir\subdir\Test\TestComponent.cshtml) - Shared.Component2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
4+
#line default
5+
using global::System;
6+
using global::System.Collections.Generic;
7+
using global::System.Linq;
8+
using global::System.Threading.Tasks;
9+
using global::Microsoft.AspNetCore.Components;
10+
#line default
11+
#line hidden
12+
#nullable restore
13+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
14+
#nullable disable
15+
{
16+
#pragma warning disable 1998
17+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
18+
{
19+
__builder.AddMarkupContent(0, "<h1>Generated</h1>\r\n");
20+
__builder.OpenComponent<global::Component1>(1);
21+
__builder.CloseComponent();
22+
__builder.AddMarkupContent(2, "\r\n");
23+
__builder.OpenComponent<global::Shared.Component2>(3);
24+
__builder.CloseComponent();
25+
}
26+
#pragma warning restore 1998
27+
}
28+
29+
#pragma warning restore 1591

0 commit comments

Comments
 (0)