Skip to content

Commit

Permalink
Centralize all CodeAction.Create calls to centralize the RS1035 disab…
Browse files Browse the repository at this point in the history
…lement
  • Loading branch information
bradwilson committed Feb 13, 2024
1 parent 1e0dae6 commit f6c256a
Show file tree
Hide file tree
Showing 26 changed files with 159 additions and 134 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,3 @@ dotnet_diagnostic.CA1720.severity = none # Identifier contains type name
dotnet_diagnostic.CA1724.severity = none # Type names should not match namespaces
dotnet_diagnostic.CA1859.severity = none # Use concrete types when possible for improved performance
dotnet_diagnostic.IDE1006.severity = none # Naming rule violation
dotnet_diagnostic.RS1035.severity = none # The symbol 'CultureInfo.CurrentCulture' is banned for use by analyzers (unresolvable conflict with CA1305, per https://github.com/dotnet/roslyn-analyzers/issues/7086)
45 changes: 0 additions & 45 deletions src/xunit.analyzers.fixes/Utility/UseDifferentMethodCodeAction.cs

This file was deleted.

92 changes: 92 additions & 0 deletions src/xunit.analyzers.fixes/Utility/XunitCodeAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;

namespace Microsoft.CodeAnalysis.CodeActions;

internal static class XunitCodeAction
{
public static CodeAction Create(
Func<CancellationToken, Task<Document>> createChangedDocument,
string equivalenceKey,
string title) =>
CodeAction.Create(
title,
createChangedDocument,
equivalenceKey
);

#pragma warning disable RS1035 // The prohibiton on CultureInfo.CurrentCulture does not apply to fixers

public static CodeAction Create(
Func<CancellationToken, Task<Document>> createChangedDocument,
string equivalenceKey,
string titleFormat,
object? arg0) =>
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, titleFormat, arg0),
createChangedDocument,
equivalenceKey
);

public static CodeAction Create(
Func<CancellationToken, Task<Document>> createChangedDocument,
string equivalenceKey,
string titleFormat,
object? arg0,
object? arg1) =>
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, titleFormat, arg0, arg1),
createChangedDocument,
equivalenceKey
);

public static CodeAction Create(
Func<CancellationToken, Task<Document>> createChangedDocument,
string equivalenceKey,
string titleFormat,
object? arg0,
object? arg1,
object? arg2) =>
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, titleFormat, arg0, arg1, arg2),
createChangedDocument,
equivalenceKey
);

public static CodeAction Create(
Func<CancellationToken, Task<Document>> createChangedDocument,
string equivalenceKey,
string titleFormat,
params object?[] args) =>
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, titleFormat, args),
createChangedDocument,
equivalenceKey
);

#pragma warning restore RS1035

