Skip to content

Commit 33de76f

Browse files
committed
Update SA1505/SA1506/SA1510/SA1511 for pattern matching
1 parent 2adb2e6 commit 33de76f

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1505CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.LayoutRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.LayoutRules.SA1505OpeningBracesMustNotBeFollowedByBlankLine,
13+
StyleCop.Analyzers.LayoutRules.SA1505CodeFixProvider>;
714

815
public partial class SA1505CSharp8UnitTests : SA1505CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
19+
public async Task TestSwitchExpressionOpeningBraceFollowedByBlankLineAsync()
20+
{
21+
var testCode = @"
22+
public class TestClass
23+
{
24+
public int Test(Wrapper value)
25+
{
26+
return value switch
27+
{|#0:{|}
28+
29+
{ X: 1 } => 1,
30+
_ => 0,
31+
};
32+
}
33+
}
34+
35+
public class Wrapper
36+
{
37+
public int X { get; set; }
38+
}
39+
";
40+
var fixedCode = @"
41+
public class TestClass
42+
{
43+
public int Test(Wrapper value)
44+
{
45+
return value switch
46+
{
47+
{ X: 1 } => 1,
48+
_ => 0,
49+
};
50+
}
51+
}
52+
53+
public class Wrapper
54+
{
55+
public int X { get; set; }
56+
}
57+
";
58+
59+
var expected = Diagnostic().WithLocation(0);
60+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
61+
}
62+
63+
[Fact]
64+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
65+
public async Task TestPropertyPatternOpeningBraceFollowedByBlankLineAsync()
66+
{
67+
var testCode = @"
68+
public class TestClass
69+
{
70+
public bool Test(Wrapper value)
71+
{
72+
return value is Wrapper
73+
{|#0:{|}
74+
75+
X: 1,
76+
Y: 2,
77+
};
78+
}
79+
}
80+
81+
public class Wrapper
82+
{
83+
public int X { get; set; }
84+
85+
public int Y { get; set; }
86+
}
87+
";
88+
var fixedCode = @"
89+
public class TestClass
90+
{
91+
public bool Test(Wrapper value)
92+
{
93+
return value is Wrapper
94+
{
95+
X: 1,
96+
Y: 2,
97+
};
98+
}
99+
}
100+
101+
public class Wrapper
102+
{
103+
public int X { get; set; }
104+
105+
public int Y { get; set; }
106+
}
107+
";
108+
109+
var expected = Diagnostic().WithLocation(0);
110+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
111+
}
10112
}
11113
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1506CSharp8UnitTests.cs

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

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

814
public partial class SA1506CSharp8UnitTests : SA1506CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestDocumentationHeaderBeforeSwitchExpressionAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
/// <summary>
24+
/// Test method.
25+
/// </summary>
26+
{|#0:
27+
|} public int Test(int value) => value switch { 0 => 0, _ => 1 };
28+
}
29+
";
30+
var fixedCode = @"
31+
public class TestClass
32+
{
33+
/// <summary>
34+
/// Test method.
35+
/// </summary>
36+
public int Test(int value) => value switch { 0 => 0, _ => 1 };
37+
}
38+
";
39+
40+
var expected = Diagnostic().WithLocation(0);
41+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
42+
}
1043
}
1144
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1510CSharp8UnitTests.cs

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

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

814
public partial class SA1510CSharp8UnitTests : SA1510CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestCatchAfterSwitchExpressionWithBlankLineAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
public int Test(int value)
24+
{
25+
try
26+
{
27+
return value switch { 0 => 0, _ => 1 };
28+
}
29+
30+
{|#0:catch|} (System.Exception)
31+
{
32+
return -1;
33+
}
34+
}
35+
}
36+
";
37+
var fixedCode = @"
38+
public class TestClass
39+
{
40+
public int Test(int value)
41+
{
42+
try
43+
{
44+
return value switch { 0 => 0, _ => 1 };
45+
}
46+
catch (System.Exception)
47+
{
48+
return -1;
49+
}
50+
}
51+
}
52+
";
53+
54+
var expected = Diagnostic().WithLocation(0).WithArguments("catch");
55+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
56+
}
1057
}
1158
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1511CSharp8UnitTests.cs

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

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

