Skip to content

Commit 66377f2

Browse files
0x084Evweijsters
authored andcommitted
Fixed compiler errors after merge
Added Test for FixAll
1 parent 845ba29 commit 66377f2

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1135CodeFixProvider.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Composition;
9+
using System.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.CodeAnalysis;
@@ -56,12 +57,12 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5657
}
5758

5859
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
59-
var replacementNode = GenerateReplacementNode(semanticModel, node, cancellationToken);
60+
var replacementNode = GetReplacementNode(semanticModel, node, cancellationToken);
6061
var newSyntaxRoot = syntaxRoot.ReplaceNode(node, replacementNode);
6162
return document.WithSyntaxRoot(newSyntaxRoot);
6263
}
6364

64-
private static SyntaxNode GenerateReplacementNode(SemanticModel semanticModel, UsingDirectiveSyntax node, CancellationToken cancellationToken)
65+
private static SyntaxNode GetReplacementNode(SemanticModel semanticModel, UsingDirectiveSyntax node, CancellationToken cancellationToken)
6566
{
6667
SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(node.Name, cancellationToken);
6768
return node.WithName(SyntaxFactory.ParseName(symbolInfo.Symbol.ToString()));
@@ -75,9 +76,8 @@ private class FixAll : DocumentBasedFixAllProvider
7576
protected override string CodeActionTitle =>
7677
ReadabilityResources.SA1135CodeFix;
7778

78-
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
79+
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
7980
{
80-
var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);
8181
if (diagnostics.IsEmpty)
8282
{
8383
return null;
@@ -86,20 +86,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
8686
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
8787
SemanticModel semanticModel = await document.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
8888

89-
var replaceMap = new Dictionary<SyntaxNode, SyntaxNode>();
89+
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf<UsingDirectiveSyntax>());
9090

91-
foreach (Diagnostic diagnostic in diagnostics)
92-
{
93-
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, false, true) as UsingDirectiveSyntax;
94-
if (node == null || node.IsMissing)
95-
{
96-
continue;
97-
}
98-
99-
replaceMap[node] = GenerateReplacementNode(semanticModel, node, fixAllContext.CancellationToken);
100-
}
101-
102-
return syntaxRoot.ReplaceNodes(replaceMap.Keys, (originalNode, rewrittenNode) => replaceMap[originalNode]);
91+
return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => GetReplacementNode(semanticModel, rewrittenNode, fixAllContext.CancellationToken));
10392
}
10493
}
10594
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1135UnitTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ namespace System.Threading
126126
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
127127
}
128128

129+
[Fact]
130+
public async Task TestFixAllAsync()
131+
{
132+
const string testCode = @"
133+
namespace System.Threading
134+
{
135+
using NA = IO;
136+
using NB = Tasks;
137+
}";
138+
const string fixedCode = @"
139+
namespace System.Threading
140+
{
141+
using NA = System.IO;
142+
using NB = System.Threading.Tasks;
143+
}";
144+
145+
DiagnosticResult[] expected =
146+
{
147+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
148+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
149+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks"),
150+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks")
151+
};
152+
153+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
154+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
155+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
156+
await this.VerifyCSharpFixAllFixAsync(testCode, fixedCode, numberOfIterations: 1, cancellationToken: CancellationToken.None).ConfigureAwait(false);
157+
}
158+
129159
protected override CodeFixProvider GetCSharpCodeFixProvider()
130160
{
131161
return new SA1135CodeFixProvider();

0 commit comments

Comments
 (0)