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

Fix issue with validation failing for mismatched line endings in verbatim strings #342

Merged
merged 1 commit into from
Jun 28, 2021
Merged
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
13 changes: 13 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ public void Should_Format_StandardInput_When_Provided()
lines.First().Should().Be(FormattedClassContent);
}

[Test]
public void File_With_Mismatched_Line_Endings_In_Verbatim_String_Should_Pass_Validation()
{
WhenAFileExists(
"file1.cs",
"public class ClassName\n{\npublic string Value = @\"EndThisLineWith\r\nEndThisLineWith\n\";\n}"
);

var (exitCode, _) = this.Format();

exitCode.Should().Be(0);
}

private (int exitCode, IList<string> lines) Format(
bool skipWrite = false,
bool check = false,
Expand Down
18 changes: 18 additions & 0 deletions Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ public class ClassName { }
);
}

[TestCase("@")]
[TestCase("@$")]
[TestCase("$@")]
public void Mismatched_Line_Endings_In_Verbatim_String_Should_Not_Print_Error(string start)
{
var left =
"public class ClassName\n{\npublic string Value = "
+ start
+ "\"EndThisLineWith\r\nEndThisLineWith\n\";\n}";
var right =
"public class ClassName\n{\npublic string Value = "
+ start
+ "\"EndThisLineWith\nEndThisLineWith\n\";\n}";

var result = this.AreEqual(left, right);
result.Should().BeEmpty();
}

private void ResultShouldBe(string result, string be)
{
if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null)
Expand Down
8 changes: 7 additions & 1 deletion Src/CSharpier/SyntaxNodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CSharpier.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace CSharpier
Expand Down Expand Up @@ -189,7 +191,11 @@ private CompareResult Compare(
SyntaxNode? originalNode,
SyntaxNode? formattedNode
) {
if (originalToken.Text != formattedToken.Text)
// when a verbatim string contains mismatched line endings they will become consistent
// this validation will fail unless we also get them consistent here
// adding a semi-complicated if check to determine when to do the string replacement
// did not appear to have any performance benefits
if (originalToken.Text.Replace("\r", "") != formattedToken.Text.Replace("\r", ""))
{
return NotEqual(
originalToken.RawKind == 0 ? originalNode?.Span : originalToken.Span,
Expand Down