Skip to content

Commit

Permalink
Merge pull request #3857 from bjornhellander/feature/sa1015-initializ…
Browse files Browse the repository at this point in the history
…er-3856

Update SA1015 to not care about trailing spaces in a number of cases: typically last in dictionary initializer items, collection initializers and collection expressions. #3856
  • Loading branch information
sharwell authored Jun 3, 2024
2 parents b9a30bc + 320f616 commit de67e30
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@

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.SA1015ClosingGenericBracketsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1015CSharp12UnitTests : SA1015CSharp11UnitTests
{
[Theory]
[InlineData(" M<int> ")]
[InlineData("M<int>")]
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
public async Task TestSpacingAfterGenericMethodGroupInCollectionExpressionAsync(string item)
{
var testCode = $@"
using System;
using System.Collections.Generic;
public class TestClass
{{
private List<Action> values = [{item}];
private static void M<T>()
{{
}}
}}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

protected override DiagnosticResult[] GetExpectedResultMissingToken()
{
return new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.SpacingRules
{
using System.Threading;
Expand Down Expand Up @@ -430,6 +428,55 @@ public void TestMethod2(object input)
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData(" M<int> ")]
[InlineData("M<int>")]
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
public async Task TestSpacingAfterGenericMethodGroupInCollectionInitializerAsync(string item)
{
var testCode = $@"
using System;
using System.Collections.Generic;
public class TestClass
{{
private List<Action> values = new List<Action> {{{item}}};
private static void M<T>()
{{
}}
}}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData(" 1, M<int> ")]
[InlineData("1, M<int>")]
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
public async Task TestSpacingAfterGenericMethodGroupInDictionaryInitializerItemAsync(string item)
{
var testCode = $@"
using System;
using System.Collections.Generic;
public class TestClass
{{
private Dictionary<int, Action> values = new Dictionary<int, Action>
{{
{{{item}}}
}};
private static void M<T>()
{{
}}
}}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultMissingToken()
{
return new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.SpacingRules
{
using System;
Expand Down Expand Up @@ -127,6 +125,8 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy

case SyntaxKind.CloseParenToken:
case SyntaxKind.GreaterThanToken:
case SyntaxKind.CloseBraceToken:
case SyntaxKind.CloseBracketToken when nextToken.Parent.IsKind(SyntaxKindEx.CollectionExpression):
allowTrailingNoSpace = true;
allowTrailingSpace = true;
break;
Expand Down

0 comments on commit de67e30

Please sign in to comment.