-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide analyzer for removing unneeded public partial class Program (#…
…58482) * Provide analyzer for removing unneeded public partial class Program * Update tests and fix async call * Address feedback * Add test for public partial class with members * Reorganize checks and add tests
- Loading branch information
1 parent
a74dc5a
commit 0853101
Showing
5 changed files
with
405 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...NetCoreAnalyzers/src/Analyzers/WebApplicationBuilder/PublicPartialProgramClassAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class PublicPartialProgramClassAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptors.PublicPartialProgramClassNotRequired); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSyntaxNodeAction(context => | ||
{ | ||
var syntaxNode = context.Node; | ||
if (IsPublicPartialClassProgram(syntaxNode)) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.PublicPartialProgramClassNotRequired, | ||
syntaxNode.GetLocation())); | ||
} | ||
}, SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
private static bool IsPublicPartialClassProgram(SyntaxNode syntaxNode) | ||
{ | ||
return syntaxNode is ClassDeclarationSyntax { Modifiers: { } modifiers } classDeclaration | ||
&& classDeclaration.Parent is CompilationUnitSyntax parentNode | ||
&& classDeclaration is { Identifier.ValueText: "Program" } | ||
&& (classDeclaration.Members == null || classDeclaration.Members.Count == 0) // Skip non-empty declarations | ||
&& modifiers is { Count: > 1 } | ||
&& modifiers.Any(SyntaxKind.PublicKeyword) | ||
&& modifiers.Any(SyntaxKind.PartialKeyword) | ||
&& parentNode.DescendantNodes().Count() > 1; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Framework/AspNetCoreAnalyzers/src/CodeFixes/PublicPartialProgramClassFixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Analyzers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
|
||
namespace Microsoft.AspNetCore.Fixers; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class PublicPartialProgramClassFixer : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = [DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id]; | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
foreach (var diagnostic in context.Diagnostics) | ||
{ | ||
context.RegisterCodeFix( | ||
CodeAction.Create("Remove unnecessary public partial class Program declaration", | ||
async cancellationToken => | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false); | ||
var root = await context.Document.GetSyntaxRootAsync(cancellationToken); | ||
if (root is null) | ||
{ | ||
return context.Document; | ||
} | ||
|
||
var classDeclaration = root.FindNode(diagnostic.Location.SourceSpan) | ||
.FirstAncestorOrSelf<ClassDeclarationSyntax>(); | ||
if (classDeclaration is null) | ||
{ | ||
return context.Document; | ||
} | ||
editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia); | ||
return editor.GetChangedDocument(); | ||
}, | ||
equivalenceKey: DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id), | ||
diagnostic); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
Oops, something went wrong.