Skip to content

Commit

Permalink
Update SA1013 to allow whitespace before } when it starts a line insi…
Browse files Browse the repository at this point in the history
…de interpolated string

Closes DotNetAnalyzers#3914
  • Loading branch information
morsiu committed Feb 6, 2025
1 parent f5843ae commit 4e05ed0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,39 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<
StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly>;

public partial class SA1013CSharp11UnitTests : SA1013CSharp10UnitTests
{
/// <summary>
/// Verifies the behavior of closing braces in interpolated strings.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3914, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3914")]
public async Task TestSpacingAroundClosingBraceInInterpolatedStringsAsync()
{
const string testCode = @"using System;
public class Foo
{
public string TestMethod(object value)
{
// The space before '}' is not checked
return $""Some random text {
value
} and some more random text."";
}
}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
return;
}

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

if (token.Parent is InterpolationSyntax)
{
if (precededBySpace)
if (precededBySpace && !firstInLine)
{
// Closing brace should{ not} be {preceded} by a space.
var properties = TokenSpacingProperties.RemovePreceding;
Expand Down Expand Up @@ -113,7 +114,7 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
precedesSpecialCharacter = false;
}

if (!precededBySpace)
if (!firstInLine && !precededBySpace)
{
// Closing brace should{} be {preceded} by a space.
var properties = TokenSpacingProperties.InsertPreceding;
Expand Down

0 comments on commit 4e05ed0

Please sign in to comment.