Skip to content

Commit 293fa06

Browse files
Update SA1012 to expect no space between a property pattern's opening brace and an enclosing list pattern's opening bracket
#3509
1 parent 8c609a7 commit 293fa06

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
@@ -5,9 +5,57 @@
55

66
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
77
{
8+
using System.Threading;
9+
using System.Threading.Tasks;
810
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
11+
using StyleCop.Analyzers.Test.Verifiers;
12+
using Xunit;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
15+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
916

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

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)