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 all 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,120 @@ 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 true
, ISpanFormattable
#endif
{
}
";

var expected = DiagnosticResult.EmptyDiagnosticResults;

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
{|#0:,|} ISpanFormattable
{
}
";

var fixedCode = @"
interface IFormattable {}
interface ISpanFormattable {}

partial struct Money : IFormattable,
ISpanFormattable
{
}
";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, 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,9 @@ 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
bool followsDirective = token.HasLeadingTrivia && IsPrecededByDirectiveTrivia(token.LeadingTrivia);

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

Expand All @@ -102,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"));
Expand All @@ -120,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;
}
}
}
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