From d59d4a68bc549b4d62bc8badd3037e8ea6dd6aa0 Mon Sep 17 00:00:00 2001 From: John Venable Date: Mon, 17 Aug 2015 11:38:34 -0400 Subject: [PATCH] Fix SA1514 directive trivia handling --- .../LayoutRules/SA1514UnitTests.cs | 122 +++++++++++++++++- ...entationHeaderMustBePrecededByBlankLine.cs | 20 +-- 2 files changed, 128 insertions(+), 14 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs index f276e60e6..39072fce0 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs @@ -829,33 +829,143 @@ public void Method2() } "; - var fixedCode = @" + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + + /// + /// Verifies that preprocessor directives before a documentation header are properly handled. + /// This is regression for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1231 + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task TestDirectivesPrecedingDocumentationAsync() + { + var testCode = @" public class TestClass { - #pragma warning disable RS1012 - +#if true /// /// ... /// public void Method1() { } +#else + /// + /// ... + /// + public void Method2() + { + } +#endif + /// + /// ... + /// + public void Method3() + { + } - #pragma checksum ""test0.cs"" ""{00000000-0000-0000-0000-000000000000}"" ""{01234567}"" // comment +#region SomeRegion + /// + /// ... + /// + public void Method4() + { + } +#endregion SomeRegion + /// + /// ... + /// + public void Method5() + { + } + +#region AnotherRegion // comment + /// + /// ... + /// + public void Method6() + { + } +#endregion AnotherRegion // comment + /// + /// ... + /// + public void Method7() + { + } +} +"; + var fixedCode = @" +public class TestClass +{ +#if true + /// + /// ... + /// + public void Method1() + { + } +#else /// /// ... /// public void Method2() { } +#endif + + /// + /// ... + /// + public void Method3() + { + } + +#region SomeRegion + + /// + /// ... + /// + public void Method4() + { + } +#endregion SomeRegion + + /// + /// ... + /// + public void Method5() + { + } + +#region AnotherRegion // comment + + /// + /// ... + /// + public void Method6() + { + } +#endregion AnotherRegion // comment + + /// + /// ... + /// + public void Method7() + { + } } "; DiagnosticResult[] expected = { - this.CSharpDiagnostic().WithLocation(5, 5), - this.CSharpDiagnostic().WithLocation(13, 5) + this.CSharpDiagnostic().WithLocation(19, 5), + this.CSharpDiagnostic().WithLocation(27, 5), + this.CSharpDiagnostic().WithLocation(34, 5), + this.CSharpDiagnostic().WithLocation(42, 5), + this.CSharpDiagnostic().WithLocation(49, 5) }; await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs index e86092ab2..a6cf31c48 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs @@ -113,22 +113,26 @@ private static void HandleDeclaration(SyntaxNodeAnalysisContext context) var done = false; for (var i = documentationHeaderIndex - 1; !done && (i >= 0); i--) { - switch (triviaList[i].Kind()) + var trivia = triviaList[i]; + if (trivia.IsDirective + && !trivia.IsKind(SyntaxKind.EndIfDirectiveTrivia) + && !trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) + && !trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia)) + { + return; + } + + switch (trivia.Kind()) { case SyntaxKind.WhitespaceTrivia: break; case SyntaxKind.EndOfLineTrivia: eolCount++; break; - case SyntaxKind.IfDirectiveTrivia: - case SyntaxKind.ElseDirectiveTrivia: - case SyntaxKind.ElifDirectiveTrivia: - // if the documentation header is inside a directive trivia, no leading blank line is needed - return; case SyntaxKind.EndIfDirectiveTrivia: - case SyntaxKind.PragmaWarningDirectiveTrivia: - case SyntaxKind.PragmaChecksumDirectiveTrivia: + case SyntaxKind.RegionDirectiveTrivia: + case SyntaxKind.EndRegionDirectiveTrivia: eolCount++; done = true; break;