Skip to content

Commit 990b6bd

Browse files
committed
For SA1137, first in line means whitespace only
1 parent b660fa6 commit 990b6bd

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,24 @@ void MethodName()
14241424
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
14251425
}
14261426

1427+
[Fact]
1428+
public async Task TestLeadingCommentAsync()
1429+
{
1430+
string testCode = @"
1431+
using System.Collections.Generic;
1432+
class ClassName
1433+
{
1434+
void MethodName()
1435+
{
1436+
/* var x = */ new List<string>();
1437+
var y = new List<string>();
1438+
}
1439+
}
1440+
";
1441+
1442+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1443+
}
1444+
14271445
[Fact]
14281446
public async Task TestSwitchStatementAsync()
14291447
{

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TokenHelper.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,44 @@ internal static class TokenHelper
1515
/// Gets a value indicating whether the <paramref name="token"/> is first in line.
1616
/// </summary>
1717
/// <param name="token">The token to process.</param>
18-
/// <returns>true if token is first in line, otherwise false.</returns>
19-
internal static bool IsFirstInLine(this SyntaxToken token)
18+
/// <param name="allowNonWhitespaceTrivia"><see langword="true"/> to consider the token first-in-line even when
19+
/// non-whitespace trivia precedes the token; otherwise, <see langword="false"/> to only consider tokens first
20+
/// in line if they are preceded solely by whitespace.</param>
21+
/// <returns>
22+
/// <see langword="true"/> if <paramref name="token"/> is first in line; otherwise, <see langword="false"/>.
23+
/// </returns>
24+
internal static bool IsFirstInLine(this SyntaxToken token, bool allowNonWhitespaceTrivia = true)
2025
{
2126
var fullLineSpan = token.SyntaxTree.GetLineSpan(token.FullSpan);
2227

28+
bool firstInLine;
2329
if (token.SyntaxTree == null || fullLineSpan.StartLinePosition.Character == 0)
2430
{
25-
return true;
31+
firstInLine = true;
32+
}
33+
else
34+
{
35+
var tokenLineSpan = token.SyntaxTree.GetLineSpan(token.Span);
36+
firstInLine = tokenLineSpan.StartLinePosition.Line != fullLineSpan.StartLinePosition.Line;
2637
}
2738

28-
var tokenLineSpan = token.SyntaxTree.GetLineSpan(token.Span);
29-
return tokenLineSpan.StartLinePosition.Line != fullLineSpan.StartLinePosition.Line;
39+
if (firstInLine && !allowNonWhitespaceTrivia)
40+
{
41+
foreach (var trivia in token.LeadingTrivia.Reverse())
42+
{
43+
if (!trivia.IsKind(SyntaxKind.WhitespaceTrivia))
44+
{
45+
if (!trivia.HasBuiltinEndLine())
46+
{
47+
firstInLine = false;
48+
}
49+
50+
break;
51+
}
52+
}
53+
}
54+
55+
return firstInLine;
3056
}
3157

3258
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ private static void CheckElements<T>(SyntaxNodeAnalysisContext context, Immutabl
360360
element =>
361361
{
362362
SyntaxToken firstToken = GetFirstTokenForAnalysis(element);
363-
return firstToken.IsMissingOrDefault() || !firstToken.IsFirstInLine();
363+
return firstToken.IsMissingOrDefault() || !firstToken.IsFirstInLine(allowNonWhitespaceTrivia: false);
364364
});
365365

366366
if (elements.Count < 2)

0 commit comments

Comments
 (0)