Skip to content
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

Global namespace component bind #10798

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -2743,6 +2743,36 @@ public class MyComponent<T> : ComponentBase
CompileToAssembly(generated);
}

[IntegrationTestFact]
chsienki marked this conversation as resolved.
Show resolved Hide resolved
public void BindToComponent_SpecifiesValue_WithMatchingProperties_GlobalNamespaceComponent()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;

public class MyComponent : ComponentBase
{
[Parameter]
public int Value { get; set; }

[Parameter]
public Action<int> ValueChanged { get; set; }
}"));

// Act
var generated = CompileToCSharp(@"
<MyComponent @bind-Value=""ParentValue"" />
@code {
public int ParentValue { get; set; } = 42;
}");

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

[IntegrationTestFact]
public void BindToElement_WritesAttributes()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// <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)
{
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
ParentValue

#line default
#line hidden
#nullable disable
);
__o = new global::System.Action<System.Int32>(
__value => ParentValue = __value);
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#pragma warning disable BL0005
((global::MyComponent)default).
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Value

#line default
#line hidden
#nullable disable
= default;
#pragma warning restore BL0005
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::MyComponent);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"

public int ParentValue { get; set; } = 42;

#line default
#line hidden
#nullable disable
}
}
#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
Component - (0:0,0 [41] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentValue
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - __value => ParentValue = __value
HtmlContent - (41:0,41 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (41:0,41 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public int ParentValue { get; set; } = 42;\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Source Location: (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|ParentValue|
Generated Location: (1135:29,26 [11] )
|ParentValue|

Source Location: (19:0,19 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|Value|
Generated Location: (1663:44,19 [5] )
|Value|

Source Location: (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
|
public int ParentValue { get; set; } = 42;
|
Generated Location: (2071:62,7 [50] )
|
public int ParentValue { get; set; } = 42;
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// <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)
{
__builder.OpenComponent<global::MyComponent>(0);
__builder.AddComponentParameter(1, nameof(global::MyComponent.
#nullable restore
#line (1,20)-(1,25) "x:\dir\subdir\Test\TestComponent.cshtml"
Value

#line default
#line hidden
#nullable disable
), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Int32>(
#nullable restore
#line (1,27)-(1,38) "x:\dir\subdir\Test\TestComponent.cshtml"
ParentValue

#line default
#line hidden
#nullable disable
));
__builder.AddComponentParameter(2, nameof(global::MyComponent.ValueChanged), (global::System.Action<System.Int32>)(__value => ParentValue = __value));
__builder.CloseComponent();
}
#pragma warning restore 1998
#nullable restore
#line (2,8)-(4,1) "x:\dir\subdir\Test\TestComponent.cshtml"

public int ParentValue { get; set; } = 42;

#line default
#line hidden
#nullable disable

}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Document -
NamespaceDeclaration - - Test
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
Component - (0:0,0 [41] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Value - Value - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentValue
ComponentAttribute - (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml) - ValueChanged - ValueChanged - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - __value => ParentValue = __value
CSharpCode - (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public int ParentValue { get; set; } = 42;\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Source Location: (19:0,19 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|Value|
Generated Location: (874:23,0 [5] )
|Value|

Source Location: (26:0,26 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|ParentValue|
Generated Location: (1134:31,0 [11] )
|ParentValue|

Source Location: (50:1,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
|
public int ParentValue { get; set; } = 42;
|
Generated Location: (1548:43,0 [50] )
|
public int ParentValue { get; set; } = 42;
|

Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ protected override void Collect(ISymbol symbol, ICollection<TagHelperDescriptor>
var shortNameMatchingDescriptor = CreateShortNameMatchingDescriptor(type, properties);
results.Add(shortNameMatchingDescriptor);

var fullyQualifiedNameMatchingDescriptor = CreateFullyQualifiedNameMatchingDescriptor(type, properties);
results.Add(fullyQualifiedNameMatchingDescriptor);
// If the component is in the global namespace, skip adding this descriptor which will be the same as the short name one.
TagHelperDescriptor? fullyQualifiedNameMatchingDescriptor = null;
if (!type.ContainingNamespace.IsGlobalNamespace)
{
fullyQualifiedNameMatchingDescriptor = CreateFullyQualifiedNameMatchingDescriptor(type, properties);
results.Add(fullyQualifiedNameMatchingDescriptor);
}

foreach (var childContent in shortNameMatchingDescriptor.GetChildContentProperties())
{
// Synthesize a separate tag helper for each child content property that's declared.
results.Add(CreateChildContentDescriptor(shortNameMatchingDescriptor, childContent));
results.Add(CreateChildContentDescriptor(fullyQualifiedNameMatchingDescriptor, childContent));
if (fullyQualifiedNameMatchingDescriptor is not null)
{
results.Add(CreateChildContentDescriptor(fullyQualifiedNameMatchingDescriptor, childContent));
}
}
}
}
Expand Down Expand Up @@ -111,8 +119,7 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(

metadata.Add(ComponentMetadata.Component.NameMatchKey, ComponentMetadata.Component.FullyQualifiedNameMatch);
}
// 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)
else
{
builder.TagMatchingRule(r =>
{
Expand Down
Loading