Skip to content

Commit 2f6d02f

Browse files
authored
Merge pull request #3511 from bjornhellander/feature/sa1012-property-list-pattern
Update SA1012 to expect no space between a property pattern's opening brace and an enclosing list pattern's opening bracket
2 parents 224763f + 293fa06 commit 2f6d02f

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1012CSharp11UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
9+
using StyleCop.Analyzers.Test.Verifiers;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
13+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
714

815
public class SA1012CSharp11UnitTests : SA1012CSharp10UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3509, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3509")]
19+
public async Task TestPropertyPatternInsideListPatternAsync()
20+
{
21+
var testCode = @"
22+
class C
23+
{
24+
void M(string[] a)
25+
{
26+
_ = a is [ {|#0:{|} Length: 1 }];
27+
_ = a is [{ Length: 0 },{|#1:{|} Length: 1 }];
28+
}
29+
}
30+
";
31+
32+
var fixedCode = @"
33+
class C
34+
{
35+
void M(string[] a)
36+
{
37+
_ = a is [{ Length: 1 }];
38+
_ = a is [{ Length: 0 }, { Length: 1 }];
39+
}
40+
}
41+
";
42+
43+
await new CSharpTest()
44+
{
45+
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
46+
TestCode = testCode,
47+
ExpectedDiagnostics =
48+
{
49+
// Opening brace should not be preceded by a space
50+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
51+
52+
// Opening brace should be preceded by a space
53+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
54+
},
55+
FixedCode = fixedCode,
56+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
57+
}
1058
}
1159
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,19 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
9292
}
9393

9494
bool expectPrecedingSpace = true;
95-
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause)
96-
&& token.GetPreviousToken() is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
95+
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause))
9796
{
98-
// value is ({ P: 0 }, { P: 0 })
99-
expectPrecedingSpace = false;
97+
var prevToken = token.GetPreviousToken();
98+
if (prevToken is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
99+
{
100+
// value is ({ P: 0 }, { P: 0 })
101+
expectPrecedingSpace = false;
102+
}
103+
else if (prevToken is { RawKind: (int)SyntaxKind.OpenBracketToken, Parent: { RawKind: (int)SyntaxKindEx.ListPattern } })
104+
{
105+
// value is [{ P: 0 }, { P: 0 }]
106+
expectPrecedingSpace = false;
107+
}
100108
}
101109

102110
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);

0 commit comments

Comments
 (0)