33
44namespace 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,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
2524 return null ;
2625 }
2726
28- var newDocument = document ;
29-
30- var root = await newDocument . GetSyntaxRootAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
27+ var root = await document . GetSyntaxRootAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
3128
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.
29+ List < SyntaxNode > nodes = new List < SyntaxNode > ( ) ;
3530 foreach ( var diagnostic in diagnostics )
3631 {
3732 SyntaxNode node = root . FindNode ( diagnostic . Location . SourceSpan ) ;
@@ -40,23 +35,21 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
4035 continue ;
4136 }
4237
43- root = root . ReplaceNode ( node , node . WithAdditionalAnnotations ( NeedsParenthesisAnnotation ) ) ;
38+ nodes . Add ( node ) ;
4439 }
4540
46- return root . ReplaceNodes ( root . GetAnnotatedNodes ( NeedsParenthesisAnnotation ) , this . AddParentheses ) ;
41+ return root . ReplaceNodes ( nodes , ( originalNode , rewrittenNode ) => AddParentheses ( originalNode , rewrittenNode ) ) ;
4742 }
4843
49- private SyntaxNode AddParentheses ( SyntaxNode originalNode , SyntaxNode rewrittenNode )
44+ private static SyntaxNode AddParentheses ( SyntaxNode originalNode , SyntaxNode rewrittenNode )
5045 {
5146 BinaryExpressionSyntax syntax = rewrittenNode as BinaryExpressionSyntax ;
5247 if ( syntax == null )
5348 {
5449 return rewrittenNode ;
5550 }
5651
57- BinaryExpressionSyntax trimmedSyntax = syntax
58- . WithoutTrivia ( )
59- . WithoutAnnotations ( NeedsParenthesisAnnotation . Kind ) ;
52+ BinaryExpressionSyntax trimmedSyntax = syntax . WithoutTrivia ( ) ;
6053
6154 return SyntaxFactory . ParenthesizedExpression ( trimmedSyntax )
6255 . WithTriviaFrom ( syntax )
0 commit comments