Skip to content

Commit bd21b58

Browse files
Update SA1515 to not let one range of trivia affect another. This basically reverts the analyzer change from commit 274493b and implements it in another way.
#3481
1 parent b46e01b commit bd21b58

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1515UnitTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,31 @@ public class TestConstants
243243

244244
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
245245
}
246+
247+
/// <summary>
248+
/// Verifies that the analyzer will properly handle documentation followed by a comment,
249+
/// even if there is another non-adjacent comment earlier.
250+
/// </summary>
251+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
252+
[Fact]
253+
[WorkItem(3481, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3481")]
254+
public async Task TestDocumentationFollowedByCommentWhenThereIsAlsoAnEarlierCommentAsync()
255+
{
256+
var testCode = @"
257+
public class Class1 // Comment 1
258+
{
259+
public Class1()
260+
{
261+
}
262+
263+
/// <summary>
264+
/// Gets value.
265+
/// </summary>
266+
// Comment 2
267+
public double Value { get; }
268+
}";
269+
270+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
271+
}
246272
}
247273
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1515SingleLineCommentMustBePrecededByBlankLine.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,18 @@ public override void Initialize(AnalysisContext context)
108108
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
109109
{
110110
var syntaxRoot = context.Tree.GetRoot(context.CancellationToken);
111-
var previousCommentNotOnOwnLine = false;
112111

113112
foreach (var trivia in syntaxRoot.DescendantTrivia().Where(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)))
114113
{
115114
if (trivia.FullSpan.Start == 0)
116115
{
117116
// skip the trivia if it is at the start of the file
118-
previousCommentNotOnOwnLine = false;
119117
continue;
120118
}
121119

122120
if (trivia.ToString().StartsWith("////", StringComparison.Ordinal))
123121
{
124122
// ignore commented out code
125-
previousCommentNotOnOwnLine = false;
126123
continue;
127124
}
128125

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

139135
if (IsPrecededByBlankLine(triviaList, triviaIndex))
140136
{
141137
// allow properly formatted blank line comments.
142-
previousCommentNotOnOwnLine = false;
143138
continue;
144139
}
145140

146-
if (!previousCommentNotOnOwnLine && IsPrecededBySingleLineCommentOrDocumentation(triviaList, triviaIndex))
141+
if (IsPrecededBySingleLineCommentOnOwnLineOrDocumentation(triviaList, triviaIndex))
147142
{
148143
// allow consecutive single line comments.
149-
previousCommentNotOnOwnLine = false;
150144
continue;
151145
}
152146

153-
previousCommentNotOnOwnLine = false;
154-
155147
if (IsAtStartOfScope(trivia))
156148
{
157149
// allow single line comment at scope start.
@@ -185,7 +177,7 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
185177
return false;
186178
}
187179

188-
private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList, int triviaIndex)
180+
private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation<T>(T triviaList, int triviaIndex)
189181
where T : IReadOnlyList<SyntaxTrivia>
190182
{
191183
var eolCount = 0;
@@ -206,6 +198,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
206198
break;
207199

208200
case SyntaxKind.SingleLineCommentTrivia:
201+
return IsOnOwnLine(triviaList, triviaIndex);
202+
209203
case SyntaxKind.SingleLineDocumentationCommentTrivia:
210204
return true;
211205

0 commit comments

Comments
 (0)