Skip to content

Commit d3e7420

Browse files
authored
Merge pull request #3061 from sharwell/pattern-cases
Fix spacing after closing brace in patterns
2 parents 1ece164 + 6840f1c commit d3e7420

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1013CSharp8UnitTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,64 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly,
12+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
713

814
public class SA1013CSharp8UnitTests : SA1013CSharp7UnitTests
915
{
16+
/// <summary>
17+
/// Verifies the behavior of closing braces in case patterns.
18+
/// </summary>
19+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
20+
/// <seealso cref="SA1024CSharp8UnitTests.TestColonAfterClosingBraceInPatternAsync"/>
21+
[Fact]
22+
[WorkItem(3053, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3053")]
23+
public async Task TestSpacingAroundClosingBraceInPatternAsync()
24+
{
25+
const string testCode = @"using System;
26+
27+
public class Foo
28+
{
29+
public void TestMethod(object value)
30+
{
31+
switch (value)
32+
{
33+
// The space before ':' is not checked
34+
case ArgumentException { Message: { } message } :
35+
break;
36+
37+
// The space before 'message' is checked
38+
case Exception { Message: { }message }:
39+
break;
40+
}
41+
}
42+
}";
43+
const string fixedCode = @"using System;
44+
45+
public class Foo
46+
{
47+
public void TestMethod(object value)
48+
{
49+
switch (value)
50+
{
51+
// The space before ':' is not checked
52+
case ArgumentException { Message: { } message } :
53+
break;
54+
55+
// The space before 'message' is checked
56+
case Exception { Message: { } message }:
57+
break;
58+
}
59+
}
60+
}";
61+
62+
var expected = Diagnostic().WithSpan(14, 37, 14, 38).WithArguments(string.Empty, "followed");
63+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
64+
}
1065
}
1166
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1024CSharp8UnitTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,54 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.SpacingRules.SA1024ColonsMustBeSpacedCorrectly,
12+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
713

814
public class SA1024CSharp8UnitTests : SA1024CSharp7UnitTests
915
{
16+
/// <summary>
17+
/// Verifies the behavior of colons in case patterns.
18+
/// </summary>
19+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
20+
/// <seealso cref="SA1013CSharp8UnitTests.TestSpacingAroundClosingBraceInPatternAsync"/>
21+
[Fact]
22+
[WorkItem(3053, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3053")]
23+
public async Task TestColonAfterClosingBraceInPatternAsync()
24+
{
25+
const string testCode = @"using System;
26+
27+
public class Foo
28+
{
29+
public void TestMethod(object value)
30+
{
31+
switch (value)
32+
{
33+
case Exception { Message: { } message } :
34+
break;
35+
}
36+
}
37+
}";
38+
const string fixedCode = @"using System;
39+
40+
public class Foo
41+
{
42+
public void TestMethod(object value)
43+
{
44+
switch (value)
45+
{
46+
case Exception { Message: { } message }:
47+
break;
48+
}
49+
}
50+
}";
51+
52+
var expected = Diagnostic().WithSpan(9, 49, 9, 50).WithArguments(" not", "preceded", string.Empty);
53+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
54+
}
1055
}
1156
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1013ClosingBracesMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
1212
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Lightup;
1314

1415
/// <summary>
1516
/// A closing brace within a C# element is not spaced correctly.
@@ -100,7 +101,8 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
100101
|| nextToken.IsKind(SyntaxKind.SemicolonToken)
101102
|| nextToken.IsKind(SyntaxKind.DotToken)
102103
|| (nextToken.IsKind(SyntaxKind.QuestionToken) && nextToken.GetNextToken(includeZeroWidth: true).IsKind(SyntaxKind.DotToken))
103-
|| nextToken.IsKind(SyntaxKind.CloseBracketToken);
104+
|| nextToken.IsKind(SyntaxKind.CloseBracketToken)
105+
|| (nextToken.IsKind(SyntaxKind.ColonToken) && nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel));
104106
}
105107
else
106108
{

0 commit comments

Comments
 (0)