Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <auto-generated/>
#pragma warning disable 1591
namespace __GeneratedComponent
namespace TestFiles.IntegrationTests.CodeGenerationIntegrationTest
{
#line default
using global::System;
Expand All @@ -11,7 +11,7 @@ namespace __GeneratedComponent
#line default
#line hidden
#nullable restore
public partial class AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
public partial class BasicComponent : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
#nullable disable
{
#pragma warning disable 219
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Document -
NamespaceDeclaration - - __GeneratedComponent
NamespaceDeclaration - - TestFiles.IntegrationTests.CodeGenerationIntegrationTest
UsingDirective - (1:0,1 [20] ) - global::System
UsingDirective - (24:1,1 [40] ) - global::System.Collections.Generic
UsingDirective - (67:2,1 [25] ) - global::System.Linq
UsingDirective - (95:3,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (134:4,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
ClassDeclaration - - public partial - BasicComponent - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
DesignTimeDirective -
DirectiveToken - (12:0,12 [11] BasicComponent.cshtml) - IDisposable
CSharpCode -
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Source Location: (12:0,12 [11] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
|IDisposable|
Generated Location: (813:21,0 [11] )
Generated Location: (788:21,0 [11] )
|IDisposable|

Source Location: (38:1,13 [15] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
|this.ToString()|
Generated Location: (1413:39,13 [15] )
Generated Location: (1388:39,13 [15] )
|this.ToString()|

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

Source Location: (132:6,12 [37] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml)
|
void IDisposable.Dispose(){ }
|
Generated Location: (1862:56,12 [37] )
Generated Location: (1837:56,12 [37] )
|
void IDisposable.Dispose(){ }
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd"
// <auto-generated/>
#pragma warning disable 1591
namespace __GeneratedComponent
namespace TestFiles.IntegrationTests.CodeGenerationIntegrationTest
{
#line default
using global::System;
Expand All @@ -12,7 +12,7 @@ namespace __GeneratedComponent
#line default
#line hidden
#nullable restore
public partial class AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
public partial class BasicComponent : global::Microsoft.AspNetCore.Components.ComponentBase, IDisposable
#nullable disable
{
#pragma warning disable 1998
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Document -
NamespaceDeclaration - - __GeneratedComponent
NamespaceDeclaration - - TestFiles.IntegrationTests.CodeGenerationIntegrationTest
UsingDirective - (1:0,1 [22] ) - global::System
UsingDirective - (24:1,1 [42] ) - global::System.Collections.Generic
UsingDirective - (67:2,1 [27] ) - global::System.Linq
UsingDirective - (95:3,1 [38] ) - global::System.Threading.Tasks
UsingDirective - (134:4,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - AspNetCore_3708c9fcd2e1ecb6cbaba92bcc60927f830690f42092c4aa1a7275c2f37020fd - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
ClassDeclaration - - public partial - BasicComponent - global::Microsoft.AspNetCore.Components.ComponentBase - IDisposable
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (25:1,0 [91] BasicComponent.cshtml) - div
HtmlAttribute - (29:1,4 [25] BasicComponent.cshtml) - class=" - "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,20 @@ public void Execute_NoNamespaceSet_Noops()
pass.Execute(codeDocument, irDocument);

// Assert
SingleChild<NamespaceDeclarationIntermediateNode>(irDocument);
Assert.Equal(2, irDocument.Children.Count);

var item = Assert.IsType<RazorCompiledItemAttributeIntermediateNode>(irDocument.Children[0]);
Assert.Equal("/test.cshtml", item.Identifier);
Assert.Equal("test", item.Kind);
Assert.Equal("Test", item.TypeName);

Assert.Equal(2, @namespace.Children.Count);
var checksum = Assert.IsType<RazorSourceChecksumAttributeIntermediateNode>(@namespace.Children[0]);
Assert.Equal(CodeAnalysis.Text.SourceHashAlgorithm.Sha256, checksum.ChecksumAlgorithm);
Assert.Equal("/test.cshtml", checksum.Identifier);

var foundClass = Assert.IsType<ClassDeclarationIntermediateNode>(@namespace.Children[1]);
Assert.Equal("Test", foundClass.ClassName);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9633,6 +9633,39 @@ @preservewhitespace false

#endregion

#region Namespace

[IntegrationTestFact]
public void EmptyRootNamespace()
{
DefaultRootNamespace = string.Empty;

AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components;
public class Component1 : ComponentBase { }
namespace Shared
{
public class Component2 : ComponentBase { }
}
class C
{
void M1(TestComponent t) { }
void M2(global::TestComponent t) { }
}
"""));
var generated = CompileToCSharp("""
<h1>Generated</h1>
<Component1 />
<Shared.Component2 />
""");

AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

#endregion

#region "CSS scoping"
[IntegrationTestFact]
public void Component_WithCssScope()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// <auto-generated/>
#pragma warning disable 1591

#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)
{
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Component1);

#line default
#line hidden
#nullable disable
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Shared.Component2);

#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,25 @@
Document -
NamespaceDeclaration - -
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
MarkupElement - (0:0,0 [18] x:\dir\subdir\Test\TestComponent.cshtml) - h1
HtmlContent - (4:0,4 [9] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (4:0,4 [9] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Generated
HtmlContent - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (18:0,18 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
Component - (20:1,0 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Component1
HtmlContent - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
Component - (36:2,0 [21] x:\dir\subdir\Test\TestComponent.cshtml) - Shared.Component2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <auto-generated/>
#pragma warning disable 1591

#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)
{
__builder.AddMarkupContent(0, "<h1>Generated</h1>\r\n");
__builder.OpenComponent<global::Component1>(1);
__builder.CloseComponent();
__builder.AddMarkupContent(2, "\r\n");
__builder.OpenComponent<global::Shared.Component2>(3);
__builder.CloseComponent();
}
#pragma warning restore 1998
}

#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Document -
NamespaceDeclaration - -
UsingDirective - (3:1,1 [22] ) - global::System
UsingDirective - (26:2,1 [42] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [27] ) - global::System.Linq
UsingDirective - (97:4,1 [38] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [47] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupBlock - - <h1>Generated</h1>\n
Component - (20:1,0 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Component1
HtmlContent - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (34:1,14 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
Component - (36:2,0 [21] x:\dir\subdir\Test\TestComponent.cshtml) - Shared.Component2
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ private ref struct DisplayNameHelper(INamedTypeSymbol type)
private (string Type, string Namespace)? _names;

public (string Type, string Namespace) GetNames()
=> _names ??= (_type.ToDisplayString(), _type.ContainingNamespace.ToDisplayString());
=> _names ??= (_type.ToDisplayString(),
_type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_type.ContainingNamespace.ToDisplayString() would be "<global namespace>"; with SymbolExtensions.FullNameTypeDisplayFormat we get an empty string instead (this format was already used in other places, just not everywhere)

}

private static TagHelperDescriptor CreateElementBindTagHelper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(

if (fullyQualified)
{
var containingNamespace = type.ContainingNamespace.ToDisplayString();
var fullName = $"{containingNamespace}.{type.Name}";
var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat);
var fullName = string.IsNullOrEmpty(containingNamespace)
? type.Name
: $"{containingNamespace}.{type.Name}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if we were more direct with the condition here:

Suggested change
var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat);
var fullName = string.IsNullOrEmpty(containingNamespace)
? type.Name
: $"{containingNamespace}.{type.Name}";
var fullName = type.ContainingNamespace.IsGlobalNamespace
? type.Name
: $"{type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat)}.{type.Name}";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible to have a component in a nested type? If so, will this handle that correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible to have a component in a nested type? If so, will this handle that correctly?

I don't think it's possible. (You've already asked me this previously in #9689 (comment) :D)

In any case, the previous code did "{namespace}.{typeName}" and I've just changed it to omit the "{namespace}." prefix when needed, so nothing should change at this front, right?


builder.TagMatchingRule(r =>
{
Expand All @@ -125,7 +127,8 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(

metadata.Add(ComponentMetadata.Component.NameMatchKey, ComponentMetadata.Component.FullyQualifiedNameMatch);
}
else
// If the component is in the global namespace, skip adding this rule which is the same as the fully qualified one.
else if (!type.ContainingNamespace.IsGlobalNamespace)
{
builder.TagMatchingRule(r =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ private ref struct DisplayNameHelper(INamedTypeSymbol type)

public (string Type, string Namespace) GetNames()
{
_names ??= (_type.ToDisplayString(), _type.ContainingNamespace.ToDisplayString());
_names ??= (_type.ToDisplayString(),
_type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat));

return _names.GetValueOrDefault();
}
Expand Down
Loading