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 issue with interpolated verbatim string literal #222

Merged
merged 3 commits into from
May 21, 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
16 changes: 8 additions & 8 deletions .github/workflows/validatePR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: dotnet test Src/CSharpier.Tests/CSharpier.Tests.csproj -c release
check_formatting:
runs-on: ubuntu-latest
name: Check Formatting
steps:
- uses: actions/checkout@v2
- run: |
dotnet tool restore
dotnet csharpier --check
# check_formatting:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out code in revision control is generally not a good thing 😏

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the last bug that has been screwing me over because when I format code locally it doesn't match how this action thinks the code should be formatted.

# runs-on: ubuntu-latest
# name: Check Formatting
# steps:
# - uses: actions/checkout@v2
# - run: |
# dotnet tool restore
# dotnet csharpier --check
19 changes: 10 additions & 9 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Format_Writes_Failed_To_Compile()

var result = this.Format();

result.lines.First().Should().Be(@"/Invalid.cs - failed to compile");
result.lines.First().Should().Contain(@"Invalid.cs - failed to compile");
}

[Test]
Expand Down Expand Up @@ -66,7 +66,7 @@ public void Format_Checks_Unformatted_File()

exitCode.Should().Be(1);
this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClass);
lines.First().Should().Contain(@"/Unformatted.cs - was not formatted");
lines.First().Should().Contain(@"Unformatted.cs - was not formatted");
}

[Test]
Expand Down Expand Up @@ -140,13 +140,14 @@ public void Ignore_Reports_Errors()

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

var path = this.fileSystem.Path.Combine(GetRootPath(), ".csharpierignore");

exitCode.Should().Be(1);
lines.Should()
.Contain(
$"The .csharpierignore file at {GetRootPath()}/.csharpierignore could not be parsed due to the following line:"
$"The .csharpierignore file at {path} could not be parsed due to the following line:"
);
// our testing code replaces the \ with /
lines.Should().Contain("/Src/Uploads/*.cs");
lines.Should().Contain(@"\Src\Uploads\*.cs");
}

