Skip to content

Commit

Permalink
Merge pull request #3813 from bjornhellander/feature/sa1012-property-…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
sharwell authored Mar 8, 2024
2 parents f5f4ca9 + e6896b6 commit cedfcf9
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,135 @@

namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1012CSharp9UnitTests : SA1012CSharp8UnitTests
{
[Fact]
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
public async Task TestInAndPropertyPatternsAsync()
{
var testCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ( {|#0:{|}Length: < 5 } and { Length: < 5 });
}
}
";

var fixedCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ({ Length: < 5 } and { Length: < 5 });
}
}
";

DiagnosticResult[] expectedResults =
{
// SA1012: Opening brace should be followed by a space
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),

// SA1012: Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

await VerifyCSharpFixAsync(
testCode,
expectedResults,
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
public async Task TestInOrPropertyPatternsAsync()
{
var testCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ( {|#0:{|}Length: < 5 } or { Length: < 5 });
}
}
";

var fixedCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ({ Length: < 5 } or { Length: < 5 });
}
}
";

DiagnosticResult[] expectedResults =
{
// SA1012: Opening brace should be followed by a space
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),

// SA1012: Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

await VerifyCSharpFixAsync(
testCode,
expectedResults,
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")]
public async Task TestInParenthesizedPropertyPatternsAsync()
{
var testCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ( {|#0:{|}Length: < 5 });
}
}
";

var fixedCode = @"
class C
{
public static bool HasValidLength(string s)
{
return s is ({ Length: < 5 });
}
}
";

DiagnosticResult[] expectedResults =
{
// SA1012: Opening brace should be followed by a space
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),

// SA1012: Opening brace should not be preceded by a space
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
};

await VerifyCSharpFixAsync(
testCode,
expectedResults,
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause))
{
var prevToken = token.GetPreviousToken();
if (prevToken is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
if (prevToken.IsKind(SyntaxKind.OpenParenToken))
{
// value is ({ P: 0 }, { P: 0 })
// value is ({ P: 0 } and { P: 0 })
// value is ({ P: 0 } or { P: 0 })
// value is ({ P: 0 })
expectPrecedingSpace = false;
}
else if (prevToken is { RawKind: (int)SyntaxKind.OpenBracketToken, Parent: { RawKind: (int)SyntaxKindEx.ListPattern } })
Expand Down

0 comments on commit cedfcf9

Please sign in to comment.