Skip to content

Commit a1189aa

Browse files
committed
Fix behavior of SA1506 when a blank line follows a line comment
Fixes #1456
1 parent cdb724f commit a1189aa

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1506UnitTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,14 @@ public class TestClass
572572
public void TestMethod1() { }
573573
574574
/// <summary>more documentation.</summary>
575-
575+
576576
/* another comment */
577577
public void TestMethod2() { }
578+
579+
/// <summary>more documentation.</summary>
580+
// another comment (a specific regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1456)
581+
582+
public void TestMethod3() { }
578583
}
579584
";
580585

@@ -592,6 +597,10 @@ public void TestMethod1() { }
592597
/// <summary>more documentation.</summary>
593598
/* another comment */
594599
public void TestMethod2() { }
600+
601+
/// <summary>more documentation.</summary>
602+
// another comment (a specific regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1456)
603+
public void TestMethod3() { }
595604
}
596605
";
597606

@@ -600,6 +609,7 @@ public void TestMethod2() { }
600609
this.CSharpDiagnostic().WithLocation(3, 1),
601610
this.CSharpDiagnostic().WithLocation(10, 1),
602611
this.CSharpDiagnostic().WithLocation(15, 1),
612+
this.CSharpDiagnostic().WithLocation(20, 1),
603613
};
604614

605615
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1506CodeFixProvider.cs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,50 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5252
var triviaList = token.LeadingTrivia;
5353

5454
var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
55-
for (; index < triviaList.Count - 1; index++)
55+
56+
int currentLineStart = index + 1;
57+
bool onBlankLine = true;
58+
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
5659
{
57-
if (triviaList[index].IsKind(SyntaxKind.SingleLineCommentTrivia)
58-
|| triviaList[index].IsKind(SyntaxKind.MultiLineCommentTrivia))
60+
switch (triviaList[currentIndex].Kind())
5961
{
60-
break;
61-
}
62-
}
62+
case SyntaxKind.EndOfLineTrivia:
63+
if (onBlankLine)
64+
{
65+
for (int i = 0; i < currentIndex - currentLineStart + 1; i++)
66+
{
67+
triviaList = triviaList.RemoveAt(currentLineStart);
68+
currentIndex = currentLineStart - 1;
69+
}
6370

64-
while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia))
65-
{
66-
index--;
67-
}
71+
continue;
72+
}
73+
else
74+
{
75+
currentLineStart = currentIndex + 1;
76+
onBlankLine = true;
77+
break;
78+
}
6879

69-
var lastEndOfLine = index;
80+
case SyntaxKind.WhitespaceTrivia:
81+
break;
7082

71-
while (!triviaList[index].IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia))
72-
{
73-
index--;
83+
default:
84+
if (triviaList[currentIndex].HasBuiltinEndLine())
85+
{
86+
currentLineStart = currentIndex + 1;
87+
onBlankLine = true;
88+
break;
89+
}
90+
else
91+
{
92+
onBlankLine = false;
93+
break;
94+
}
95+
}
7496
}
7597

76-
var lastDocumentation = index;
77-
78-
var newLeadingTrivia = triviaList.Take(lastDocumentation + 1).Concat(triviaList.Skip(lastEndOfLine + 1));
79-
var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(newLeadingTrivia));
80-
98+
var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(triviaList));
8199
return document.WithSyntaxRoot(newSyntaxRoot);
82100
}
83101
}

0 commit comments

Comments
 (0)