Skip to content

Commit b85dc4a

Browse files
Correct SA1515 to not fire in file headers
#3630
1 parent e1fa460 commit b85dc4a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1515UnitTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.Test.LayoutRules
77
{
8+
using System;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis.Testing;
@@ -269,5 +270,38 @@ public Class1()
269270

270271
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
271272
}
273+
274+
/// <summary>
275+
/// Verifies that the analyzer does not fire in file headers (i.e. one line comments at the start of the file).
276+
/// </summary>
277+
/// <param name="startWithPragma"><see langword="true"/> if the source code should start with a pragma; otherwise, <see langword="false"/>.</param>
278+
/// <param name="numberOfHeaderLines">The number of lines in the header.</param>
279+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
280+
[Theory]
281+
[InlineData(false, 1)]
282+
[InlineData(false, 2)]
283+
[InlineData(false, 3)]
284+
[InlineData(true, 1)]
285+
[InlineData(true, 2)]
286+
[WorkItem(3630, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3630")]
287+
public async Task TestFileHeaderAsync(bool startWithPragma, int numberOfHeaderLines)
288+
{
289+
var testCode = @"
290+
class TestClass
291+
{
292+
}";
293+
294+
for (var i = 0; i < numberOfHeaderLines; i++)
295+
{
296+
testCode = "// A comment line." + Environment.NewLine + testCode;
297+
}
298+
299+
if (startWithPragma)
300+
{
301+
testCode = "#pragma warning disable CS1591" + Environment.NewLine + testCode;
302+
}
303+
304+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
305+
}
272306
}
273307
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1515SingleLineCommentMustBePrecededByBlankLine.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,19 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
164164
private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
165165
where T : IReadOnlyList<SyntaxTrivia>
166166
{
167+
if (triviaList[triviaIndex].Span.Start == 0)
168+
{
169+
return true;
170+
}
171+
167172
while (triviaIndex >= 0)
168173
{
174+
if (triviaList[triviaIndex].IsDirective)
175+
{
176+
// directive trivia are special, as they have a 'built-in' end-of-line.
177+
return true;
178+
}
179+
169180
if (triviaList[triviaIndex].IsKind(SyntaxKind.EndOfLineTrivia))
170181
{
171182
return true;
@@ -275,6 +286,7 @@ private static bool IsPrecededByDirectiveTrivia<T>(T triviaList, int triviaIndex
275286
case SyntaxKind.IfDirectiveTrivia:
276287
case SyntaxKind.ElifDirectiveTrivia:
277288
case SyntaxKind.ElseDirectiveTrivia:
289+
case SyntaxKind.PragmaWarningDirectiveTrivia:
278290
return true;
279291

280292
default:

0 commit comments

Comments
 (0)