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

Skip Warnings for Commas After Conditional Directives #3839

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,27 @@ public void TestMethod()
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
[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
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +77,16 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo
return;
}

// Check if the comma follows a conditional preprocessor directive
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia =>
trivia.IsKind(SyntaxKind.IfDirectiveTrivia) ||
trivia.IsKind(SyntaxKind.ElifDirectiveTrivia) ||
trivia.IsKind(SyntaxKind.ElseDirectiveTrivia) ||
trivia.IsKind(SyntaxKind.EndIfDirectiveTrivia)))
{
return;
MattFromRVA marked this conversation as resolved.
Show resolved Hide resolved
}

// check for a following space
bool missingFollowingSpace = true;

Expand Down
6 changes: 6 additions & 0 deletions documentation/SA1001.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 conditional preprocessor directive (#if, #elif, #else, #endif). 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.

Expand Down