Skip to content

Commit 268fc6d

Browse files
committed
Fix SA1133 handling of whitespace in code fix
1 parent e60c596 commit 268fc6d

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax
7777

7878
for (var i = 0; i < attributeList.Attributes.Count; i++)
7979
{
80-
var newAttributes = SyntaxFactory.SingletonSeparatedList(attributeList.Attributes[i]);
80+
var newAttributes = SyntaxFactory.SingletonSeparatedList(
81+
attributeList.Attributes[i].WithLeadingTrivia(
82+
attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
8183
var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);
8284

8385
newAttributeList = (i == 0)

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1133UnitTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
using Xunit;
45
namespace StyleCop.Analyzers.Test.ReadabilityRules
56
{
67
using System.Collections.Generic;
@@ -421,6 +422,51 @@ public enum ImplicitUseKindFlags { Assign }
421422
await this.VerifyCSharpFixAllFixAsync(testCode, fixedTestCode, maxNumberOfIterations: 1).ConfigureAwait(false);
422423
}
423424

425+
/// <summary>
426+
/// Regression test for issue 1883 (whitespace is preserved incorrectly): https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1883
427+
/// </summary>
428+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
429+
[Fact]
430+
public async Task TestWhitespaceIsHandledCorrectlyAsync()
431+
{
432+
var testCode = @"
433+
namespace SA1133CodeFix
434+
{
435+
using System.ComponentModel;
436+
using System.Diagnostics.CodeAnalysis;
437+
438+
[DefaultValue(true),
439+
SuppressMessage(null, null)]
440+
internal class Foo
441+
{
442+
}
443+
}
444+
";
445+
446+
var fixedTestCode = @"
447+
namespace SA1133CodeFix
448+
{
449+
using System.ComponentModel;
450+
using System.Diagnostics.CodeAnalysis;
451+
452+
[DefaultValue(true)]
453+
[SuppressMessage(null, null)]
454+
internal class Foo
455+
{
456+
}
457+
}
458+
";
459+
460+
DiagnosticResult[] expected =
461+
{
462+
this.CSharpDiagnostic().WithLocation(8, 5)
463+
};
464+
465+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
466+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
467+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
468+
}
469+
424470
/// <inheritdoc/>
425471
protected override CodeFixProvider GetCSharpCodeFixProvider()
426472
{

0 commit comments

Comments
 (0)