Skip to content

Commit ebf7ca3

Browse files
committed
Merge pull request #1755 from vweijsters/fix-1663
SA1001 no longer accepts commas as first token on a line
2 parents f8ac672 + 5fcd853 commit ebf7ca3

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,23 @@ public async Task TestLastCommaInLineAsync()
7272
[Fact]
7373
public async Task TestFirstCommaInLineAsync()
7474
{
75-
string statement = $"f(a{Environment.NewLine}, b);";
76-
await this.TestCommaInStatementOrDeclAsync(statement, EmptyDiagnosticResults, statement).ConfigureAwait(false);
75+
string testStatement = $"f(a{Environment.NewLine}, b);";
76+
string fixedStatement = $"f(a,{Environment.NewLine}b);";
77+
78+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(8, 1);
79+
80+
await this.TestCommaInStatementOrDeclAsync(testStatement, expected, fixedStatement).ConfigureAwait(false);
7781
}
7882

7983
[Fact]
8084
public async Task TestCommentBeforeFirstCommaInLineAsync()
8185
{
82-
string statement = $"f(a // comment{Environment.NewLine}, b);";
83-
await this.TestCommaInStatementOrDeclAsync(statement, EmptyDiagnosticResults, statement).ConfigureAwait(false);
86+
string testStatement = $"f(a // comment{Environment.NewLine}, b);";
87+
string fixedStatement = $"f(a, // comment{Environment.NewLine}b);";
88+
89+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(8, 1);
90+
91+
await this.TestCommaInStatementOrDeclAsync(testStatement, expected, fixedStatement).ConfigureAwait(false);
8492
}
8593

8694
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class SA1001CommasMustBeSpacedCorrectly : DiagnosticAnalyzer
2727
/// </summary>
2828
public const string DiagnosticId = "SA1001";
2929
private const string Title = "Commas must be spaced correctly";
30-
private const string MessageFormat = "Commas must{0} be {1} by a space.";
30+
private const string MessageFormat = "Commas must{0} be {1} by whitespace.";
3131
private const string Description = "The spacing around a comma is incorrect, within a C# code file.";
3232
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1001.md";
3333

@@ -99,21 +99,15 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo
9999
}
100100
}
101101

102-
bool hasPrecedingSpace = false;
103-
if (!token.IsFirstInLine())
102+
if (token.IsFirstInLine() || token.IsPrecededByWhitespace())
104103
{
105-
hasPrecedingSpace = token.IsPrecededByWhitespace();
106-
}
107-
108-
if (hasPrecedingSpace)
109-
{
110-
// comma must{ not} be {preceded} by a space
111-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePreceding, " not", "preceded"));
104+
// comma must{ not} be {preceded} by whitespace
105+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePrecedingPreserveLayout, " not", "preceded"));
112106
}
113107

114108
if (missingFollowingSpace)
115109
{
116-
// comma must{} be {followed} by a space
110+
// comma must{} be {followed} by whitespace
117111
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.InsertFollowing, string.Empty, "followed"));
118112
}
119113
}

documentation/SA1001.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The spacing around a comma is incorrect, within a C# code file.
2323

2424
A violation of this rule occurs when the spacing around a comma is incorrect.
2525

26-
A comma should always be followed by a single space, unless it is the last character on the line, and a comma should never be preceded by any whitespace, unless it is the first character on the line.
26+
A comma should always be followed by a single space, unless it is the last character on the line, and a comma should never be preceded by any whitespace.
2727

2828
## How to fix violations
2929

0 commit comments

Comments
 (0)