Skip to content

Commit cedfcf9

Browse files
authored
Merge pull request #3813 from bjornhellander/feature/sa1012-property-patterns-3812
Update SA1012 to forbid space before a property pattern, when it is preceeded by an opening parenthesis. Previously only handled positional patterns correctly.
2 parents f5f4ca9 + e6896b6 commit cedfcf9

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1012CSharp9UnitTests.cs

+126
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,135 @@
33

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

815
public partial class SA1012CSharp9UnitTests : SA1012CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
19+
public async Task TestInAndPropertyPatternsAsync()
20+
{
21+
var testCode = @"
22+
class C
23+
{
24+
public static bool HasValidLength(string s)
25+
{
26+
return s is ( {|#0:{|}Length: < 5 } and { Length: < 5 });
27+
}
28+
}
29+
";
30+
31+
var fixedCode = @"
32+
class C
33+
{
34+
public static bool HasValidLength(string s)
35+
{
36+
return s is ({ Length: < 5 } and { Length: < 5 });
37+
}
38+
}
39+
";
40+
41+
DiagnosticResult[] expectedResults =
42+
{
43+
// SA1012: Opening brace should be followed by a space
44+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),
45+
46+
// SA1012: Opening brace should not be preceded by a space
47+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
48+
};
49+
50+
await VerifyCSharpFixAsync(
51+
testCode,
52+
expectedResults,
53+
fixedCode,
54+
CancellationToken.None).ConfigureAwait(false);
55+
}
56+
57+
[Fact]
58+
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
59+
public async Task TestInOrPropertyPatternsAsync()
60+
{
61+
var testCode = @"
62+
class C
63+
{
64+
public static bool HasValidLength(string s)
65+
{
66+
return s is ( {|#0:{|}Length: < 5 } or { Length: < 5 });
67+
}
68+
}
69+
";
70+
71+
var fixedCode = @"
72+
class C
73+
{
74+
public static bool HasValidLength(string s)
75+
{
76+
return s is ({ Length: < 5 } or { Length: < 5 });
77+
}
78+
}
79+
";
80+
81+
DiagnosticResult[] expectedResults =
82+
{
83+
// SA1012: Opening brace should be followed by a space
84+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),
85+
86+
// SA1012: Opening brace should not be preceded by a space
87+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
88+
};
89+
90+
await VerifyCSharpFixAsync(
91+
testCode,
92+
expectedResults,
93+
fixedCode,
94+
CancellationToken.None).ConfigureAwait(false);
95+
}
96+
97+
[Fact]
98+
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
99+
public async Task TestInParenthesizedPropertyPatternsAsync()
100+
{
101+
var testCode = @"
102+
class C
103+
{
104+
public static bool HasValidLength(string s)
105+
{
106+
return s is ( {|#0:{|}Length: < 5 });
107+
}
108+
}
109+
";
110+
111+
var fixedCode = @"
112+
class C
113+
{
114+
public static bool HasValidLength(string s)
115+
{
116+
return s is ({ Length: < 5 });
117+
}
118+
}
119+
";
120+
121+
DiagnosticResult[] expectedResults =
122+
{
123+
// SA1012: Opening brace should be followed by a space
124+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),
125+
126+
// SA1012: Opening brace should not be preceded by a space
127+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
128+
};
129+
130+
await VerifyCSharpFixAsync(
131+
testCode,
132+
expectedResults,
133+
fixedCode,
134+
CancellationToken.None).ConfigureAwait(false);
135+
}
10136
}
11137
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1012OpeningBracesMustBeSpacedCorrectly.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
9595
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause))
9696
{
9797
var prevToken = token.GetPreviousToken();
98-
if (prevToken is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
98+
if (prevToken.IsKind(SyntaxKind.OpenParenToken))
9999
{
100100
// value is ({ P: 0 }, { P: 0 })
101+
// value is ({ P: 0 } and { P: 0 })
102+
// value is ({ P: 0 } or { P: 0 })
103+
// value is ({ P: 0 })
101104
expectPrecedingSpace = false;
102105
}
103106
else if (prevToken is { RawKind: (int)SyntaxKind.OpenBracketToken, Parent: { RawKind: (int)SyntaxKindEx.ListPattern } })

0 commit comments

Comments
 (0)