Skip to content

Commit 6e5e389

Browse files
THammond9sharwell
authored andcommitted
Clarified documentation, removed indentation, and added additional tests
1 parent 582df15 commit 6e5e389

8 files changed

Lines changed: 46 additions & 9 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ public async Task TestSpaceAfterCommaWhenPartOfInterpolationAlignmentClauseAsync
190190
await this.TestCommaInStatementOrDeclAsync(statement, expected, fixedStatement).ConfigureAwait(false);
191191
}
192192

193+
[Fact]
194+
[WorkItem(2289, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2289")]
195+
public async Task TestSpaceBeforeAndAfterCommaWhenPartOfInterpolationAlignmentClauseAsync()
196+
{
197+
string statement = @"var x = new[] { 1, 2, 3, 4, 5, 6, 7 };
198+
var t = $""{x[2] , 3}"";";
199+
string fixedStatement = @"var x = new[] { 1, 2, 3, 4, 5, 6, 7 };
200+
var t = $""{x[2],3}"";";
201+
202+
DiagnosticResult[] expected =
203+
{
204+
this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(8, 29),
205+
this.CSharpDiagnostic().WithArguments(" not", "followed").WithLocation(8, 29),
206+
};
207+
208+
await this.TestCommaInStatementOrDeclAsync(statement, expected, fixedStatement).ConfigureAwait(false);
209+
}
210+
193211
[Fact]
194212
public async Task TestSpaceOnlyBeforeCommaAsync()
195213
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1011UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,24 @@ void Method()
349349
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
350350
}
351351

352+
[Fact]
353+
[WorkItem(2289, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2289")]
354+
public async Task TestCommaCanFollowSquareBracketWhenPartOfInterpolationFormatClauseAsync()
355+
{
356+
string testCode = @"
357+
class ClassName
358+
{
359+
void Method()
360+
{
361+
var x = new[] { 1, 2, 3, 4, 5, 6, 7 };
362+
var t = $""{ x[2],3}"";
363+
}
364+
}
365+
";
366+
367+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
368+
}
369+
352370
/// <inheritdoc/>
353371
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
354372
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace StyleCop.Analyzers.SpacingRules
1616
/// <remarks>
1717
/// <para>A violation of this rule occurs when the spacing around a comma is incorrect.</para>
1818
///
19-
/// <para>A comma should always be followed by a single space, unless it is the last character on the line
20-
/// or it is part of an alignment component, and a comma should never be preceded by any whitespace,
19+
/// <para>A comma should always be followed by a single space, unless it is the last character on the line or it is
20+
/// part of a string interpolation alignment component, and a comma should never be preceded by any whitespace,
2121
/// unless it is the first character on the line.</para>
2222
/// </remarks>
2323
[DiagnosticAnalyzer(LanguageNames.CSharp)]

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace StyleCop.Analyzers.SpacingRules
2222
///
2323
/// <para>A closing square bracket should be followed by whitespace, unless it is the last character on the line, it
2424
/// is followed by a closing bracket or an opening parenthesis, it is followed by a comma or semicolon, it is
25-
/// followed by a alignment component or format string component, or it is followed by certain types of operator
26-
/// symbols.</para>
25+
/// followed by a string interpolation alignment component or string interpolation formatting component, or it is
26+
/// followed by certain types of operator symbols.</para>
2727
/// </remarks>
2828
[DiagnosticAnalyzer(LanguageNames.CSharp)]
2929
internal class SA1011ClosingSquareBracketsMustBeSpacedCorrectly : DiagnosticAnalyzer

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1024ColonsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ namespace StyleCop.Analyzers.SpacingRules
4242
/// }
4343
/// </code>
4444
///
45-
/// <para>A colon that appears as part of format string component should not have leading whitespace characters. For
46-
/// example:</para>
45+
/// <para>A colon that appears as part of a string interpolation formatting component should not have leading
46+
/// whitespace characters. For example:</para>
4747
///
4848
/// <code language="cs">
4949
/// var s = $"{x:N}";

documentation/SA1001.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A comma should be followed by a single space, except in the following cases.
2727

2828
* A comma may appear at the end of a line
2929
* A comma should not be followed by a space when used in an open generic type in a `typeof` expression
30-
* A comma is part of an alignment component. For example:`$"{x,3}"`
30+
* A comma is part of a string interpolation alignment component. For example:`$"{x,3}"`
3131

3232
A comma should never be preceded by a space or appear as the first token on a line.
3333

documentation/SA1011.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ A closing square bracket should be followed by whitespace unless:
2929
* It is the last character on the line
3030
* It is followed by a closing bracket or an opening parenthesis
3131
* It is followed by a comma or semicolon
32-
* It is followed by a alignment component or format string component. For example: `$"{x[i],3}"` or `$"{x[i]:C}"`
32+
* It is followed by a string interpolation alignment component. For example: `$"{x[i]:C}"`
33+
* It is followed by a string interpolation formatting component. For example: `$"{x[i],3}"`
3334
* It is followed by certain types of operator symbols.
3435

3536
## How to fix violations

documentation/SA1024.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ switch (x)
4545
}
4646
```
4747

48-
A colon that appears as part of format string component should not have leading whitespace characters. For example:
48+
A colon that appears as part of a string interpolation formatting component should not have leading whitespace characters. For example:
4949

5050
```csharp
5151
var s = $"{x:N}";

0 commit comments

Comments
 (0)