private (int exitCode, IList<string> lines) Format(
Expand Down Expand Up @@ -178,18 +179,18 @@ public void Ignore_Reports_Errors()

private string GetRootPath()
{
return OperatingSystem.IsWindows() ? "c:/test" : "/Test";
return OperatingSystem.IsWindows() ? @"c:\test" : "/Test";
}

private string GetFileContent(string path)
{
path = Path.Combine(GetRootPath(), path);
path = this.fileSystem.Path.Combine(GetRootPath(), path);
return this.fileSystem.File.ReadAllText(path);
}

private void WhenThereExists(string path, string contents)
{
path = Path.Combine(GetRootPath(), path);
path = this.fileSystem.Path.Combine(GetRootPath(), path);
this.fileSystem.AddFile(path, new MockFileData(contents));
}

Expand All @@ -206,7 +207,7 @@ public void WriteLine(string line = null)

if (line != null)
{
this.Lines.Add(line.Replace("\\", "/"));
this.Lines.Add(line);
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions Src/CSharpier.Tests/LineEndingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CSharpier.Tests
public class LineEndingTests
{
[Test]
public void LineEndings_Should_Not_Affect_Printed_Output()
public void LineEndings_Should_Not_Affect_Printed_Output_With_Verbatim_String()
{
// this is a verbatim string that is just the right size to format differently if it has \n vs \r\n in it
var code =
Expand All @@ -30,6 +30,30 @@ public void LineEndings_Should_Not_Affect_Printed_Output()
lfResult.Code.Should().Be(crLfResult.Code.Replace("\r\n", "\n"));
}

[Test]
public void LineEndings_Should_Not_Affect_Printed_Output_With_Interpolated_String()
{
// this is a interpolated verbatim string that is just the right size to format differently if it has \n vs \r\n in it
var code =
@"class ClassName
{
private string blah = @$""one1111111111111111111111111111111
two
three
four"";
}
";
var codeWithLf = code.Replace("\r\n", "\n");
var codeWithCrLf = codeWithLf.Replace("\n", "\r\n");

var formatter = new CodeFormatter();
var printerOptions = new PrinterOptions { EndOfLine = EndOfLine.Auto, Width = 80 };
var lfResult = formatter.Format(codeWithLf, printerOptions);
var crLfResult = formatter.Format(codeWithCrLf, printerOptions);

lfResult.Code.Should().Be(crLfResult.Code.Replace("\r\n", "\n"));
}

[TestCase("\r\n", EndOfLine.LF)]
[TestCase("\n", EndOfLine.CRLF)]
public void LineEndings_In_Verbatim_String_Should_Respect_Options(
Expand All @@ -53,7 +77,8 @@ EndOfLine endOfLine
string escapedNewLine,
EndOfLine endOfLine
) {
var code = @$"class ClassName
var code =
@$"class ClassName
{{
string value = @""one{escapedNewLine}two"";
}}
Expand Down
3 changes: 2 additions & 1 deletion Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public ConstructorWithBase(string value)
[Test]
public void MissingAttribute()
{
var left = @"class Resources
var left =
@"class Resources
{
[Obsolete]
public Resources()
Expand Down
38 changes: 38 additions & 0 deletions Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
class ClassName
{
private string bothLineEndingsPersist = @"\r\n \n";
private string interpolatedVersion = @$"\r\n \n";

private string emptyLines =
@"one

two


three
";

private string interpolatedEmptyLines =
@$"one

two


three
";

Expand All @@ -26,9 +36,20 @@ jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksd
";

private string stayOnLine2 =
$@"one
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
";

private string stayOnLine3 =
@"one
two
three
four";

private string stayOnLine4 =
@$"one
two
three
four";

private void MethodName()
Expand All @@ -42,10 +63,27 @@ jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksd
"
);

CallSomeLongMethod(
$@"one
two
three
four
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
"
);

CallSomeLongMethod(
@"one
two
three
four",
"two"
);

CallSomeLongMethod(
$@"one
two
three
four",
"two"
);
Expand Down
5 changes: 2 additions & 3 deletions Src/CSharpier/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
Expand Down Expand Up @@ -50,8 +49,8 @@ IgnoreFile ignoreFile
IConsole console,
CancellationToken cancellationToken
) {
var baseDirectoryPath = File.Exists(commandLineOptions.DirectoryOrFile)
? Path.GetDirectoryName(commandLineOptions.DirectoryOrFile)
var baseDirectoryPath = fileSystem.File.Exists(commandLineOptions.DirectoryOrFile)
? fileSystem.Path.GetDirectoryName(commandLineOptions.DirectoryOrFile)
: commandLineOptions.DirectoryOrFile;

if (baseDirectoryPath == null)
Expand Down
9 changes: 7 additions & 2 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter.SyntaxNodePrinters;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier.SyntaxPrinter
{
Expand Down Expand Up @@ -58,8 +60,11 @@ public static Doc PrintWithSuffix(SyntaxToken syntaxToken, Doc suffixDoc)
ShouldSkipNextLeadingTrivia = false;

if (
syntaxToken.Kind() == SyntaxKind.StringLiteralToken
&& syntaxToken.Text.StartsWith("@")
(syntaxToken.Kind() == SyntaxKind.StringLiteralToken
&& syntaxToken.Text.StartsWith("@"))
|| (syntaxToken.Kind() == SyntaxKind.InterpolatedStringTextToken
&& syntaxToken.Parent!.Parent
is InterpolatedStringExpressionSyntax { StringStartToken: { RawKind: (int)SyntaxKind.InterpolatedVerbatimStringStartToken } } )
) {
var lines = syntaxToken.Text.Replace("\r", string.Empty).Split(new[] { '\n' });
docs.Add(Doc.Join(Doc.LiteralLine, lines.Select(o => new StringDoc(o))));
Expand Down