Skip to content

Commit fd61d29

Browse files
committed
Add warning for unnecessary at
1 parent 46d7dca commit fd61d29

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Components/ComponentLoweringPass.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ private static ComponentIntermediateNode RewriteAsComponent(TagHelperIntermediat
163163
}
164164

165165
ValidateRequiredAttributes(node, tagHelper, component);
166+
WarnForUnnecessaryAt(component);
166167

167168
return component;
168169
}
@@ -213,6 +214,20 @@ static bool IsPresentAsAttribute(string attributeName, ComponentIntermediateNode
213214
}
214215
}
215216

217+
private static void WarnForUnnecessaryAt(ComponentIntermediateNode component)
218+
{
219+
foreach (var attribute in component.Attributes)
220+
{
221+
// IntParam="@x" has unnecessary `@`, can just use IntParam="x" -> warn
222+
// StrParam="@x" is different than StrParam="x" -> don't warn
223+
if (!attribute.BoundAttribute.IsStringProperty &&
224+
attribute.Children is [CSharpExpressionIntermediateNode])
225+
{
226+
attribute.Diagnostics.Add(RazorDiagnosticFactory.CreateComponentParameter_UnnecessaryAt(attribute.Source));
227+
}
228+
}
229+
}
230+
216231
private static MarkupElementIntermediateNode RewriteAsElement(TagHelperIntermediateNode node)
217232
{
218233
var result = new MarkupElementIntermediateNode()

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorDiagnosticFactory.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,14 @@ public static RazorDiagnostic CreateTagHelper_InconsistentTagStructure(SourceSpa
480480
public static RazorDiagnostic CreateComponent_EditorRequiredParameterNotSpecified(SourceSpan? location, string tagName, string parameterName)
481481
=> RazorDiagnostic.Create(Component_EditorRequiredParameterNotSpecified, location, tagName, parameterName);
482482

483+
internal static readonly RazorDiagnosticDescriptor ComponentParameter_UnnecessaryAt =
484+
new($"{DiagnosticPrefix}2013",
485+
Resources.ComponentParameter_UnnecessaryAt,
486+
RazorDiagnosticSeverity.Warning);
487+
488+
public static RazorDiagnostic CreateComponentParameter_UnnecessaryAt(SourceSpan? location)
489+
=> RazorDiagnostic.Create(ComponentParameter_UnnecessaryAt, location);
490+
483491
#endregion
484492

485493
#region TagHelper Errors

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@
574574
<data name="Component_EditorRequiredParameterNotSpecified" xml:space="preserve">
575575
<value>Component '{0}' expects a value for the parameter '{1}', but a value may not have been provided.</value>
576576
</data>
577+
<data name="ComponentParameter_UnnecessaryAt" xml:space="preserve">
578+
<value>The '@' prefix is not necessary for component parameters whose type is not string.</value>
579+
</data>
577580
<data name="An_item_with_the_same_key_has_already_been_added_Key_0" xml:space="preserve">
578581
<value>An item with the same key has already been added. Key: {0}</value>
579582
</data>

src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,13 @@ public async Task ComponentParameter_UnnecessaryAt()
309309
var result = RunGenerator(compilation!, ref driver, out compilation);
310310

311311
// Assert
312-
Assert.Empty(result.Diagnostics);
312+
result.Diagnostics.VerifyRazor(project,
313+
// Shared/Component1.razor(6,23): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
314+
// <Component2 IntParam="@(43)" StrParam="@hi" />
315+
Diagnostic("RZ2013", "@(43)").WithLocation(6, 23),
316+
// Shared/Component1.razor(7,23): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
317+
// <Component2 IntParam="@x" StrParam="@("lit")" />
318+
Diagnostic("RZ2013", "@x").WithLocation(7, 23));
313319
Assert.Equal(3, result.GeneratedSources.Length);
314320
await VerifyRazorPageMatchesBaselineAsync(compilation, "Views_Home_Index");
315321
}

0 commit comments

Comments
 (0)