Skip to content

Commit db6ead6

Browse files
Update SA1010 to accept whitespace before collection initializers
#3687
1 parent f21721e commit db6ead6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1010CSharp12UnitTests.cs

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

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

816
public partial class SA1010CSharp12UnitTests : SA1010CSharp11UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3687, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3687")]
20+
public async Task TestCollectionExpressionAsync()
21+
{
22+
var testCode = $@"
23+
using System.Collections.Generic;
24+
25+
namespace TestNamespace
26+
{{
27+
public class TestClass
28+
{{
29+
protected static readonly int[] DefaultMetadataPaths = [1, 2];
30+
31+
public void TestMethod(List<int> x, int y)
32+
{{
33+
List<int> foo = [];
34+
foo = [42];
35+
foo = [..x, y];
36+
Bar([1 ,2, 3]);
37+
}}
38+
39+
public void Bar(int[] x)
40+
{{
41+
}}
42+
}}
43+
}}
44+
";
45+
46+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
47+
}
1048
}
1149
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ internal static class SyntaxKindEx
6969
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;
7070
public const SyntaxKind FunctionPointerUnmanagedCallingConventionList = (SyntaxKind)9066;
7171
public const SyntaxKind RecordStructDeclaration = (SyntaxKind)9068;
72+
public const SyntaxKind CollectionExpression = (SyntaxKind)9076;
7273
}
7374
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
100100
}
101101

102102
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem &&
103-
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token))
103+
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token) && !IsPartOfCollectionExpression(token))
104104
{
105105
// Opening square bracket should {not be preceded} by a space.
106106
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPreceded, token.GetLocation(), TokenSpacingProperties.RemovePreceding));
@@ -128,5 +128,10 @@ private static bool IsPartOfListPattern(SyntaxToken token)
128128
{
129129
return token.Parent.IsKind(SyntaxKindEx.ListPattern);
130130
}
131+
132+
private static bool IsPartOfCollectionExpression(SyntaxToken token)
133+
{
134+
return token.Parent.IsKind(SyntaxKindEx.CollectionExpression);
135+
}
131136
}
132137
}

0 commit comments

Comments
 (0)