Skip to content

Commit d8d53da

Browse files
authored
Merge pull request #2621 from vweijsters/fix-2594
Improved SA1132 code fix
2 parents 9728db3 + 6bdfe19 commit d8d53da

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1132CodeFixProvider.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6363
{
6464
var editor = new SyntaxEditor(syntaxRoot, document.Project.Solution.Workspace);
6565
editor.InsertAfter(baseFieldDeclaration, newFieldDeclarations);
66-
editor.RemoveNode(baseFieldDeclaration);
66+
editor.RemoveNode(baseFieldDeclaration, SyntaxRemoveOptions.KeepNoTrivia);
6767
return document.WithSyntaxRoot(editor.GetChangedRoot().WithoutFormatting());
6868
}
6969

@@ -118,6 +118,27 @@ private static List<BaseFieldDeclarationSyntax> DeclarationSplitter(
118118
if (variable != first)
119119
{
120120
var triviaList = previous.GetLeadingTrivia().WithoutDirectiveTrivia();
121+
122+
// Remove all leading blank lines
123+
var nonBlankLinetriviaIndex = TriviaHelper.IndexOfFirstNonBlankLineTrivia(triviaList);
124+
if (nonBlankLinetriviaIndex > 0)
125+
{
126+
triviaList = triviaList.RemoveRange(0, nonBlankLinetriviaIndex);
127+
}
128+
129+
// Add a blank line if the first line contains a comment.
130+
var nonWhitespaceTriviaIndex = TriviaHelper.IndexOfFirstNonWhitespaceTrivia(triviaList, false);
131+
if (nonWhitespaceTriviaIndex >= 0)
132+
{
133+
switch (triviaList[nonWhitespaceTriviaIndex].Kind())
134+
{
135+
case SyntaxKind.SingleLineCommentTrivia:
136+
case SyntaxKind.MultiLineCommentTrivia:
137+
triviaList = triviaList.Insert(0, SyntaxFactory.CarriageReturnLineFeed);
138+
break;
139+
}
140+
}
141+
121142
previous = previous.WithLeadingTrivia(triviaList);
122143
}
123144
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1132UnitTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,77 @@ class TestAttribute : System.Attribute
170170
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
171171
}
172172

173+
[Fact]
174+
[WorkItem(2594, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2594")]
175+
public async Task VerifyThatDirectiveTriviaAreHandledCorrectlyAsync()
176+
{
177+
var testCode = @"
178+
namespace StyleCopDemo
179+
{
180+
class Program
181+
{
182+
#region some members
183+
184+
string myString;
185+
186+
#region some fields
187+
188+
// this line:
189+
int myInt, yourInt;
190+
191+
#endregion
192+
193+
#region some other fields
194+
195+
int secondInt, thirdInt;
196+
197+
#endregion
198+
199+
#endregion
200+
}
201+
}";
202+
203+
var fixedCode = @"
204+
namespace StyleCopDemo
205+
{
206+
class Program
207+
{
208+
#region some members
209+
210+
string myString;
211+
212+
#region some fields
213+
214+
// this line:
215+
int myInt;
216+
217+
// this line:
218+
int yourInt;
219+
220+
#endregion
221+
222+
#region some other fields
223+
224+
int secondInt;
225+
int thirdInt;
226+
227+
#endregion
228+
229+
#endregion
230+
}
231+
}";
232+
233+
DiagnosticResult[] expected =
234+
{
235+
this.CSharpDiagnostic().WithLocation(13, 9),
236+
this.CSharpDiagnostic().WithLocation(19, 9),
237+
};
238+
239+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
240+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
241+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
242+
}
243+
173244
protected override CodeFixProvider GetCSharpCodeFixProvider()
174245
{
175246
return new SA1132CodeFixProvider();

0 commit comments

Comments
 (0)