814
public partial class SA1511CSharp8UnitTests : SA1511CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestDoWhileWithPatternConditionAndBlankLineAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
public void Test(object value)
24+
{
25+
do
26+
{
27+
value = new object();
28+
}
29+
30+
{|#0:while|} (value is int);
31+
}
32+
}
33+
";
34+
var fixedCode = @"
35+
public class TestClass
36+
{
37+
public void Test(object value)
38+
{
39+
do
40+
{
41+
value = new object();
42+
}
43+
while (value is int);
44+
}
45+
}
46+
";
47+
48+
var expected = Diagnostic().WithLocation(0);
49+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
50+
}
1051
}
1152
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1505OpeningBracesMustNotBeFollowedByBlankLine.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.LayoutRules
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
1313
using Microsoft.CodeAnalysis.Diagnostics;
1414
using StyleCop.Analyzers.Helpers;
15+
using StyleCop.Analyzers.Lightup;
1516

1617
/// <summary>
1718
/// An opening brace within a C# element, statement, or expression is followed by a blank line.
@@ -61,6 +62,8 @@ internal class SA1505OpeningBracesMustNotBeFollowedByBlankLine : DiagnosticAnaly
6162
private static readonly Action<SyntaxNodeAnalysisContext> NamespaceDeclarationAction = HandleNamespaceDeclaration;
6263
private static readonly Action<SyntaxNodeAnalysisContext> BaseTypeDeclarationAction = HandleBaseTypeDeclaration;
6364
private static readonly Action<SyntaxNodeAnalysisContext> AccessorListAction = HandleAccessorList;
65+
private static readonly Action<SyntaxNodeAnalysisContext> SwitchExpressionAction = HandleSwitchExpression;
66+
private static readonly Action<SyntaxNodeAnalysisContext> PropertyPatternClauseAction = HandlePropertyPatternClause;
6467

6568
/// <inheritdoc/>
6669
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -79,6 +82,8 @@ public override void Initialize(AnalysisContext context)
7982
context.RegisterSyntaxNodeAction(NamespaceDeclarationAction, SyntaxKind.NamespaceDeclaration);
8083
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
8184
context.RegisterSyntaxNodeAction(AccessorListAction, SyntaxKind.AccessorList);
85+
context.RegisterSyntaxNodeAction(SwitchExpressionAction, SyntaxKindEx.SwitchExpression);
86+
context.RegisterSyntaxNodeAction(PropertyPatternClauseAction, SyntaxKindEx.PropertyPatternClause);
8287
}
8388

8489
private static void HandleBlock(SyntaxNodeAnalysisContext context)
@@ -123,6 +128,18 @@ private static void HandleAccessorList(SyntaxNodeAnalysisContext context)
123128
AnalyzeOpenBrace(context, accessorList.OpenBraceToken);
124129
}
125130

131+
private static void HandleSwitchExpression(SyntaxNodeAnalysisContext context)
132+
{
133+
var switchExpression = (SwitchExpressionSyntaxWrapper)context.Node;
134+
AnalyzeOpenBrace(context, switchExpression.OpenBraceToken);
135+
}
136+
137+
private static void HandlePropertyPatternClause(SyntaxNodeAnalysisContext context)
138+
{
139+
var propertyPatternClause = (PropertyPatternClauseSyntaxWrapper)context.Node;
140+
AnalyzeOpenBrace(context, propertyPatternClause.OpenBraceToken);
141+
}
142+
126143
private static void AnalyzeOpenBrace(SyntaxNodeAnalysisContext context, SyntaxToken openBraceToken)
127144
{
128145
var nextToken = openBraceToken.GetNextToken();

0 commit comments

Comments
 (0)