Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1010 to accept whitespace before collection initializers #3745

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,47 @@

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

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1010CSharp12UnitTests : SA1010CSharp11UnitTests
{
[Fact]
[WorkItem(3687, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3687")]
public async Task TestCollectionExpressionAsync()
{
var testCode = $@"
using System.Collections.Generic;

namespace TestNamespace
{{
public class TestClass
{{
protected static readonly int[] DefaultMetadataPaths = [1, 2];

public void TestMethod(List<int> x, int y)
{{
List<int> foo = [];
foo = [42];
foo = [..x, y];
Bar([1 ,2, 3]);
}}

public void Bar(int[] x)
{{
}}
}}
}}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ internal static class SyntaxKindEx
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;
public const SyntaxKind FunctionPointerUnmanagedCallingConventionList = (SyntaxKind)9066;
public const SyntaxKind RecordStructDeclaration = (SyntaxKind)9068;
public const SyntaxKind CollectionExpression = (SyntaxKind)9076;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
}

if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem &&
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token))
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token) && !IsPartOfCollectionExpression(token))
{
// Opening square bracket should {not be preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPreceded, token.GetLocation(), TokenSpacingProperties.RemovePreceding));
Expand Down Expand Up @@ -128,5 +128,10 @@ private static bool IsPartOfListPattern(SyntaxToken token)
{
return token.Parent.IsKind(SyntaxKindEx.ListPattern);
}

private static bool IsPartOfCollectionExpression(SyntaxToken token)
{
return token.Parent.IsKind(SyntaxKindEx.CollectionExpression);
}
}
}