Skip to content

Commit 79c05c8

Browse files
committed
Improve SA1407 and SA1408 fix all provider
1 parent 842a1ee commit 79c05c8

1 file changed

Lines changed: 6 additions & 11 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1407SA1408FixAllProvider.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.MaintainabilityRules
55
{
6+
using System.Collections.Generic;
67
using System.Threading.Tasks;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CodeFixes;
@@ -13,8 +14,6 @@ namespace StyleCop.Analyzers.MaintainabilityRules
1314

1415
internal sealed class SA1407SA1408FixAllProvider : DocumentBasedFixAllProvider
1516
{
16-
private static readonly SyntaxAnnotation NeedsParenthesisAnnotation = new SyntaxAnnotation("StyleCop.NeedsParenthesis");
17-
1817
protected override string CodeActionTitle => MaintainabilityResources.SA1407SA1408CodeFix;
1918

2019
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
@@ -25,13 +24,10 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
2524
return null;
2625
}
2726

28-
var newDocument = document;
27+
var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
2928

30-
var root = await newDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
29+
List<SyntaxNode> nodes = new List<SyntaxNode>();
3130

32-
// First annotate all expressions that need parenthesis with a temporary annotation.
33-
// With this annotation we can find the nodes that need parenthesis even if
34-
// the source span changes.
3531
foreach (var diagnostic in diagnostics)
3632
{
3733
SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan);
@@ -40,10 +36,10 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
4036
continue;
4137
}
4238

43-
root = root.ReplaceNode(node, node.WithAdditionalAnnotations(NeedsParenthesisAnnotation));
39+
nodes.Add(node);
4440
}
4541

46-
return root.ReplaceNodes(root.GetAnnotatedNodes(NeedsParenthesisAnnotation), this.AddParentheses);
42+
return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => this.AddParentheses(originalNode, rewrittenNode));
4743
}
4844

4945
private SyntaxNode AddParentheses(SyntaxNode originalNode, SyntaxNode rewrittenNode)
@@ -55,8 +51,7 @@ private SyntaxNode AddParentheses(SyntaxNode originalNode, SyntaxNode rewrittenN
5551
}
5652

5753
BinaryExpressionSyntax trimmedSyntax = syntax
58-
.WithoutTrivia()
59-
.WithoutAnnotations(NeedsParenthesisAnnotation.Kind);
54+
.WithoutTrivia();
6055

6156
return SyntaxFactory.ParenthesizedExpression(trimmedSyntax)
6257
.WithTriviaFrom(syntax)

0 commit comments

Comments
 (0)