From f518fd803527e4d1568266c7606e7562e20f37b1 Mon Sep 17 00:00:00 2001 From: belav Date: Tue, 18 May 2021 21:48:36 -0500 Subject: [PATCH 1/3] fixing issue with interpolated verbatim string literal also fixing some issues with unit tests closes #221 --- .../CommandLineFormatterTests.cs | 19 +++++++------- Src/CSharpier.Tests/LineEndingTests.cs | 26 ++++++++++++++++++- Src/CSharpier/CommandLineFormatter.cs | 5 ++-- Src/CSharpier/SyntaxPrinter/Token.cs | 11 ++++++-- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Src/CSharpier.Tests/CommandLineFormatterTests.cs b/Src/CSharpier.Tests/CommandLineFormatterTests.cs index cae6fa072..124436ca7 100644 --- a/Src/CSharpier.Tests/CommandLineFormatterTests.cs +++ b/Src/CSharpier.Tests/CommandLineFormatterTests.cs @@ -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] @@ -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] @@ -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 lines) Format( @@ -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)); } @@ -206,7 +207,7 @@ public void WriteLine(string line = null) if (line != null) { - this.Lines.Add(line.Replace("\\", "/")); + this.Lines.Add(line); } } } diff --git a/Src/CSharpier.Tests/LineEndingTests.cs b/Src/CSharpier.Tests/LineEndingTests.cs index 4856b4a53..fe1a49a66 100644 --- a/Src/CSharpier.Tests/LineEndingTests.cs +++ b/Src/CSharpier.Tests/LineEndingTests.cs @@ -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 = @@ -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 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( diff --git a/Src/CSharpier/CommandLineFormatter.cs b/Src/CSharpier/CommandLineFormatter.cs index 15e89fa4d..49fb87f5d 100644 --- a/Src/CSharpier/CommandLineFormatter.cs +++ b/Src/CSharpier/CommandLineFormatter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Abstractions; @@ -50,8 +49,8 @@ public static async Task Format( 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) diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index 7afc416ba..64d9bb69f 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -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 { @@ -58,8 +60,13 @@ public static Doc PrintSyntaxToken( 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 interpolatedStringExpressionSyntax + && interpolatedStringExpressionSyntax.StringStartToken.Kind() + == 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)))); From d531c5692812cfd07c8d82277367e102024ea56e Mon Sep 17 00:00:00 2001 From: belav Date: Tue, 18 May 2021 21:53:40 -0500 Subject: [PATCH 2/3] self code review + disable this action to avoid the action failing because of this bug --- .github/workflows/validatePR.yml | 16 ++++++++-------- Src/CSharpier.Tests/LineEndingTests.cs | 5 +++-- Src/CSharpier.Tests/SyntaxNodeComparerTests.cs | 3 ++- Src/CSharpier/SyntaxPrinter/Token.cs | 4 +--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/validatePR.yml b/.github/workflows/validatePR.yml index ef96e5834..9d39d3cf6 100644 --- a/.github/workflows/validatePR.yml +++ b/.github/workflows/validatePR.yml @@ -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: +# runs-on: ubuntu-latest +# name: Check Formatting +# steps: +# - uses: actions/checkout@v2 +# - run: | +# dotnet tool restore +# dotnet csharpier --check diff --git a/Src/CSharpier.Tests/LineEndingTests.cs b/Src/CSharpier.Tests/LineEndingTests.cs index fe1a49a66..d6641f802 100644 --- a/Src/CSharpier.Tests/LineEndingTests.cs +++ b/Src/CSharpier.Tests/LineEndingTests.cs @@ -33,7 +33,7 @@ public void LineEndings_Should_Not_Affect_Printed_Output_With_Verbatim_String() [Test] public void LineEndings_Should_Not_Affect_Printed_Output_With_Interpolated_String() { - // this is a verbatim string that is just the right size to format differently if it has \n vs \r\n in it + // 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 { @@ -77,7 +77,8 @@ public void Escaped_LineEndings_In_Verbatim_String_Should_Remain( string escapedNewLine, EndOfLine endOfLine ) { - var code = @$"class ClassName + var code = + @$"class ClassName {{ string value = @""one{escapedNewLine}two""; }} diff --git a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs index 30b79b1f1..5560860fe 100644 --- a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs +++ b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs @@ -70,7 +70,8 @@ public ConstructorWithBase(string value) [Test] public void MissingAttribute() { - var left = @"class Resources + var left = + @"class Resources { [Obsolete] public Resources() diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index 64d9bb69f..6f0c0b3ad 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -64,9 +64,7 @@ public static Doc PrintSyntaxToken( && syntaxToken.Text.StartsWith("@")) || (syntaxToken.Kind() == SyntaxKind.InterpolatedStringTextToken && syntaxToken.Parent!.Parent - is InterpolatedStringExpressionSyntax interpolatedStringExpressionSyntax - && interpolatedStringExpressionSyntax.StringStartToken.Kind() - == SyntaxKind.InterpolatedVerbatimStringStartToken) + 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)))); From 68c2bb460ba73661693fb08de8f0f6cb6a740afc Mon Sep 17 00:00:00 2001 From: belav Date: Thu, 20 May 2021 20:27:42 -0500 Subject: [PATCH 3/3] add some extra tests --- .../StringLiteral/VerbatimStrings.cst | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst b/Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst index c07bc5f3c..9b0650f88 100644 --- a/Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst +++ b/Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst @@ -4,6 +4,7 @@ class ClassName { private string bothLineEndingsPersist = @"\r\n \n"; + private string interpolatedVersion = @$"\r\n \n"; private string emptyLines = @"one @@ -11,6 +12,15 @@ class ClassName two +three +"; + + private string interpolatedEmptyLines = + @$"one + +two + + three "; @@ -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() @@ -42,10 +63,27 @@ jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksd " ); + CallSomeLongMethod( + $@"one +two +three +four +jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf +" + ); + CallSomeLongMethod( @"one two three +four", + "two" + ); + + CallSomeLongMethod( + $@"one +two +three four", "two" );