Skip to content

Commit

Permalink
Merge pull request #3768 from bjornhellander/feature/sa1515-collectio…
Browse files Browse the repository at this point in the history
…n-expression-3766

Update SA1515 to not require a blank line before a comment at the start of a collection expression
  • Loading branch information
sharwell authored Jan 3, 2024
2 parents 47801cc + 7dae8e9 commit 2bbd35e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,37 @@

namespace StyleCop.Analyzers.Test.CSharp12.LayoutRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp11.LayoutRules;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1515SingleLineCommentMustBePrecededByBlankLine,
StyleCop.Analyzers.LayoutRules.SA1515CodeFixProvider>;

public partial class SA1515CSharp12UnitTests : SA1515CSharp11UnitTests
{
[Fact]
[WorkItem(3766, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3766")]
public async Task TestFirstInCollectionExpressionAsync()
{
var testCode = @"
public class TestClass
{
private string[] elements =
[
// Hydrogen
""H"",
// Helium
""He"",
];
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,10 @@ private static bool IsAtStartOfScope(SyntaxTrivia trivia)

var prevToken = token.GetPreviousToken();
return prevToken.IsKind(SyntaxKind.OpenBraceToken)
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
|| (prevToken.IsKind(SyntaxKind.OpenBracketToken) && prevToken.Parent.IsKind(SyntaxKindEx.CollectionExpression))
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
}

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

0 comments on commit 2bbd35e

Please sign in to comment.