File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed
StyleCop.Analyzers.Test/LayoutRules
StyleCop.Analyzers/LayoutRules Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 5
5
6
6
namespace StyleCop . Analyzers . Test . LayoutRules
7
7
{
8
+ using System ;
8
9
using System . Threading ;
9
10
using System . Threading . Tasks ;
10
11
using Microsoft . CodeAnalysis . Testing ;
@@ -269,5 +270,38 @@ public Class1()
269
270
270
271
await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
271
272
}
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
+ }
272
306
}
273
307
}
Original file line number Diff line number Diff line change @@ -164,8 +164,19 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
164
164
private static bool IsOnOwnLine < T > ( T triviaList , int triviaIndex )
165
165
where T : IReadOnlyList < SyntaxTrivia >
166
166
{
167
+ if ( triviaList [ triviaIndex ] . Span . Start == 0 )
168
+ {
169
+ return true ;
170
+ }
171
+
167
172
while ( triviaIndex >= 0 )
168
173
{
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
+
169
180
if ( triviaList [ triviaIndex ] . IsKind ( SyntaxKind . EndOfLineTrivia ) )
170
181
{
171
182
return true ;
@@ -275,6 +286,7 @@ private static bool IsPrecededByDirectiveTrivia<T>(T triviaList, int triviaIndex
275
286
case SyntaxKind . IfDirectiveTrivia :
276
287
case SyntaxKind . ElifDirectiveTrivia :
277
288
case SyntaxKind . ElseDirectiveTrivia :
289
+ case SyntaxKind . PragmaWarningDirectiveTrivia :
278
290
return true ;
279
291
280
292
default :
You can’t perform that action at this time.
0 commit comments