Skip to content

Commit

Permalink
Fix warning for @formname without @onsubmit (#9296)
Browse files Browse the repository at this point in the history
* Fix warning for `@formname` without `@onsubmit`

* Remove the warning completely
  • Loading branch information
jjonescz authored Sep 19, 2023
1 parent 0836e19 commit 8848273
Show file tree
Hide file tree
Showing 139 changed files with 2,506 additions and 1,001 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,7 @@ public static RazorDiagnostic CreateBindAttributeParameter_UnsupportedSyntaxBind
return diagnostic;
}

public static readonly RazorDiagnosticDescriptor FormName_MissingOnSubmit =
new RazorDiagnosticDescriptor(
$"{DiagnosticPrefix}10021",
() => "Attribute '@formname' can only be used when '@onsubmit' event handler is also present.",
RazorDiagnosticSeverity.Warning);

public static RazorDiagnostic CreateFormName_MissingOnSubmit(SourceSpan? source)
{
return RazorDiagnostic.Create(FormName_MissingOnSubmit, source ?? SourceSpan.Undefined);
}
// Removed warning RZ10021: Attribute '@formname' can only be used when '@onsubmit' event handler is also present.

public static readonly RazorDiagnosticDescriptor FormName_NotAForm =
new RazorDiagnosticDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
continue;
}

if (!parent.Children.Any(c => c is HtmlAttributeIntermediateNode { AttributeName: "@onsubmit" }))
{
node.Diagnostics.Add(ComponentDiagnosticFactory.CreateFormName_MissingOnSubmit(node.Source));
reference.Replace(RewriteForErrorRecovery(node, parent));
continue;
}

reference.Replace(Rewrite(node));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10309,6 +10309,7 @@ public void FormName_HtmlValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="named-form-handler"></form>
""");

Expand All @@ -10323,6 +10324,7 @@ public void FormName_CSharpValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@("named-form-handler")"></form>
""");

Expand All @@ -10337,6 +10339,7 @@ public void FormName_CSharpValue_Integer()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@x"></form>
@code {
int x = 1;
Expand All @@ -10348,16 +10351,17 @@ public void FormName_CSharpValue_Integer()
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
var result = CompileToAssembly(generated, throwOnFailure: false);
result.Diagnostics.Verify(
// x:\dir\subdir\Test\TestComponent.cshtml(1,55): error CS1503: Argument 1: cannot convert from 'int' to 'string'
// x:\dir\subdir\Test\TestComponent.cshtml(2,55): error CS1503: Argument 1: cannot convert from 'int' to 'string'
// x
Diagnostic(ErrorCode.ERR_BadArgType, "x").WithArguments("1", "int", "string").WithLocation(1, 55));
Diagnostic(ErrorCode.ERR_BadArgType, "x").WithArguments("1", "int", "string").WithLocation(2, 55));
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9077")]
public void FormName_MixedValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="start @("literal") @x end"></form>
@code {
int x = 1;
Expand All @@ -10377,6 +10381,7 @@ public void FormName_Nullability()

// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@null"></form>
""",
nullableEnable: true);
Expand All @@ -10392,6 +10397,7 @@ public void FormName_CSharpError()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@x"></form>
""");

Expand All @@ -10400,16 +10406,17 @@ public void FormName_CSharpError()
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
var result = CompileToAssembly(generated, throwOnFailure: false);
result.Diagnostics.Verify(
// x:\dir\subdir\Test\TestComponent.cshtml(1,55): error CS0103: The name 'x' does not exist in the current context
// x:\dir\subdir\Test\TestComponent.cshtml(2,55): error CS0103: The name 'x' does not exist in the current context
// x
Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(1, 55));
Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(2, 55));
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9077")]
public void FormName_RazorError()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@{ }"></form>
""");

Expand All @@ -10423,6 +10430,7 @@ public void FormName_NotAForm()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<div method="post" @onsubmit="() => { }" @formname="named-form-handler"></div>
<div method="post" @onsubmit="() => { }" @formname="@("named-form-handler")"></div>
""");
Expand All @@ -10432,11 +10440,26 @@ public void FormName_NotAForm()
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9077")]
public void FormName_MissingUsing()
{
// Act
var generated = CompileToCSharp("""
<form method="post" @onsubmit="() => { }" @formname="named-form-handler"></form>
<form method="post" @onsubmit="() => { }" @formname="@("named-form-handler")"></form>
""");

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

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9077")]
public void FormName_NotAForm_RazorLangVersion7()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<div method="post" @onsubmit="() => { }" @formname="named-form-handler"></div>
<div method="post" @onsubmit="() => { }" @formname="@("named-form-handler")"></div>
""",
Expand All @@ -10453,6 +10476,7 @@ public void FormName_MissingSubmit()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @formname="named-form-handler"></form>
<form method="post" @formname="@("named-form-handler")"></form>
""");
Expand All @@ -10467,6 +10491,7 @@ public void FormName_FakeSubmit()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" onsubmit="" @formname="named-form-handler"></form>
<form method="post" onsubmit="" @formname="@("named-form-handler")"></form>
""");
Expand All @@ -10481,6 +10506,7 @@ public void FormName_Component()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<TestComponent method="post" @onsubmit="() => { }" @formname="named-form-handler" />
<TestComponent method="post" @onsubmit="() => { }" @formname="@("named-form-handler")" />
""");
Expand All @@ -10495,6 +10521,7 @@ public void FormName_Component_RazorLangVersion7()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<TestComponent method="post" @onsubmit="() => { }" @formname="named-form-handler" />
<TestComponent method="post" @onsubmit="() => { }" @formname="@("named-form-handler")" />
""",
Expand All @@ -10511,6 +10538,7 @@ public void FormName_Component_Generic()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
@typeparam T
<TestComponent method="post" @onsubmit="() => { }" @formname="named-form-handler" Parameter="1" />
<TestComponent method="post" @onsubmit="() => { }" @formname="@("named-form-handler")" Parameter="2" />
Expand All @@ -10529,6 +10557,7 @@ public void FormName_Component_Generic_RazorLangVersion7()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
@typeparam T
<TestComponent method="post" @onsubmit="() => { }" @formname="named-form-handler" Parameter="1" />
<TestComponent method="post" @onsubmit="() => { }" @formname="@("named-form-handler")" Parameter="2" />
Expand All @@ -10549,6 +10578,7 @@ public void FormName_Duplicate_HtmlValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="x" @formname="y"></form>
""");

