Skip to content

Commit

Permalink
Correct SA1515 to not fire on the second line of a file header
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Apr 8, 2023
1 parent ef1f91c commit 3e3de67
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace StyleCop.Analyzers.Test.LayoutRules
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
Expand Down Expand Up @@ -269,5 +270,30 @@ public Class1()

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that the analyzer does not fire in file headers (i.e. one line comments at the start of the file).
/// </summary>
/// <param name="numberOfHeaderLines">The number of lines in the header.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[WorkItem(3630, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3630")]
public async Task TestFileHeaderAsync(int numberOfHeaderLines)
{
var testCode = @"
class TestClass
{
}";

for (var i = 0; i < numberOfHeaderLines; i++)
{
testCode = "// A comment line." + Environment.NewLine + testCode;
}

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation<T>(T t
break;

case SyntaxKind.SingleLineCommentTrivia:
return IsOnOwnLine(triviaList, triviaIndex);
return IsOnOwnLine(triviaList, triviaIndex) || currentTrivia.Span.Start == 0;

case SyntaxKind.SingleLineDocumentationCommentTrivia:
return true;
Expand Down

0 comments on commit 3e3de67

Please sign in to comment.