Skip to content

Update SA1515 to not let one range of trivia affect another #3529

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

Merged
merged 1 commit into from
Jun 22, 2022
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 @@ -243,5 +243,31 @@ public class TestConstants

await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that the analyzer will properly handle documentation followed by a comment,
/// even if there is another non-adjacent comment earlier.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3481, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3481")]
public async Task TestDocumentationFollowedByCommentWhenThereIsAlsoAnEarlierCommentAsync()
{
var testCode = @"
public class Class1 // Comment 1
{
public Class1()
{
}

/// <summary>
/// Gets value.
/// </summary>
// Comment 2
public double Value { get; }
}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,18 @@ public override void Initialize(AnalysisContext context)
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
{
var syntaxRoot = context.Tree.GetRoot(context.CancellationToken);
var previousCommentNotOnOwnLine = false;

foreach (var trivia in syntaxRoot.DescendantTrivia().Where(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)))
{
if (trivia.FullSpan.Start == 0)
{
// skip the trivia if it is at the start of the file
previousCommentNotOnOwnLine = false;
continue;
}

if (trivia.ToString().StartsWith("////", StringComparison.Ordinal))
{
// ignore commented out code
previousCommentNotOnOwnLine = false;
continue;
}

Expand All @@ -132,26 +129,21 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
if (!IsOnOwnLine(triviaList, triviaIndex))
{
// ignore comments after other code elements.
previousCommentNotOnOwnLine = true;
continue;
}

if (IsPrecededByBlankLine(triviaList, triviaIndex))
{
// allow properly formatted blank line comments.
previousCommentNotOnOwnLine = false;
continue;
}

if (!previousCommentNotOnOwnLine && IsPrecededBySingleLineCommentOrDocumentation(triviaList, triviaIndex))
if (IsPrecededBySingleLineCommentOnOwnLineOrDocumentation(triviaList, triviaIndex))
{
// allow consecutive single line comments.
previousCommentNotOnOwnLine = false;
continue;
}

previousCommentNotOnOwnLine = false;

if (IsAtStartOfScope(trivia))
{
// allow single line comment at scope start.
Expand Down Expand Up @@ -185,7 +177,7 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
return false;
}

private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList, int triviaIndex)
private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation<T>(T triviaList, int triviaIndex)
where T : IReadOnlyList<SyntaxTrivia>
{
var eolCount = 0;
Expand All @@ -206,6 +198,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
break;

case SyntaxKind.SingleLineCommentTrivia:
return IsOnOwnLine(triviaList, triviaIndex);

case SyntaxKind.SingleLineDocumentationCommentTrivia:
return true;

Expand Down