Skip to content

Commit 8af10d5

Browse files
authored
cleanup: simplify the edit-action API (#283)
1 parent 37c27af commit 8af10d5

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/FluentAssertions.Analyzers/Tips/DocumentEditorUtils.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static CreateChangedDocument RenameMethodToSubjectShouldAssertion(IInvoca
1818
var invocationExpression = (InvocationExpressionSyntax)invocation.Syntax;
1919

2020
return async ctx => await RewriteExpression(invocationExpression, [
21-
..Array.ConvertAll(argumentsToRemove, arg => new RemoveNodeAction(invocationExpression.ArgumentList.Arguments[arg])),
22-
new SubjectShouldAssertionAction(subjectIndex, newName)
21+
..Array.ConvertAll(argumentsToRemove, arg => EditAction.RemoveNode(invocationExpression.ArgumentList.Arguments[arg])),
22+
EditAction.SubjectShouldAssertion(subjectIndex, newName)
2323
], context, ctx);
2424
}
2525

@@ -30,8 +30,8 @@ public static CreateChangedDocument RenameMethodToSubjectShouldGenericAssertion(
3030
var invocationExpression = (InvocationExpressionSyntax)invocation.Syntax;
3131

3232
return async ctx => await RewriteExpression(invocationExpression, [
33-
..Array.ConvertAll(argumentsToRemove, arg => new RemoveNodeAction(invocationExpression.ArgumentList.Arguments[arg])),
34-
new SubjectShouldGenericAssertionAction(subjectIndex, newName, genericTypes)
33+
..Array.ConvertAll(argumentsToRemove, arg => EditAction.RemoveNode(invocationExpression.ArgumentList.Arguments[arg])),
34+
EditAction.SubjectShouldGenericAssertion(subjectIndex, newName, genericTypes)
3535
], context, ctx);
3636
}
3737

@@ -40,21 +40,20 @@ public static CreateChangedDocument RenameMethodToSubjectShouldAssertionWithOpti
4040
var invocationExpression = (InvocationExpressionSyntax)invocation.Syntax;
4141

4242
return async ctx => await RewriteExpression(invocationExpression, [
43-
new SubjectShouldAssertionAction(subjectIndex, newName),
44-
new CreateEquivalencyAssertionOptionsLambda(optionsIndex)
43+
EditAction.SubjectShouldAssertion(subjectIndex, newName),
44+
EditAction.CreateEquivalencyAssertionOptionsLambda(optionsIndex)
4545
], context, ctx);
4646
}
4747

48-
private static async Task<Document> RewriteExpression(InvocationExpressionSyntax invocationExpression, IEditAction[] actions, CodeFixContext context, CancellationToken cancellationToken)
48+
private static async Task<Document> RewriteExpression(InvocationExpressionSyntax invocationExpression, Action<DocumentEditor, InvocationExpressionSyntax>[] actions, CodeFixContext context, CancellationToken cancellationToken)
4949
{
5050
var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken);
5151

5252
foreach (var action in actions)
5353
{
54-
action.Apply(editor, invocationExpression);
54+
action(editor, invocationExpression);
5555
}
5656

5757
return editor.GetChangedDocument();
5858
}
5959
}
60-

src/FluentAssertions.Analyzers/Tips/Editing/CreateEquivalencyAssertionOptionsLambda.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace FluentAssertions.Analyzers;
55

6-
public class CreateEquivalencyAssertionOptionsLambda(int argumentIndex) : IEditAction
6+
public class CreateEquivalencyAssertionOptionsLambdaAction(int argumentIndex) : IEditAction
77
{
88
public void Apply(DocumentEditor editor, InvocationExpressionSyntax invocationExpression)
99
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Microsoft.CodeAnalysis.Editing;
6+
7+
namespace FluentAssertions.Analyzers;
8+
9+
public static class EditAction
10+
{
11+
public static Action<DocumentEditor, InvocationExpressionSyntax> RemoveNode(SyntaxNode node)
12+
=> (editor, invocationExpression) => editor.RemoveNode(node);
13+
14+
public static Action<DocumentEditor, InvocationExpressionSyntax> SubjectShouldAssertion(int argumentIndex, string assertion)
15+
=> (editor, invocationExpression) => new SubjectShouldAssertionAction(argumentIndex, assertion).Apply(editor, invocationExpression);
16+
17+
public static Action<DocumentEditor, InvocationExpressionSyntax> SubjectShouldGenericAssertion(int argumentIndex, string assertion, ImmutableArray<ITypeSymbol> genericTypes)
18+
=> (editor, invocationExpression) => new SubjectShouldGenericAssertionAction(argumentIndex, assertion, genericTypes).Apply(editor, invocationExpression);
19+
20+
public static Action<DocumentEditor, InvocationExpressionSyntax> CreateEquivalencyAssertionOptionsLambda(int optionsIndex)
21+
=> (editor, invocationExpression) => new CreateEquivalencyAssertionOptionsLambdaAction(optionsIndex).Apply(editor, invocationExpression);
22+
}

0 commit comments

Comments
 (0)