Skip to content

Commit 4d76e73

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 01f7674 commit 4d76e73

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
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: 6 additions & 11 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.
@@ -174,7 +166,8 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
174166
{
175167
while (triviaIndex >= 0)
176168
{
177-
if (triviaList[triviaIndex].IsKind(SyntaxKind.EndOfLineTrivia))
169+
var currentTrivia = triviaList[triviaIndex];
170+
if (currentTrivia.IsKind(SyntaxKind.EndOfLineTrivia))
178171
{
179172
return true;
180173
}
@@ -185,7 +178,7 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
185178
return false;
186179
}
187180

188-
private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList, int triviaIndex)
181+
private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation<T>(T triviaList, int triviaIndex)
189182
where T : IReadOnlyList<SyntaxTrivia>
190183
{
191184
var eolCount = 0;
@@ -206,6 +199,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
206199
break;
207200

208201
case SyntaxKind.SingleLineCommentTrivia:
202+
return IsOnOwnLine(triviaList, triviaIndex);
203+
209204
case SyntaxKind.SingleLineDocumentationCommentTrivia:
210205
return true;
211206

0 commit comments

Comments
 (0)