Skip to content

Commit 2bbd35e

Browse files
authored
Merge pull request #3768 from bjornhellander/feature/sa1515-collection-expression-3766
Update SA1515 to not require a blank line before a comment at the start of a collection expression
2 parents 47801cc + 7dae8e9 commit 2bbd35e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1515CSharp12UnitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,37 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp12.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp11.LayoutRules;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.LayoutRules.SA1515SingleLineCommentMustBePrecededByBlankLine,
14+
StyleCop.Analyzers.LayoutRules.SA1515CodeFixProvider>;
715

816
public partial class SA1515CSharp12UnitTests : SA1515CSharp11UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3766, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3766")]
20+
public async Task TestFirstInCollectionExpressionAsync()
21+
{
22+
var testCode = @"
23+
public class TestClass
24+
{
25+
private string[] elements =
26+
[
27+
// Hydrogen
28+
""H"",
29+
30+
// Helium
31+
""He"",
32+
];
33+
}
34+
";
35+
36+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
37+
}
1038
}
1139
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,10 @@ private static bool IsAtStartOfScope(SyntaxTrivia trivia)
266266

267267
var prevToken = token.GetPreviousToken();
268268
return prevToken.IsKind(SyntaxKind.OpenBraceToken)
269-
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
270-
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
271-
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
269+
|| (prevToken.IsKind(SyntaxKind.OpenBracketToken) && prevToken.Parent.IsKind(SyntaxKindEx.CollectionExpression))
270+
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
271+
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
272+
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
272273
}
273274

274275
private static bool IsPrecededByDirectiveTrivia<T>(T triviaList, int triviaIndex)

0 commit comments

Comments
 (0)