Skip to content

Commit

Permalink
Start on Men017 fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Mar 28, 2024
1 parent 3c13e2e commit a4259fc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace Menees.Analyzers.CodeFixes;

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;

[Shared]
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(Men017RemoveUnusedPrivateSetterFixer))]
public sealed class Men017RemoveUnusedPrivateSetterFixer : CodeFixProvider
{
#region Private Data Members

private static readonly ImmutableArray<string> FixableDiagnostics
= ImmutableArray.Create(Men017RemoveUnusedPrivateSetter.DiagnosticId);

#endregion

#region Public Properties

public sealed override ImmutableArray<string> FixableDiagnosticIds => FixableDiagnostics;

#endregion

#region Public Methods

public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (Diagnostic diagnostic in context.Diagnostics.Where(d => FixableDiagnostics.Contains(d.Id)))
{
context.RegisterCodeFix(
CodeAction.Create(
Resources.Men017CodeFix,
cancellation => GetTransformedDocumentAsync(context.Document, diagnostic, cancellation),
equivalenceKey: nameof(Men017RemoveUnusedPrivateSetterFixer)),
diagnostic);
}

return Task.FromResult(true);
}

#endregion

#region Private Methods

private async Task<Document> GetTransformedDocumentAsync(
Document document,
Diagnostic diagnostic,
CancellationToken cancellation)
{
Document result = document;

SyntaxNode? root = await document.GetSyntaxRootAsync(cancellation);
if (root != null)
{
AccessorDeclarationSyntax? accessorDeclaration = root.FindNode(diagnostic.Location.SourceSpan)
?.FirstAncestorOrSelf<AccessorDeclarationSyntax>();
AccessorListSyntax? accessorList = accessorDeclaration?.FirstAncestorOrSelf<AccessorListSyntax>();

if (accessorList != null && accessorDeclaration != null)
{
AccessorListSyntax? newAccessorList = accessorList
.RemoveNode(accessorDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia)?
.WithAdditionalAnnotations(Formatter.Annotation);
if (newAccessorList != null)
{
SyntaxNode newRoot = root.ReplaceNode(accessorList, newAccessorList);
result = document.WithSyntaxRoot(newRoot);
}
}
}

return result;
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
<Using Include="Microsoft.CodeAnalysis.CodeFixes" />
<Using Include="Microsoft.CodeAnalysis.Formatting" />
<Using Include="Microsoft.CodeAnalysis.Options" />
</ItemGroup>
</ItemGroup>
</Project>
11 changes: 10 additions & 1 deletion src/Menees.Analyzers.CodeFixes/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Menees.Analyzers.CodeFixes/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@
<data name="Men015CodeFix" xml:space="preserve">
<value>Replace with preferred term</value>
</data>
<data name="Men017CodeFix" xml:space="preserve">
<value>Remove unused private setter</value>
</data>
</root>
2 changes: 1 addition & 1 deletion src/Menees.Analyzers/Men017RemoveUnusedPrivateSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static void HandleSetter(SyntaxNodeAnalysisContext context)
// because it will use property initializer syntax.
if (canRemove)
{
Diagnostic diagnostic = Diagnostic.Create(Rule, accessorDeclaration.GetLocation());
Diagnostic diagnostic = Diagnostic.Create(Rule, accessorDeclaration.GetLocation(), propertyDeclaration.Identifier);
context.ReportDiagnostic(diagnostic);
}
}
Expand Down

0 comments on commit a4259fc

Please sign in to comment.