Skip to content

Commit 74cc5fc

Browse files
committed
Address feedback
1 parent b8daab0 commit 74cc5fc

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/DiagnosticDescriptors.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,6 @@ internal static class DiagnosticDescriptors
231231
"Usage",
232232
DiagnosticSeverity.Info,
233233
isEnabledByDefault: true,
234-
helpLinkUri: "https://aka.ms/aspnet/analyzers");
234+
helpLinkUri: "https://aka.ms/aspnet/analyzers",
235+
customTags: WellKnownDiagnosticTags.Unnecessary);
235236
}

src/Framework/AspNetCoreAnalyzers/src/Analyzers/Resources.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@
322322
<value>[Authorize] overridden by [AllowAnonymous] from farther away</value>
323323
</data>
324324
<data name="Analyzer_PublicPartialProgramClass_Message" xml:space="preserve">
325-
<value>Using public partial class Program { } to make generated Program class public is no longer required. See https://aka.ms/aspnetcore-warnings/ASP0027 for more details.</value>
325+
<value>Using public partial class Program { } to make the generated Program class public is no longer required in ASP.NET Core apps. See https://aka.ms/aspnetcore-warnings/ASP0027 for more details.</value>
326326
</data>
327327
<data name="Analyzer_PublicPartialProgramClass_Title" xml:space="preserve">
328-
<value>Explicit class declaration not required</value>
328+
<value>Unnecessary public Program class declaration</value>
329329
</data>
330-
</root>
330+
</root>

src/Framework/AspNetCoreAnalyzers/src/Analyzers/WebApplicationBuilder/PublicPartialProgramClassAnalyzer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Immutable;
5+
using System.Linq;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CSharp;
78
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -23,7 +24,9 @@ public override void Initialize(AnalysisContext context)
2324
var syntaxNode = context.Node;
2425
if (IsPublicPartialClassProgram(syntaxNode))
2526
{
26-
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.PublicPartialProgramClassNotRequired, syntaxNode.GetLocation()));
27+
context.ReportDiagnostic(Diagnostic.Create(
28+
DiagnosticDescriptors.PublicPartialProgramClassNotRequired,
29+
syntaxNode.GetLocation()));
2730
}
2831
}, SyntaxKind.ClassDeclaration);
2932
}
@@ -34,6 +37,8 @@ private static bool IsPublicPartialClassProgram(SyntaxNode syntaxNode)
3437
&& modifiers is { Count: > 1 }
3538
&& modifiers.Any(SyntaxKind.PublicKeyword)
3639
&& modifiers.Any(SyntaxKind.PartialKeyword)
37-
&& classDeclaration is { Identifier.ValueText: "Program" };
40+
&& classDeclaration is { Identifier.ValueText: "Program" }
41+
&& classDeclaration.Parent is CompilationUnitSyntax parentNode
42+
&& parentNode.DescendantNodes().Count() > 1;
3843
}
3944
}

src/Framework/AspNetCoreAnalyzers/src/CodeFixes/PublicPartialProgramClassFixer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
2525
foreach (var diagnostic in context.Diagnostics)
2626
{
2727
context.RegisterCodeFix(
28-
CodeAction.Create("Fix unnecessary public partial class Program",
28+
CodeAction.Create("Remove unnecessary public partial class Program declaration",
2929
async cancellationToken =>
3030
{
3131
var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);
@@ -41,7 +41,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
4141
{
4242
return context.Document;
4343
}
44-
editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepNoTrivia);
44+
editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia);
4545
return editor.GetChangedDocument();
4646
},
4747
equivalenceKey: DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id),

src/Framework/AspNetCoreAnalyzers/test/WebApplicationBuilder/PublicPartialProgramClassTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@ public async Task RemovesDeclarationIfItIsFound(string declarationStyle)
6060
6161
app.Run();
6262
63+
64+
""";
65+
66+
// Assert
67+
await VerifyCS.VerifyCodeFixAsync(source, [diagnostic], fixedSource);
68+
}
69+
70+
[Fact]
71+
public async Task RemovesDeclarationIfItIsFound_WithLeadingTrivia()
72+
{
73+
// Arrange
74+
var source = """
75+
using Microsoft.AspNetCore.Builder;
76+
77+
var app = WebApplication.Create();
78+
79+
app.MapGet("/", () => "Hello, World!");
80+
81+
app.Run();
82+
83+
// This is a test
84+
85+
{|#0:public partial class Program;|}
86+
""";
87+
88+
var diagnostic = new DiagnosticResult(DiagnosticDescriptors.PublicPartialProgramClassNotRequired)
89+
.WithLocation(0);
90+
91+
var fixedSource = """
92+
using Microsoft.AspNetCore.Builder;
93+
94+
var app = WebApplication.Create();
95+
96+
app.MapGet("/", () => "Hello, World!");
97+
98+
app.Run();
99+
100+
// This is a test
101+
102+
63103
""";
64104

65105
// Assert

0 commit comments

Comments
 (0)