Skip to content

Commit

Permalink
Report warning at the @ only
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed May 6, 2024
1 parent fd61d29 commit fa4d3e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ private static void WarnForUnnecessaryAt(ComponentIntermediateNode component)
// IntParam="@x" has unnecessary `@`, can just use IntParam="x" -> warn
// StrParam="@x" is different than StrParam="x" -> don't warn
if (!attribute.BoundAttribute.IsStringProperty &&
attribute.Source is { } originalSource &&
attribute.Children is [CSharpExpressionIntermediateNode])
{
attribute.Diagnostics.Add(RazorDiagnosticFactory.CreateComponentParameter_UnnecessaryAt(attribute.Source));
var source = originalSource.With(length: 1, endCharacterIndex: originalSource.CharacterIndex + 1);
attribute.Diagnostics.Add(RazorDiagnosticFactory.CreateComponentParameter_UnnecessaryAt(source));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,21 @@ public async Task ComponentParameter_UnnecessaryAt()
var x = 21;
}
<Component2 IntParam="42" StrParam="hi" />
<Component2 IntParam="@(43)" StrParam="@hi" />
<Component2 IntParam="@x" StrParam="@("lit")" />
<Component2
IntParam="@(43)"
StrParam="@hi" />
<Component2
IntParam="@x"
StrParam="@("lit")" />
<Component2 IntParam="x * 3" StrParam="@(hi + "hey")" />
<Component2 IntParam=@x />
""",
["Shared/Component2.razor"] = """
I: @(IntParam + 1), S: @StrParam.ToUpperInvariant()
I: @(IntParam + 1), S: @StrParam?.ToUpperInvariant()
@code {
[Parameter] public int IntParam { get; set; }
[Parameter] public required string StrParam { get; set; }
[Parameter] public string? StrParam { get; set; }
}
"""
});
Expand All @@ -310,12 +315,15 @@ public async Task ComponentParameter_UnnecessaryAt()

// Assert
result.Diagnostics.VerifyRazor(project,
// Shared/Component1.razor(6,23): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
// <Component2 IntParam="@(43)" StrParam="@hi" />
Diagnostic("RZ2013", "@(43)").WithLocation(6, 23),
// Shared/Component1.razor(7,23): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
// <Component2 IntParam="@x" StrParam="@("lit")" />
Diagnostic("RZ2013", "@x").WithLocation(7, 23));
// Shared/Component1.razor(7,15): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
// IntParam="@(43)"
Diagnostic("RZ2013", "@").WithLocation(7, 15),
// Shared/Component1.razor(10,15): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
// IntParam="@x"
Diagnostic("RZ2013", "@").WithLocation(10, 15),
// Shared/Component1.razor(13,22): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
// <Component2 IntParam=@x />
Diagnostic("RZ2013", "@").WithLocation(13, 22));
Assert.Equal(3, result.GeneratedSources.Length);
await VerifyRazorPageMatchesBaselineAsync(compilation, "Views_Home_Index");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
I: 43, S: HI
I: 43, S: HI
I: 44, S: STR
I: 22, S: LIT
I: 64, S: STRHEY
I: 64, S: STRHEY
I: 22, S:

0 comments on commit fa4d3e6

Please sign in to comment.