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

Consider @bind for EditorRequired parameters #8337

Merged
merged 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -196,7 +196,14 @@ static bool IsPresentAsAttribute(string attributeName, ComponentIntermediateNode
{
return true;
}
else if (child is ComponentChildContentIntermediateNode childContent && attributeName == childContent.AttributeName)
if (child is ComponentChildContentIntermediateNode childContent && attributeName == childContent.AttributeName)
{
return true;
}
const string bindPrefix = "@bind-";
if (child is TagHelperDirectiveAttributeIntermediateNode { OriginalAttributeName: { } originalAttributeName } &&
originalAttributeName.StartsWith(bindPrefix, StringComparison.Ordinal) &&
originalAttributeName.AsSpan()[bindPrefix.Length..].Equals(attributeName.AsSpan(), StringComparison.Ordinal))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,37 @@ public class ComponentWithEditorRequiredParameters : ComponentBase
Assert.Empty(generated.Diagnostics);
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7395")]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBind()
{
AdditionalSyntaxTrees.Add(Parse("""
using System;
using Microsoft.AspNetCore.Components;

namespace Test;

public class ComponentWithEditorRequiredParameters : ComponentBase
{
[Parameter]
[EditorRequired]
public string Property1 { get; set; }
}
"""));

var generated = CompileToCSharp("""
<ComponentWithEditorRequiredParameters @bind-Property1="myField" />

@code {
private string myField = "Some Value";
}
""");

AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
Assert.Empty(generated.Diagnostics);
}

[Fact]
public void Component_WithEditorRequiredChildContent_NoValueSpecified()
{
Expand Down Expand Up @@ -6723,7 +6754,7 @@ public class Child<TOne, TTwo> : ComponentBase
CompileToAssembly(generated);
}

[Fact] // https://github.com/dotnet/razor/issues/7103
[Fact, WorkItem("https://github.com/dotnet/razor/issues/7103")]
public void CascadingGenericInference_ParameterInNamespace()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#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.String>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
myField

#line default
#line hidden
#nullable disable
);
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => myField = __value, myField);
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#pragma warning disable BL0005
((global::Test.ComponentWithEditorRequiredParameters)default).
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
Property1

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

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

private string myField = "Some Value";

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 [67] x:\dir\subdir\Test\TestComponent.cshtml) - ComponentWithEditorRequiredParameters
ComponentAttribute - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Property1 - Property1 - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - myField
ComponentAttribute - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Property1Changed - - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this,
IntermediateToken - - CSharp - __value => myField = __value
IntermediateToken - - CSharp - , myField)
HtmlContent - (67:0,67 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (67:0,67 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private string myField = "Some Value";\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Source Location: (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml)
|myField|
Generated Location: (1082:25,56 [7] )
|myField|

Source Location: (45:0,45 [9] x:\dir\subdir\Test\TestComponent.cshtml)
|Property1|
Generated Location: (1720:39,45 [9] )
|Property1|

Source Location: (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml)
|
private string myField = "Some Value";
|
Generated Location: (2163:57,7 [46] )
|
private string myField = "Some Value";
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Test.ComponentWithEditorRequiredParameters>(0);
__builder.AddComponentParameter(1, "Property1", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.String>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
myField

#line default
#line hidden
#nullable disable
));
__builder.AddComponentParameter(2, "Property1Changed", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => myField = __value, myField));
__builder.CloseComponent();
}
#pragma warning restore 1998
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"

private string myField = "Some Value";

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 [67] x:\dir\subdir\Test\TestComponent.cshtml) - ComponentWithEditorRequiredParameters
ComponentAttribute - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Property1 - Property1 - AttributeStructure.DoubleQuotes
CSharpExpression -
LazyIntermediateToken - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - myField
ComponentAttribute - (56:0,56 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Property1Changed - - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - - CSharp - global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this,
IntermediateToken - - CSharp - __value => myField = __value
IntermediateToken - - CSharp - , myField)
CSharpCode - (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private string myField = "Some Value";\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Source Location: (78:2,7 [46] x:\dir\subdir\Test\TestComponent.cshtml)
|
private string myField = "Some Value";
|
Generated Location: (1414:31,7 [46] )
|
private string myField = "Some Value";
|

Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ internal sealed class Views_Shared__Layout : global::Microsoft.AspNetCore.Mvc.Ra
);
}

[Fact] // https://github.com/dotnet/razor/issues/7049
[Fact, WorkItem("https://github.com/dotnet/razor/issues/7049")]
public async Task SourceGenerator_CshtmlFiles_TagHelperInFunction()
{
// Arrange
Expand Down Expand Up @@ -2005,7 +2005,10 @@ internal sealed class Pages_Index : global::Microsoft.AspNetCore.Mvc.Razor.Razor
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral(""\r\n"");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""h2"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, ""5d59ecd7b7cf7355d7f60234988be34b81a8b6142529"", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""h2"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, ""5d59ecd7b7cf7355d7f60234988be34b81a8b6142529"",
#pragma warning disable 1998
async() => {
#pragma warning restore 1998
WriteLiteral(""Hello world"");
}
);
Expand Down Expand Up @@ -2504,7 +2507,10 @@ internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""vc:test"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, ""209ff2a910aa467bb7942ed3e6cb586652327a442587"", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""vc:test"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, ""209ff2a910aa467bb7942ed3e6cb586652327a442587"",
#pragma warning disable 1998
async() => {
#pragma warning restore 1998
}
);
__TestViewComponentTagHelper = CreateTagHelper<global::AspNetCoreGeneratedDocument.Views_Home_Index.__Generated__TestViewComponentTagHelper>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("\r\n");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("email", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "c7a5bfe3b24fca2a512de60cbc0070e782f08b7a2540", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("email", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "c7a5bfe3b24fca2a512de60cbc0070e782f08b7a2540",
#pragma warning disable 1998
async() => {
#pragma warning restore 1998
WriteLiteral("\r\n custom tag helper\r\n ");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("email", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "c7a5bfe3b24fca2a512de60cbc0070e782f08b7a2828", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("email", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "c7a5bfe3b24fca2a512de60cbc0070e782f08b7a2930",
#pragma warning disable 1998
async() => {
#pragma warning restore 1998
WriteLiteral("nested tag helper");
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.
#line hidden
#nullable disable
WriteLiteral("\r\n");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("vc:test", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "ae625dc7ef6db4af156601325d79196402a512422749", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("vc:test", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "ae625dc7ef6db4af156601325d79196402a512422749",
#pragma warning disable 1998
async() => {
#pragma warning restore 1998
}
);
__TestViewComponentTagHelper = CreateTagHelper<global::AspNetCoreGeneratedDocument.Views_Home_Index.__Generated__TestViewComponentTagHelper>();
Expand Down