public static CodeAction UseDifferentAssertMethod(
string equivalenceKey,
Document document,
InvocationExpressionSyntax invocation,
string replacementMethod) =>
CodeAction.Create(
"Use Assert." + replacementMethod,
async ct =>
{
var editor = await DocumentEditor.CreateAsync(document, ct).ConfigureAwait(false);

if (invocation.Expression is MemberAccessExpressionSyntax memberAccess)
if (editor.Generator.IdentifierName(replacementMethod) is SimpleNameSyntax replacementNameSyntax)
editor.ReplaceNode(memberAccess, memberAccess.WithName(replacementNameSyntax));

return editor.GetChangedDocument();
},
equivalenceKey
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -68,15 +67,15 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var replaceConfigureAwaitNode = SyntaxFactory.ParseExpression(replaceConfigureAwaitText);

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Replace ConfigureAwait({0}) with ConfigureAwait({1})", original, replacement),
XunitCodeAction.Create(
async ct =>
{
var editor = await DocumentEditor.CreateAsync(context.Document, ct).ConfigureAwait(false);
editor.ReplaceNode(syntaxNode, replaceConfigureAwaitNode);
return editor.GetChangedDocument();
},
Key_ReplaceArgumentValue
Key_ReplaceArgumentValue,
"Replace ConfigureAwait({0}) with ConfigureAwait({1})", original, replacement
),
context.Diagnostics
);
Expand Down
7 changes: 3 additions & 4 deletions src/xunit.analyzers.fixes/X1000/RemoveMethodParameterFix.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
Expand Down Expand Up @@ -33,10 +32,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var parameterName = parameter.Identifier.Text;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Remove parameter '{0}'", parameterName),
XunitCodeAction.Create(
ct => context.Document.RemoveNode(parameter, ct),
Key_RemoveParameter
Key_RemoveParameter,
"Remove parameter '{0}'", parameterName
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -44,15 +43,15 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Generate constructor '{0}({1})'", testClassName, tFixtureName),
XunitCodeAction.Create(
ct => context.Document.AddConstructor(
classDeclaration,
typeDisplayName: tFixtureDisplayName,
typeName: tFixtureName,
ct
),
Key_GenerateConstructor
Key_GenerateConstructor,
"Generate constructor '{0}({1})'", testClassName, tFixtureName
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var simpleName = GetAttributeSimpleName(attributeType);

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Keep '{0}' attribute", simpleName),
XunitCodeAction.Create(
ct => RemoveAttributes(context.Document, methodDeclaration, attributeTypes, attributeType, ct),
Key_KeepAttribute(simpleName)
Key_KeepAttribute(simpleName),
"Keep '{0}' attribute", simpleName
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
Expand Down Expand Up @@ -30,10 +29,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var parameterName = parameter.Identifier.Text;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Remove parameter '{0}' default", parameterName),
XunitCodeAction.Create(
ct => context.Document.RemoveNode(parameter.Default, ct),
Key_RemoveParameterDefault
Key_RemoveParameterDefault,
"Remove parameter '{0}' default", parameterName
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,10 +41,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.Create(
ct => UseContainsCheck(context.Document, invocation, replacement, ct),
Key_UseAlternateAssert
Key_UseAlternateAssert,
"Use Assert.{0}", replacement
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,10 +41,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.Create(
ct => UseContainsCheck(context.Document, invocation, replacement, ct),
Key_UseAlternateAssert
Key_UseAlternateAssert,
"Use Assert.{0}", replacement
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,10 +42,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use precision {0}", replacementInt),
XunitCodeAction.Create(
ct => UseRecommendedPrecision(context.Document, precisionArgument, replacementInt, ct),
Key_UsePrecision
Key_UsePrecision,
"Use precision {0}", replacementInt
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -45,10 +44,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (invocation.Expression is MemberAccessExpressionSyntax)
context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.Create(
ct => UseBoolCheckAsync(context.Document, invocation, replacement, ct),
Key_UseAlternateAssert
Key_UseAlternateAssert,
"Use Assert.{0}", replacement
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -44,10 +43,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.Create(
ct => UseCollectionSizeAssertionAsync(context.Document, invocation, replacement, ct),
Key_UseAlternateAssert
Key_UseAlternateAssert,
"Use Assert.{0}", replacement
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,10 +42,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (invocation.Expression is MemberAccessExpressionSyntax)
context.RegisterCodeFix(
CodeAction.Create(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.Create(
ct => UseNullCheckAsync(context.Document, invocation, replacement, ct),
Key_UseAlternateAssert
Key_UseAlternateAssert,
"Use Assert.{0}", replacement
),
context.Diagnostics
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand Down Expand Up @@ -37,8 +37,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (invocation.Expression is MemberAccessExpressionSyntax)
context.RegisterCodeFix(
new UseDifferentMethodCodeAction(
string.Format(CultureInfo.CurrentCulture, "Use Assert.{0}", replacement),
XunitCodeAction.UseDifferentAssertMethod(
Key_UseAlternateAssert,
context.Document,
invocation,
Expand Down
Loading

0 comments on commit f6c256a

Please sign in to comment.