From 320f616adda2011b26bca71d84d687f87a68ed06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 1 Jun 2024 10:17:02 +0200 Subject: [PATCH] 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 --- .../SpacingRules/SA1015CSharp11UnitTests.cs | 2 - .../SpacingRules/SA1015CSharp12UnitTests.cs | 29 +++++++++++ .../SpacingRules/SA1015CSharp7UnitTests.cs | 2 - .../SpacingRules/SA1015CSharp8UnitTests.cs | 2 - .../SpacingRules/SA1015UnitTests.cs | 51 ++++++++++++++++++- ...ingGenericBracketsMustBeSpacedCorrectly.cs | 4 +- 6 files changed, 80 insertions(+), 10 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1015CSharp11UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1015CSharp11UnitTests.cs index 91fab7a48..aa72b4aef 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1015CSharp11UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1015CSharp11UnitTests.cs @@ -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; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1015CSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1015CSharp12UnitTests.cs index a01446ce9..d29fbcef3 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1015CSharp12UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1015CSharp12UnitTests.cs @@ -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 ")] + [InlineData("M")] + [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 values = [{item}]; + + private static void M() + {{ + }} +}} +"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + protected override DiagnosticResult[] GetExpectedResultMissingToken() { return new[] diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1015CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1015CSharp7UnitTests.cs index 3f12cd92e..0baa9488f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1015CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1015CSharp7UnitTests.cs @@ -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; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1015CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1015CSharp8UnitTests.cs index fe04df86b..cc0e8a05c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1015CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1015CSharp8UnitTests.cs @@ -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; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs index e6e7822be..e4b57ef6a 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs @@ -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; @@ -430,6 +428,55 @@ public void TestMethod2(object input) await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } + [Theory] + [InlineData(" M ")] + [InlineData("M")] + [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 values = new List {{{item}}}; + + private static void M() + {{ + }} +}} +"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + + [Theory] + [InlineData(" 1, M ")] + [InlineData("1, M")] + [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 values = new Dictionary + {{ + {{{item}}} + }}; + + private static void M() + {{ + }} +}} +"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + protected virtual DiagnosticResult[] GetExpectedResultMissingToken() { return new[] diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs index 7b5642f12..dfc566795 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs @@ -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; @@ -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;