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 :get/:set in EditorRequired checking #10628

Merged
merged 3 commits into from
Jul 17, 2024
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 @@ -1198,6 +1198,96 @@ public class ComponentWithEditorRequiredParameters : ComponentBase
Assert.Empty(generated.RazorDiagnostics);
}

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10553")]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBindGetSet()
{
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:get="myField" @bind-Property1:set="OnFieldChanged" />

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

CompileToAssembly(generated);
Assert.Empty(generated.RazorDiagnostics);
}

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10553")]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBindGet()
{
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:get="myField" />

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

CompileToAssembly(generated);
Assert.Empty(generated.RazorDiagnostics);
}

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10553")]
public void Component_WithEditorRequiredParameter_ValueSpecifiedUsingBindSet()
{
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:set="OnFieldChanged" />

@code {
private void OnFieldChanged(string value) { }
}
""");

var compiled = CompileToAssembly(generated);
generated.RazorDiagnostics.Verify(
// x:\dir\subdir\Test\TestComponent.cshtml(1,61): error RZ10016: Attribute 'bind-Property1:set' was used but no attribute 'bind-Property1:get' was found.
Diagnostic("RZ10016").WithLocation(1, 61));
}

[IntegrationTestFact]
public void Component_WithEditorRequiredChildContent_NoValueSpecified()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,17 @@ static bool IsPresentAsAttribute(string attributeName, ComponentIntermediateNode
const string bindPrefix = "@bind-";
if (child is TagHelperDirectiveAttributeIntermediateNode { OriginalAttributeName: { } originalAttributeName } &&
originalAttributeName.StartsWith(bindPrefix, StringComparison.Ordinal) &&
originalAttributeName.AsSpan()[bindPrefix.Length..].Equals(attributeName.AsSpan(), StringComparison.Ordinal))
originalAttributeName.AsSpan(start: bindPrefix.Length).Equals(attributeName.AsSpan(), StringComparison.Ordinal))
{
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))
{
// `@bind-Value:get` or `@bind-Value:set` is specified.
return true;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.NET.Sdk.Razor.SourceGenerators;

namespace Microsoft.CodeAnalysis;

public static class RazorDiagnosticExtensions
{
public static void Verify(
this IEnumerable<RazorDiagnostic> diagnostics,
params DiagnosticDescription[] expected)
{
diagnostics.Select(d => d.AsDiagnostic()).Verify(expected);
}
}