Expand All @@ -10571,6 +10601,7 @@ public void FormName_Duplicate_CSharpValue()

// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@x" @formname="@y"></form>
@code {
string x = "a";
Expand All @@ -10589,6 +10620,7 @@ public void FormName_MoreElements_HtmlValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="x"></form>
<form method="post" @onsubmit="() => { }" @formname="y"></form>
""");
Expand All @@ -10604,6 +10636,7 @@ public void FormName_MoreElements_CSharpValue()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="@x"></form>
<form method="post" @onsubmit="() => { }" @formname="@y"></form>
@code {
Expand All @@ -10623,6 +10656,7 @@ public void FormName_Nested()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="1"></form>
<TestComponent>
<form method="post" @onsubmit="() => { }" @formname="2"></form>
Expand Down Expand Up @@ -10661,6 +10695,7 @@ public sealed class RenderTreeBuilder
public void AddMarkupContent(int sequence, string markupContent) { }
public void OpenElement(int sequence, string elementName) { }
public void AddAttribute(int sequence, string name, string value) { }
public void AddAttribute<TArgument>(int sequence, string name, EventCallback<TArgument> value) { }
public void CloseElement() { }
}
}
Expand All @@ -10671,6 +10706,27 @@ public static class RuntimeHelpers
public static T TypeCheck<T>(T value) => throw null;
}
}
namespace Web
{
[EventHandler("onsubmit", typeof(System.EventArgs), true, true)]
public static class EventHandlers
{
}
}
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EventHandlerAttribute : System.Attribute
{
public EventHandlerAttribute(string attributeName, System.Type eventArgsType, bool enableStopPropagation, bool enablePreventDefault) { }
}
public readonly struct EventCallback
{
public static readonly EventCallbackFactory Factory;
}
public readonly struct EventCallback<TValue> { }
public sealed class EventCallbackFactory
{
public EventCallback<TValue> Create<TValue>(object receiver, System.Action callback) => throw null;
}
}
""";
var minimalShimRef = CSharpCompilation.Create(
Expand All @@ -10683,6 +10739,7 @@ public static class RuntimeHelpers

// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="named-form-handler"></form>
""",
baseCompilation: baseCompilation);
Expand All @@ -10698,6 +10755,7 @@ public void FormName_RazorLangVersion7()
{
// Act
var generated = CompileToCSharp("""
@using Microsoft.AspNetCore.Components.Web
<form method="post" @onsubmit="() => { }" @formname="named-form-handler"></form>
""",
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace Test
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
using Microsoft.AspNetCore.Components.Web;

#line default
#line hidden
#nullable disable
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
Expand All @@ -20,9 +27,18 @@ private void __RazorDirectiveTokenHelpers__() {
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o = global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.EventArgs>(this,
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
() => { }

#line default
#line hidden
#nullable disable
);
global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<string>(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
x

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
UsingDirective - (1:0,1 [41] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Web
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
Expand All @@ -14,13 +15,17 @@
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [64] x:\dir\subdir\Test\TestComponent.cshtml) - form
HtmlContent - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
MarkupElement - (44:1,0 [64] x:\dir\subdir\Test\TestComponent.cshtml) - form
HtmlAttribute - - method=" - "
HtmlAttributeValue - (14:0,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (14:0,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - post
HtmlAttribute - - @onsubmit=" - "
HtmlAttributeValue - (31:0,31 [9] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (31:0,31 [9] x:\dir\subdir\Test\TestComponent.cshtml) - Html - () => { }
FormName - (53:0,53 [2] x:\dir\subdir\Test\TestComponent.cshtml)
CSharpExpression - (54:0,54 [1] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (54:0,54 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - x
HtmlAttributeValue - (58:1,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (58:1,14 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - post
HtmlAttribute - (75:1,31 [9] x:\dir\subdir\Test\TestComponent.cshtml) - onsubmit=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<global::System.EventArgs>(this,
LazyIntermediateToken - (75:1,31 [9] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - () => { }
IntermediateToken - - CSharp - )
FormName - (97:1,53 [2] x:\dir\subdir\Test\TestComponent.cshtml)
CSharpExpression - (98:1,54 [1] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (98:1,54 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - x
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Source Location: (54:0,54 [1] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (1:0,1 [41] x:\dir\subdir\Test\TestComponent.cshtml)
|using Microsoft.AspNetCore.Components.Web|
Generated Location: (360:12,0 [41] )
|using Microsoft.AspNetCore.Components.Web|

Source Location: (75:1,31 [9] x:\dir\subdir\Test\TestComponent.cshtml)
|() => { }|
Generated Location: (1217:32,31 [9] )
|() => { }|

Source Location: (98:1,54 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|x|
Generated Location: (1059:25,54 [1] )
Generated Location: (1522:41,54 [1] )
|x|

Loading

0 comments on commit 8848273

Please sign in to comment.