Skip to content

Commit

Permalink
Merge pull request #3511 from bjornhellander/feature/sa1012-property-…
Browse files Browse the repository at this point in the history
…list-pattern

Update SA1012 to expect no space between a property pattern's opening brace and an enclosing list pattern's opening bracket
  • Loading branch information
sharwell authored Dec 6, 2022
2 parents 224763f + 293fa06 commit 2f6d02f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,57 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1012CSharp11UnitTests : SA1012CSharp10UnitTests
{
[Fact]
[WorkItem(3509, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3509")]
public async Task TestPropertyPatternInsideListPatternAsync()
{
var testCode = @"
class C
{
void M(string[] a)
{
_ = a is [ {|#0:{|} Length: 1 }];
_ = a is [{ Length: 0 },{|#1:{|} Length: 1 }];
}
}
";

var fixedCode = @"
class C
{
void M(string[] a)
{
_ = a is [{ Length: 1 }];
_ = a is [{ Length: 0 }, { Length: 1 }];
}
}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
ExpectedDiagnostics =
{
// Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),

// Opening brace should be preceded by a space
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,19 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
}

bool expectPrecedingSpace = true;
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause)
&& token.GetPreviousToken() is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause))
{
// value is ({ P: 0 }, { P: 0 })
expectPrecedingSpace = false;
var prevToken = token.GetPreviousToken();
if (prevToken is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
{
// value is ({ P: 0 }, { P: 0 })
expectPrecedingSpace = false;
}
else if (prevToken is { RawKind: (int)SyntaxKind.OpenBracketToken, Parent: { RawKind: (int)SyntaxKindEx.ListPattern } })
{
// value is [{ P: 0 }, { P: 0 }]
expectPrecedingSpace = false;
}
}

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

0 comments on commit 2f6d02f

Please sign in to comment.