From b38cfd7dbaca7277e8479ed72a0b38c34cc9bc2a Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Wed, 15 May 2024 15:44:18 -0400 Subject: [PATCH 1/8] Do not warn if a comma follows a preprocessor directive --- .../SpacingRules/SA1001UnitTests.cs | 21 +++++++++++++++++++ .../SA1001CommasMustBeSpacedCorrectly.cs | 9 ++++++++ documentation/SA1001.md | 6 ++++++ 3 files changed, 36 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs index 082829819..bc66ecd6e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs @@ -349,6 +349,27 @@ public void TestMethod() await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } + [Fact] + [WorkItem(3816, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3816")] + public async Task TestCommaFollowingPreprocessorDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if !NETSTANDARD + , ISpanFormattable +#endif +{ +} +"; + + var expected = DiagnosticResult.EmptyDiagnosticResults; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + private Task TestCommaInStatementOrDeclAsync(string originalStatement, DiagnosticResult expected, string fixedStatement) { return this.TestCommaInStatementOrDeclAsync(originalStatement, new[] { expected }, fixedStatement); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index eced91ae2..222989f2c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.SpacingRules { using System; using System.Collections.Immutable; + using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; @@ -76,6 +77,14 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo return; } + // Check if the comma follows a preprocessor directive + if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia => + trivia.IsDirective)) + { + // Ignore this comma as it follows a preprocessor directive + return; + } + // check for a following space bool missingFollowingSpace = true; diff --git a/documentation/SA1001.md b/documentation/SA1001.md index 0d68cce9d..b4a16b88d 100644 --- a/documentation/SA1001.md +++ b/documentation/SA1001.md @@ -28,6 +28,12 @@ A comma should be followed by a single space, except in the following cases. * A comma may appear at the end of a line * A comma should not be followed by a space when used in an open generic type in a `typeof` expression * A comma is part of a string interpolation alignment component. For example:`$"{x,3}"` +* A comma follows a preprocessor directive. For example: + ```csharp + partial struct Money : IFormattable + #if !NETSTANDARD + , ISpanFormattable + #endif A comma should never be preceded by a space or appear as the first token on a line. From 0ab468c3171a2bc687cd21e6a4ecd69b5b355e52 Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Wed, 15 May 2024 15:50:44 -0400 Subject: [PATCH 2/8] cleanup --- .../SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index 222989f2c..9bc2b00f3 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -81,7 +81,6 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia => trivia.IsDirective)) { - // Ignore this comma as it follows a preprocessor directive return; } From 091e474c87ca02c5763dcd08214eb453cdb301e9 Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Wed, 15 May 2024 16:11:06 -0400 Subject: [PATCH 3/8] Updated to ignore conditional directives --- .../SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index 9bc2b00f3..a4cd47526 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -77,10 +77,14 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo return; } - // Check if the comma follows a preprocessor directive + // Check if the comma follows a conditional preprocessor directive if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia => - trivia.IsDirective)) + trivia.IsKind(SyntaxKind.IfDirectiveTrivia) || + trivia.IsKind(SyntaxKind.ElifDirectiveTrivia) || + trivia.IsKind(SyntaxKind.ElseDirectiveTrivia) || + trivia.IsKind(SyntaxKind.EndIfDirectiveTrivia))) { + // Ignore this comma as it follows a conditional preprocessor directive return; } From 350bed00461cb2be7b06ea9a00cff62b7e7cf4fa Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Wed, 15 May 2024 16:12:29 -0400 Subject: [PATCH 4/8] cleanup --- .../SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index a4cd47526..403934015 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -30,6 +30,7 @@ internal class SA1001CommasMustBeSpacedCorrectly : DiagnosticAnalyzer /// The ID for diagnostics produced by the analyzer. /// public const string DiagnosticId = "SA1001"; + private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1001.md"; private static readonly LocalizableString Title = new LocalizableResourceString(nameof(SpacingResources.SA1001Title), SpacingResources.ResourceManager, typeof(SpacingResources)); private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(SpacingResources.SA1001MessageFormat), SpacingResources.ResourceManager, typeof(SpacingResources)); @@ -84,7 +85,6 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo trivia.IsKind(SyntaxKind.ElseDirectiveTrivia) || trivia.IsKind(SyntaxKind.EndIfDirectiveTrivia))) { - // Ignore this comma as it follows a conditional preprocessor directive return; } From 78c2c164a647a0d16c19ea8675d2ca5ecd12152b Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Thu, 16 May 2024 08:01:32 -0400 Subject: [PATCH 5/8] Update documentation --- .../SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs | 1 - documentation/SA1001.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index 403934015..4ac18d2c3 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -30,7 +30,6 @@ internal class SA1001CommasMustBeSpacedCorrectly : DiagnosticAnalyzer /// The ID for diagnostics produced by the analyzer. /// public const string DiagnosticId = "SA1001"; - private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1001.md"; private static readonly LocalizableString Title = new LocalizableResourceString(nameof(SpacingResources.SA1001Title), SpacingResources.ResourceManager, typeof(SpacingResources)); private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(SpacingResources.SA1001MessageFormat), SpacingResources.ResourceManager, typeof(SpacingResources)); diff --git a/documentation/SA1001.md b/documentation/SA1001.md index b4a16b88d..ed0fba4d0 100644 --- a/documentation/SA1001.md +++ b/documentation/SA1001.md @@ -28,7 +28,7 @@ A comma should be followed by a single space, except in the following cases. * A comma may appear at the end of a line * A comma should not be followed by a space when used in an open generic type in a `typeof` expression * A comma is part of a string interpolation alignment component. For example:`$"{x,3}"` -* A comma follows a preprocessor directive. For example: +* A comma follows a conditional preprocessor directive (#if, #elif, #else, #endif). For example: ```csharp partial struct Money : IFormattable #if !NETSTANDARD From 523a357a2425655496875051939b8e9d3f1eb984 Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Wed, 12 Jun 2024 11:32:17 -0400 Subject: [PATCH 6/8] Updated code to check for trailing whitespace and extra space. Updated and added Tests --- .../SpacingRules/SA1001UnitTests.cs | 185 +++++++++++++++++- .../SA1001CommasMustBeSpacedCorrectly.cs | 36 +++- 2 files changed, 211 insertions(+), 10 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs index bc66ecd6e..0b9f19f7c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs @@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.SpacingRules using System; using System.Threading; using System.Threading.Tasks; + using Microsoft; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.SpacingRules; using Xunit; @@ -358,7 +359,7 @@ interface IFormattable {} interface ISpanFormattable {} partial struct Money : IFormattable -#if !NETSTANDARD +#if true , ISpanFormattable #endif { @@ -370,6 +371,188 @@ partial struct Money : IFormattable await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); } + [Fact] + public async Task TestCommaFollowingElifDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#elif true + , ISpanFormattable +#endif +{ +} +"; + + var expected = DiagnosticResult.EmptyDiagnosticResults; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestCommaFollowingElseDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#else + , ISpanFormattable +#endif +{ +} +"; + + var expected = DiagnosticResult.EmptyDiagnosticResults; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestCommaFollowingEndIfDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#endif + , ISpanFormattable +{ +} +"; + + var expected = DiagnosticResult.EmptyDiagnosticResults; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestCommaNotFollowingDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable + , ISpanFormattable +{ +} +"; + + var expected = new[] + { + DiagnosticResult.CompilerWarning("SA1001").WithSpan(6, 5, 6, 6).WithArguments(" not", "preceded"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestSpaceBeforeCommaFollowingIfDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if true + + , ISpanFormattable +#endif +{ +} +"; + + var expected = new[] + { + DiagnosticResult.CompilerWarning("SA1001").WithSpan(8, 5, 8, 6).WithArguments(" not", "preceded"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestSpaceBeforeCommaFollowingElifDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#elif true + + , ISpanFormattable +#endif +{ +} +"; + + var expected = new[] + { + DiagnosticResult.CompilerWarning("SA1001").WithSpan(9, 5, 9, 6).WithArguments(" not", "preceded"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestSpaceBeforeCommaFollowingElseDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#else + + , ISpanFormattable +#endif +{ +} +"; + + var expected = new[] + { + DiagnosticResult.CompilerWarning("SA1001").WithSpan(9, 5, 9, 6).WithArguments(" not", "preceded"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + public async Task TestSpaceBeforeCommaFollowingEndIfDirectiveAsync() + { + var testCode = @" +interface IFormattable {} +interface ISpanFormattable {} + +partial struct Money : IFormattable +#if false +#elif true +#endif + + , ISpanFormattable +{ +} +"; + + var expected = new[] + { + DiagnosticResult.CompilerWarning("SA1001").WithSpan(10, 5, 10, 6).WithArguments(" not", "preceded"), + }; + + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + } + private Task TestCommaInStatementOrDeclAsync(string originalStatement, DiagnosticResult expected, string fixedStatement) { return this.TestCommaInStatementOrDeclAsync(originalStatement, new[] { expected }, fixedStatement); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs index 4ac18d2c3..ff0e88e16 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs @@ -78,14 +78,7 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo } // Check if the comma follows a conditional preprocessor directive - if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia => - trivia.IsKind(SyntaxKind.IfDirectiveTrivia) || - trivia.IsKind(SyntaxKind.ElifDirectiveTrivia) || - trivia.IsKind(SyntaxKind.ElseDirectiveTrivia) || - trivia.IsKind(SyntaxKind.EndIfDirectiveTrivia))) - { - return; - } + bool followsDirective = token.HasLeadingTrivia && IsPrecededByDirectiveTrivia(token.LeadingTrivia); // check for a following space bool missingFollowingSpace = true; @@ -113,7 +106,7 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo } } - if (token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken)) + if (!followsDirective && (token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken))) { // comma should{ not} be {preceded} by whitespace context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePrecedingPreserveLayout, " not", "preceded")); @@ -131,5 +124,30 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemoveFollowing, " not", "followed")); } } + + private static bool IsPrecededByDirectiveTrivia(SyntaxTriviaList triviaList) + { + int triviaIndex = triviaList.Count - 1; + while (triviaIndex >= 0) + { + switch (triviaList[triviaIndex].Kind()) + { + case SyntaxKind.WhitespaceTrivia: + triviaIndex--; + break; + + case SyntaxKind.IfDirectiveTrivia: + case SyntaxKind.ElifDirectiveTrivia: + case SyntaxKind.ElseDirectiveTrivia: + case SyntaxKind.EndIfDirectiveTrivia: + return true; + + default: + return false; + } + } + + return false; + } } } From 1c48cde1b75c932beae22d47de847a83f085af4e Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Tue, 25 Jun 2024 14:29:26 -0400 Subject: [PATCH 7/8] Updated unit tests with diagnostic location --- .../SpacingRules/SA1001UnitTests.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs index 0b9f19f7c..58b3b06fc 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs @@ -441,14 +441,14 @@ interface IFormattable {} interface ISpanFormattable {} partial struct Money : IFormattable - , ISpanFormattable + {|#0:,|} ISpanFormattable { } "; var expected = new[] { - DiagnosticResult.CompilerWarning("SA1001").WithSpan(6, 5, 6, 6).WithArguments(" not", "preceded"), + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); @@ -464,7 +464,7 @@ interface ISpanFormattable {} partial struct Money : IFormattable #if true - , ISpanFormattable + {|#0:,|} ISpanFormattable #endif { } @@ -472,7 +472,7 @@ partial struct Money : IFormattable var expected = new[] { - DiagnosticResult.CompilerWarning("SA1001").WithSpan(8, 5, 8, 6).WithArguments(" not", "preceded"), + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); @@ -489,7 +489,7 @@ partial struct Money : IFormattable #if false #elif true - , ISpanFormattable + {|#0:,|} ISpanFormattable #endif { } @@ -497,7 +497,7 @@ partial struct Money : IFormattable var expected = new[] { - DiagnosticResult.CompilerWarning("SA1001").WithSpan(9, 5, 9, 6).WithArguments(" not", "preceded"), + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); @@ -514,7 +514,7 @@ partial struct Money : IFormattable #if false #else - , ISpanFormattable + {|#0:,|} ISpanFormattable #endif { } @@ -522,7 +522,7 @@ partial struct Money : IFormattable var expected = new[] { - DiagnosticResult.CompilerWarning("SA1001").WithSpan(9, 5, 9, 6).WithArguments(" not", "preceded"), + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); @@ -540,14 +540,14 @@ partial struct Money : IFormattable #elif true #endif - , ISpanFormattable + {|#0:,|} ISpanFormattable { } "; var expected = new[] { - DiagnosticResult.CompilerWarning("SA1001").WithSpan(10, 5, 10, 6).WithArguments(" not", "preceded"), + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); From f8a89a28209e57a94e4a98954274208da1a1208a Mon Sep 17 00:00:00 2001 From: Matt Chaulklin Date: Thu, 1 Aug 2024 13:12:06 -0400 Subject: [PATCH 8/8] Updated tests --- .../SpacingRules/SA1001UnitTests.cs | 98 +------------------ 1 file changed, 4 insertions(+), 94 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs index 58b3b06fc..618fccb4d 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs @@ -8,7 +8,6 @@ namespace StyleCop.Analyzers.Test.SpacingRules using System; using System.Threading; using System.Threading.Tasks; - using Microsoft; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.SpacingRules; using Xunit; @@ -446,26 +445,12 @@ partial struct Money : IFormattable } "; - var expected = new[] - { - Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), - }; - - await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); - } - - [Fact] - public async Task TestSpaceBeforeCommaFollowingIfDirectiveAsync() - { - var testCode = @" + var fixedCode = @" interface IFormattable {} interface ISpanFormattable {} -partial struct Money : IFormattable -#if true - - {|#0:,|} ISpanFormattable -#endif +partial struct Money : IFormattable, + ISpanFormattable { } "; @@ -475,82 +460,7 @@ partial struct Money : IFormattable Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), }; - await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); - } - - [Fact] - public async Task TestSpaceBeforeCommaFollowingElifDirectiveAsync() - { - var testCode = @" -interface IFormattable {} -interface ISpanFormattable {} - -partial struct Money : IFormattable -#if false -#elif true - - {|#0:,|} ISpanFormattable -#endif -{ -} -"; - - var expected = new[] - { - Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), - }; - - await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); - } - - [Fact] - public async Task TestSpaceBeforeCommaFollowingElseDirectiveAsync() - { - var testCode = @" -interface IFormattable {} -interface ISpanFormattable {} - -partial struct Money : IFormattable -#if false -#else - - {|#0:,|} ISpanFormattable -#endif -{ -} -"; - - var expected = new[] - { - Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), - }; - - await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); - } - - [Fact] - public async Task TestSpaceBeforeCommaFollowingEndIfDirectiveAsync() - { - var testCode = @" -interface IFormattable {} -interface ISpanFormattable {} - -partial struct Money : IFormattable -#if false -#elif true -#endif - - {|#0:,|} ISpanFormattable -{ -} -"; - - var expected = new[] - { - Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), - }; - - await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } private Task TestCommaInStatementOrDeclAsync(string originalStatement, DiagnosticResult expected, string fixedStatement)