Skip to content

Commit fab095a

Browse files
committed
Fixed issue in SA1001 fix all code fix
1 parent ebf6c5b commit fab095a

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/TokenSpacingCodeFixProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ private static void UpdateReplaceMap(Dictionary<SyntaxToken, SyntaxToken> replac
225225
case TokenSpacingProperties.ActionInsert:
226226
if (!replaceMap.ContainsKey(nextToken))
227227
{
228-
replaceMap[token] = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, SyntaxFactory.Space));
228+
// If the token is already present in the map and it has trailing trivia, then it has already been
229+
// processed during an earlier step in the fix all process, so no additional processing is needed.
230+
if (!replaceMap.ContainsKey(token) || (replaceMap[token].TrailingTrivia.Count == 0))
231+
{
232+
replaceMap[token] = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, SyntaxFactory.Space));
233+
}
229234
}
230235

231236
break;

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,55 @@ void MethodName()
227227
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
228228
}
229229

230+
[Fact]
231+
[WorkItem(2468, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2468")]
232+
public async Task TestCodeFixCommaPlacementAsync()
233+
{
234+
var testCode = @"using System;
235+
236+
public class TestClass
237+
{
238+
public void TestMethod()
239+
{
240+
var test = (new[]
241+
{
242+
new Tuple<int, int>(1, 2)
243+
,new Tuple<int, int>(3, 4)
244+
,new Tuple<int, int>(5, 6)
245+
}).ToString();
246+
}
247+
}
248+
";
249+
250+
var fixedCode = @"using System;
251+
252+
public class TestClass
253+
{
254+
public void TestMethod()
255+
{
256+
var test = (new[]
257+
{
258+
new Tuple<int, int>(1, 2),
259+
new Tuple<int, int>(3, 4),
260+
new Tuple<int, int>(5, 6)
261+
}).ToString();
262+
}
263+
}
264+
";
265+
266+
DiagnosticResult[] expected =
267+
{
268+
this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(10, 12),
269+
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(10, 12),
270+
this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(11, 12),
271+
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(11, 12),
272+
};
273+
274+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
275+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
276+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
277+
}
278+
230279
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
231280
{
232281
yield return new SA1001CommasMustBeSpacedCorrectly();

0 commit comments

Comments
 (0)