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

Handle EditorRequired *Changed/*Expression parameters #11043

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1141,6 +1141,35 @@ public class ComponentWithEditorRequiredParameters : ComponentBase
Assert.Empty(generated.RazorDiagnostics);
}

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

namespace Test;

public class ComponentWithEditorRequiredParameters : ComponentBase
{
[Parameter, EditorRequired]
public bool Property1 { get; set; }

[Parameter, EditorRequired]
public EventCallback<bool> Property1Changed { get; set; }
}
"""));

var generated = CompileToCSharp("""
<ComponentWithEditorRequiredParameters Property1="false" />
""");

CompileToAssembly(generated);
generated.RazorDiagnostics.Verify(
// x:\dir\subdir\Test\TestComponent.cshtml(1,1): warning RZ2012: Component 'ComponentWithEditorRequiredParameters' expects a value for the parameter 'Property1Changed', but a value may not have been provided.
Diagnostic("RZ2012").WithLocation(1, 1));
}

[IntegrationTestFact]
public void Component_WithEditorRequiredParameter_ValueSpecified_DifferentCasing()
{
Expand Down Expand Up @@ -1217,6 +1246,37 @@ public class ComponentWithEditorRequiredParameters : ComponentBase
Assert.Empty(generated.RazorDiagnostics);
}

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

namespace Test;

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

[Parameter, EditorRequired]
public EventCallback<string> Property1Changed { get; set; }
}
"""));

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

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

CompileToAssembly(generated);
generated.RazorDiagnostics.Verify();
}

[IntegrationTestFact]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBind_DifferentCasing()
{
Expand Down Expand Up @@ -1266,6 +1326,38 @@ private void OnFieldChanged(string value) { }
Assert.Empty(generated.RazorDiagnostics);
}

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

namespace Test;

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

[Parameter, EditorRequired]
public EventCallback<string> Property1Changed { get; set; }
}
"""));

var generated = CompileToCSharp("""
<ComponentWithEditorRequiredParameters @bind-Property1:get="myField" @bind-Property1:set="OnFieldChanged" />

@code {
private string myField = "Some Value";
private void OnFieldChanged(string value) { }
}
""");

CompileToAssembly(generated);
generated.RazorDiagnostics.Verify();
}

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10553")]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBindGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ static bool IsPresentAsAttribute(string attributeName, ComponentIntermediateNode
const string bindPrefix = "@bind-";
if (child is TagHelperDirectiveAttributeIntermediateNode { OriginalAttributeName: { } originalAttributeName } &&
originalAttributeName.StartsWith(bindPrefix, StringComparison.Ordinal) &&
originalAttributeName.AsSpan(start: bindPrefix.Length).Equals(attributeName.AsSpan(), StringComparison.Ordinal))
EqualsWithOptionalChangedSuffix(originalAttributeName.AsSpan(start: bindPrefix.Length), attributeName))
{
return true;
}
if (child is TagHelperDirectiveAttributeParameterIntermediateNode { OriginalAttributeName: { } originalName, AttributeNameWithoutParameter: { } nameWithoutParameter } &&
originalName.StartsWith(bindPrefix, StringComparison.Ordinal) &&
nameWithoutParameter.AsSpan(start: bindPrefix.Length - 1).Equals(attributeName.AsSpan(), StringComparison.Ordinal))
EqualsWithOptionalChangedSuffix(nameWithoutParameter.AsSpan(start: bindPrefix.Length - 1), attributeName))
{
// `@bind-Value:get` or `@bind-Value:set` is specified.
return true;
Expand All @@ -218,6 +218,16 @@ static bool IsPresentAsAttribute(string attributeName, ComponentIntermediateNode

return false;
}

// True if `specifiedName` is equal to `requiredName` or to `requiredName + "Changed"`.
static bool EqualsWithOptionalChangedSuffix(ReadOnlySpan<char> specifiedName, string requiredName)
{
const string changedSuffix = "Changed";
var requiredNameSpan = requiredName.AsSpan();
return requiredNameSpan.EndsWith(changedSuffix.AsSpan(), StringComparison.Ordinal)
? specifiedName.Equals(requiredNameSpan[..^changedSuffix.Length], StringComparison.Ordinal)
: specifiedName.Equals(requiredNameSpan, StringComparison.Ordinal);
}
}

private static MarkupElementIntermediateNode RewriteAsElement(TagHelperIntermediateNode node)
Expand Down