Extended code analyzers (#293)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2025-02-25 19:58:17 +01:00
1 parent 99dac520cb
commit 5e3b632eee
22 files changed
+1099 -50

No files matched your search

@@ -0,0 +1,54 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
namespace SourceCodeRules.UsageCodeFixes;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(EmptyStringCodeFixProvider)), Shared]
public class EmptyStringCodeFixProvider : CodeFixProvider
{
private const string TITLE = """Replace "" with string.Empty""";
public sealed override ImmutableArray<string> FixableDiagnosticIds => [Identifier.EMPTY_STRING_ANALYZER];
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
if(root is null)
return;
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
if (root.FindToken(diagnosticSpan.Start).Parent is not LiteralExpressionSyntax emptyStringLiteral)
return;
context.RegisterCodeFix(
CodeAction.Create(
title: TITLE,
createChangedDocument: c => ReplaceWithStringEmpty(context.Document, emptyStringLiteral, c),
equivalenceKey: TITLE),
diagnostic);
}
private static async Task<Document> ReplaceWithStringEmpty(Document document, LiteralExpressionSyntax emptyStringLiteral, CancellationToken cancellationToken)
{
var stringEmptyExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("string"), SyntaxFactory.IdentifierName("Empty")).WithAdditionalAnnotations(Formatter.Annotation);
var root = await document.GetSyntaxRootAsync(cancellationToken);
if (root is null)
return document;
var newRoot = root.ReplaceNode(emptyStringLiteral, stringEmptyExpression);
return document.WithSyntaxRoot(newRoot);
}
}
@@ -0,0 +1,71 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace SourceCodeRules.UsageCodeFixes;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ThisUsageCodeFixProvider)), Shared]
public class ThisUsageCodeFixProvider : CodeFixProvider
{
private const string TITLE = "Add 'this.' prefix";
public sealed override ImmutableArray<string> FixableDiagnosticIds => [Identifier.THIS_USAGE_ANALYZER];
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
if (root == null)
return;
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var node = root.FindNode(diagnosticSpan);
if (node is IdentifierNameSyntax identifierNode)
{
context.RegisterCodeFix(
CodeAction.Create(
title: TITLE,
createChangedDocument: c => AddThisPrefixAsync(context.Document, identifierNode, c),
equivalenceKey: nameof(ThisUsageCodeFixProvider)),
diagnostic);
}
else if (node is GenericNameSyntax genericNameNode)
{
context.RegisterCodeFix(
CodeAction.Create(
title: TITLE,
createChangedDocument: c => AddThisPrefixAsync(context.Document, genericNameNode, c),
equivalenceKey: nameof(ThisUsageCodeFixProvider)),
diagnostic);
}
}
private static async Task<Document> AddThisPrefixAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
if (root == null)
return document;
var thisExpression = SyntaxFactory.ThisExpression();
var leadingTrivia = node.GetLeadingTrivia();
var memberAccessExpression = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
thisExpression.WithLeadingTrivia(leadingTrivia),
((SimpleNameSyntax)node).WithLeadingTrivia(SyntaxTriviaList.Empty))
.WithTrailingTrivia(node.GetTrailingTrivia());
var newRoot = root.ReplaceNode(node, memberAccessExpression);
return document.WithSyntaxRoot(newRoot);
}
}