File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
StyleCop.Analyzers.Test/LayoutRules
StyleCop.Analyzers/LayoutRules Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 55
66namespace 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,36 @@ 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+ [ CombinatorialData ]
282+ [ WorkItem ( 3630 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3630" ) ]
283+ public async Task TestFileHeaderAsync (
284+ bool startWithPragma ,
285+ [ CombinatorialValues ( 1 , 2 , 3 ) ] int numberOfHeaderLines )
286+ {
287+ var testCode = @"
288+ class TestClass
289+ {
290+ }" ;
291+
292+ for ( var i = 0 ; i < numberOfHeaderLines ; i ++ )
293+ {
294+ testCode = "// A comment line." + Environment . NewLine + testCode ;
295+ }
296+
297+ if ( startWithPragma )
298+ {
299+ testCode = "#pragma warning disable CS1591" + Environment . NewLine + testCode ;
300+ }
301+
302+ await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
303+ }
272304 }
273305}
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments