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

Fixing false positive of SyntaxCompare running on InterpolatedRawStri… #1132

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,50 @@ public void RawStringLiterals_Work_With_Moving_Indentation_2()

result.Should().BeEmpty();
}

[Test]
public void RawStringLiterals_Work_With_Moving_Indentation_3()
{
var left = """"
CallMethod(CallMethod(
$$"""
SomeString
""", someValue));
"""";
var right = """"
CallMethod(
CallMethod(
$$"""
SomeString
""",
someValue
)
);
"""";

var result = CompareSource(left, right);

result.Should().BeEmpty();
}

[Test]
public void RawStringLiterals_Error_With_Adding_Indentation_When_There_Was_None()
{
var left = """"
var x = $$"""

""";
"""";
var right = """"
var x = $$"""

""";
"""";

var result = CompareSource(left, right);

result.Should().NotBeEmpty();
}

private static void ResultShouldBe(string result, string be)
{
Expand Down
10 changes: 9 additions & 1 deletion Src/CSharpier/SyntaxNodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,19 @@ formattedNode is FileScopedNamespaceDeclarationSyntax fsnd
}
}

if (originalToken.Parent is InterpolatedStringExpressionSyntax && originalToken.Kind() is SyntaxKind.InterpolatedRawStringEndToken)
{
// this detects if we added indentation when there was none, or removed all indentation when there was some
if (originalToken.ValueText.TrimStart(['\r', '\n'])[0] != formattedToken.ValueText.TrimStart(['\r', '\n'])[0])
{
return NotEqual(originalToken.Span, formattedNode!.Span);
}
}
// 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.ValueText.Replace("\r", "") != formattedToken.ValueText.Replace("\r", ""))
else if (originalToken.ValueText.Replace("\r", "") != formattedToken.ValueText.Replace("\r", ""))
{
return NotEqual(
originalToken.RawSyntaxKind() == SyntaxKind.None
Expand Down
Loading