-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1407SA1408FixAllProvider.cs
More file actions
70 lines (57 loc) · 2.42 KB
/
SA1407SA1408FixAllProvider.cs
File metadata and controls
70 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;
internal sealed class SA1407SA1408FixAllProvider : DocumentBasedFixAllProvider
{
protected override string CodeActionTitle => MaintainabilityResources.SA1407SA1408CodeFix;
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
{
if (diagnostics.IsEmpty)
{
return null;
}
var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
List<SyntaxNode> nodes = new List<SyntaxNode>();
foreach (var diagnostic in diagnostics)
{
SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan);
if (node.IsMissing)
{
continue;
}
nodes.Add(node);
}
return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(rewrittenNode));
}
private static SyntaxNode AddParentheses(SyntaxNode node)
{
if (node is BinaryExpressionSyntax syntax)
{
BinaryExpressionSyntax trimmedSyntax = syntax.WithoutTrivia();
return SyntaxFactory.ParenthesizedExpression(trimmedSyntax)
.WithTriviaFrom(syntax)
.WithoutFormatting();
}
if (BinaryPatternSyntaxWrapper.IsInstance(node))
{
BinaryPatternSyntaxWrapper trimmedSyntax = (BinaryPatternSyntaxWrapper)node.WithoutTrivia();
return SyntaxFactoryEx.ParenthesizedPattern(trimmedSyntax)
.SyntaxNode
.WithTriviaFrom(node)
.WithoutFormatting();
}
return node;
}
}
}