Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1013 to allow whitespace before } when it starts a line inside interpolated string #3915

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading