Skip to content

Commit fa4d3e6

Browse files
committed
Report warning at the @ only
1 parent fd61d29 commit fa4d3e6

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ private static void WarnForUnnecessaryAt(ComponentIntermediateNode component)
221221
// IntParam="@x" has unnecessary `@`, can just use IntParam="x" -> warn
222222
// StrParam="@x" is different than StrParam="x" -> don't warn
223223
if (!attribute.BoundAttribute.IsStringProperty &&
224+
attribute.Source is { } originalSource &&
224225
attribute.Children is [CSharpExpressionIntermediateNode])
225226
{
226-
attribute.Diagnostics.Add(RazorDiagnosticFactory.CreateComponentParameter_UnnecessaryAt(attribute.Source));
227+
var source = originalSource.With(length: 1, endCharacterIndex: originalSource.CharacterIndex + 1);
228+
attribute.Diagnostics.Add(RazorDiagnosticFactory.CreateComponentParameter_UnnecessaryAt(source));
227229
}
228230
}
229231
}

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,21 @@ public async Task ComponentParameter_UnnecessaryAt()
289289
var x = 21;
290290
}
291291
<Component2 IntParam="42" StrParam="hi" />
292-
<Component2 IntParam="@(43)" StrParam="@hi" />
293-
<Component2 IntParam="@x" StrParam="@("lit")" />
292+
<Component2
293+
IntParam="@(43)"
294+
StrParam="@hi" />
295+
<Component2
296+
IntParam="@x"
297+
StrParam="@("lit")" />
294298
<Component2 IntParam="x * 3" StrParam="@(hi + "hey")" />
299+
<Component2 IntParam=@x />
295300
""",
296301
["Shared/Component2.razor"] = """
297-
I: @(IntParam + 1), S: @StrParam.ToUpperInvariant()
302+
I: @(IntParam + 1), S: @StrParam?.ToUpperInvariant()
298303
299304
@code {
300305
[Parameter] public int IntParam { get; set; }
301-
[Parameter] public required string StrParam { get; set; }
306+
[Parameter] public string? StrParam { get; set; }
302307
}
303308
"""
304309
});
@@ -310,12 +315,15 @@ public async Task ComponentParameter_UnnecessaryAt()
310315

311316
// Assert
312317
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));
318+
// Shared/Component1.razor(7,15): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
319+
// IntParam="@(43)"
320+
Diagnostic("RZ2013", "@").WithLocation(7, 15),
321+
// Shared/Component1.razor(10,15): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
322+
// IntParam="@x"
323+
Diagnostic("RZ2013", "@").WithLocation(10, 15),
324+
// Shared/Component1.razor(13,22): warning RZ2013: The '@' prefix is not necessary for component parameters whose type is not string.
325+
// <Component2 IntParam=@x />
326+
Diagnostic("RZ2013", "@").WithLocation(13, 22));
319327
Assert.Equal(3, result.GeneratedSources.Length);
320328
await VerifyRazorPageMatchesBaselineAsync(compilation, "Views_Home_Index");
321329
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
I: 43, S: HI
1+
I: 43, S: HI
22
I: 44, S: STR
33
I: 22, S: LIT
4-
I: 64, S: STRHEY
4+
I: 64, S: STRHEY
5+
I: 22, S:

0 commit comments

Comments
 (0)