Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SA1514 directive trivia handling #1233

Merged
merged 1 commit into from
Aug 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -829,33 +829,143 @@ public void Method2()
}
";

var fixedCode = @"
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that preprocessor directives before a documentation header are properly handled.
/// This is regression for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1231
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestDirectivesPrecedingDocumentationAsync()
{
var testCode = @"
public class TestClass
{
#pragma warning disable RS1012

#if true
/// <summary>
/// ...
/// </summary>
public void Method1()
{
}
#else
/// <summary>
/// ...
/// </summary>
public void Method2()
{
}
#endif
/// <summary>
/// ...
/// </summary>
public void Method3()
{
}

#pragma checksum ""test0.cs"" ""{00000000-0000-0000-0000-000000000000}"" ""{01234567}"" // comment
#region SomeRegion
/// <summary>
/// ...
/// </summary>
public void Method4()
{
}
#endregion SomeRegion
/// <summary>
/// ...
/// </summary>
public void Method5()
{
}

#region AnotherRegion // comment
/// <summary>
/// ...
/// </summary>
public void Method6()
{
}
#endregion AnotherRegion // comment
/// <summary>
/// ...
/// </summary>
public void Method7()
{
}
}
";

var fixedCode = @"
public class TestClass
{
#if true
/// <summary>
/// ...
/// </summary>
public void Method1()
{
}
#else
/// <summary>
/// ...
/// </summary>
public void Method2()
{
}
#endif

/// <summary>
/// ...
/// </summary>
public void Method3()
{
}

#region SomeRegion

/// <summary>
/// ...
/// </summary>
public void Method4()
{
}
#endregion SomeRegion

/// <summary>
/// ...
/// </summary>
public void Method5()
{
}

#region AnotherRegion // comment

/// <summary>
/// ...
/// </summary>
public void Method6()
{
}
#endregion AnotherRegion // comment

/// <summary>
/// ...
/// </summary>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down