Skip to content

Commit 401f11e

Browse files
committed
Fixed issues with SA1120 code fix
1 parent 23fa4de commit 401f11e

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1120CodeFixProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8888

8989
private static bool TriviaHasLeadingContentOnLine(SyntaxNode root, SyntaxTrivia commentTrivia)
9090
{
91+
if (commentTrivia.SpanStart == 0)
92+
{
93+
// It is impossible to have leading content at the start of the file.
94+
return false;
95+
}
96+
9197
var nodeBeforeStart = commentTrivia.SpanStart - 1;
9298
var nodeBefore = root.FindNode(new Microsoft.CodeAnalysis.Text.TextSpan(nodeBeforeStart, 1));
9399

@@ -96,6 +102,12 @@ private static bool TriviaHasLeadingContentOnLine(SyntaxNode root, SyntaxTrivia
96102

97103
private static bool TriviaHasTrailingContentOnLine(SyntaxNode root, SyntaxTrivia commentTrivia)
98104
{
105+
if (commentTrivia.Span.End == root.Span.End)
106+
{
107+
// It is impossible to have trailing content at the end of the file.
108+
return false;
109+
}
110+
99111
var nodeAfterTriviaStart = commentTrivia.Span.End + 1;
100112
var nodeAfterTrivia = root.FindNode(new Microsoft.CodeAnalysis.Text.TextSpan(nodeAfterTriviaStart, 1));
101113

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1120UnitTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,56 @@ public SomeException()
354354
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
355355
}
356356

357+
/// <summary>
358+
/// Verifies that an empty comment at the start of a source file will be handled correctly.
359+
/// This is a regression test for #1708
360+
/// </summary>
361+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
362+
[Fact]
363+
public async Task VerifyThatEmptyCommentAtFileStartWillBeHandledProperlyAsync()
364+
{
365+
var testCode = @"//
366+
public class TestClass
367+
{
368+
}
369+
";
370+
371+
var fixedTestCode = @"public class TestClass
372+
{
373+
}
374+
";
375+
376+
var expected = this.CSharpDiagnostic().WithLocation(1, 1);
377+
378+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
379+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
380+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
381+
}
382+
383+
/// <summary>
384+
/// Verifies that an empty comment at the end of a source file will be handled correctly.
385+
/// </summary>
386+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
387+
[Fact]
388+
public async Task VerifyThatEmptyCommentAtFileEndWillBeHandledProperlyAsync()
389+
{
390+
var testCode = @"public class TestClass
391+
{
392+
}
393+
//";
394+
395+
var fixedTestCode = @"public class TestClass
396+
{
397+
}
398+
";
399+
400+
var expected = this.CSharpDiagnostic().WithLocation(4, 1);
401+
402+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
403+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
404+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
405+
}
406+
357407
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
358408
{
359409
yield return new SA1120CommentsMustContainText();

0 commit comments

Comments
 (0)