Skip to content

Commit

Permalink
Handle :get/:set in EditorRequired checking (#10628)
Browse files Browse the repository at this point in the history
* Add utility for verifying razor diagnostics

* Handle `:get`/`:set` in `EditorRequired` checking

* Simplify code
  • Loading branch information
jjonescz authored Jul 17, 2024
1 parent 850778b commit c0b9aa2
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
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);
}
}

0 comments on commit c0b9aa2

Please sign in to comment.