From c7fd405e3dd34287ad097f966a9727227b0dfe48 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 17 Aug 2024 12:22:25 -0500 Subject: [PATCH] Fixing a few more edge cases with trailing comments caught by csharpier-repos formatting --- .github/workflows/format_repositories.yml | 2 +- .../DisabledTextComparerTests.cs | 19 +++++ .../TestFiles/cs/CollectionExpressions.test | 18 ----- .../TestFiles/cs/EnumDeclarations.test | 22 ----- .../cs/TrailingComma_Directives.test | 81 +++++++++++++++++++ Src/CSharpier/DisabledTextComparer.cs | 2 +- .../SyntaxPrinter/SeparatedSyntaxList.cs | 7 +- 7 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_Directives.test diff --git a/.github/workflows/format_repositories.yml b/.github/workflows/format_repositories.yml index 5eb8823e8..18359d7b7 100644 --- a/.github/workflows/format_repositories.yml +++ b/.github/workflows/format_repositories.yml @@ -18,4 +18,4 @@ jobs: repository: belav/csharpier-repos path: csharpier-repos - run: dotnet build csharpier/Src/CSharpier.Cli/CSharpier.Cli.csproj -c release - - run: dotnet csharpier/Src/CSharpier.Cli/bin/release/net7.0/dotnet-csharpier.dll csharpier-repos --skip-write + - run: dotnet csharpier/Src/CSharpier.Cli/bin/release/net8.0/dotnet-csharpier.dll csharpier-repos --skip-write diff --git a/Src/CSharpier.Tests/DisabledTextComparerTests.cs b/Src/CSharpier.Tests/DisabledTextComparerTests.cs index 49c5136d3..24b36e0ea 100644 --- a/Src/CSharpier.Tests/DisabledTextComparerTests.cs +++ b/Src/CSharpier.Tests/DisabledTextComparerTests.cs @@ -46,6 +46,25 @@ public enum Enum Squash(before).Should().Be(Squash(after)); } + [Test] + public void Squash_Should_Work_For_Trailing_Comma_With_Attribute() + { + var before = """ + [ + SomeAttribute, + ] + public class ClassName { } + """; + + var after = """ + [SomeAttribute] + public class ClassName { } + + """; + + Squash(before).Should().Be(Squash(after)); + } + [Test] public void Squash_Should_Work_With_Pointer_Stuff() { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test index 27be13027..f0b0d6eb4 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CollectionExpressions.test @@ -86,24 +86,6 @@ CallMethod(_ => ] ); -int[] someArray = -[ - 1 -#if DEBUG - , - 2 -#endif -]; - -int[] someArray = -[ - 1 -#if !DEBUG - , - 2 -#endif -]; - class MyClass { private readonly List _items; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/EnumDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/EnumDeclarations.test index 523c00b39..b2012eab1 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/EnumDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/EnumDeclarations.test @@ -11,25 +11,3 @@ public enum E B = A, C = 2 + A, } - -public enum EnumWithDirective -{ - A, - B = A, - C = 2 + A -#if DEBUG - , - D = 4, -#endif -} - -public enum EnumWithDirective2 -{ - A, - B = A, - C = 2 + A, -#if DEBUG - D = 4, -#endif - E = 5, -} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_Directives.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_Directives.test new file mode 100644 index 000000000..cca7f21bc --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_Directives.test @@ -0,0 +1,81 @@ +int[] someArray = +[ + 1 +#if DEBUG + , + 2 +#endif +]; + +int[] someArray = +[ + 1 +#if !DEBUG + , + 2 +#endif +]; + +int[] someArray = +[ + 1, +#if DEBUG + 2 +#endif +]; + +int[] someArray = +[ + 1, +#if !DEBUG + 2 +#endif +]; + +public class ClassName +{ + private SomeCollection someCollection = new SomeCollection() + { + new SomeValue(), +#if DEBUG + new SomeValue(), + new SomeValue() +#endif + }; + + private SomeCollection someCollection = new SomeCollection() + { + new SomeValue() +#if DEBUG + , + new SomeValue(), + new SomeValue() +#endif + }; +} + +public enum SomeEnum +{ + Value1 +#if DEBUG + , + Value2, +#endif +} + +public enum SomeEnum +{ + Value1, +#if DEBUG + Value2, +#endif +} + +public enum SomeEnum +{ + Value1, +#if DEBUG + Value2, +#endif + Value3, +} diff --git a/Src/CSharpier/DisabledTextComparer.cs b/Src/CSharpier/DisabledTextComparer.cs index 0ea78a60d..8f3df1d71 100644 --- a/Src/CSharpier/DisabledTextComparer.cs +++ b/Src/CSharpier/DisabledTextComparer.cs @@ -95,7 +95,7 @@ is not ( // this removes trailing commas so that added trailing commas inside #if blocks don't result // in validation failures - if (nextChar is '}' && result.Length > 1 && result[^2] is ',') + if (nextChar is '}' or ']' && result.Length > 1 && result[^2] is ',') { result[^2] = nextChar; result.Length -= 1; diff --git a/Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs b/Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs index baab1ff22..29a936bf7 100644 --- a/Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs +++ b/Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs @@ -61,7 +61,12 @@ private static Doc Print( { var trailingSeparatorToken = list.GetSeparator(x); // when the trailing separator has trailing comments, we have to print it normally to prevent it from collapsing - if (trailingSeparatorToken.TrailingTrivia.Any(o => o.IsComment())) + // when the closing token has a directive, we can't assume the comma should be added/removed so just print it normally + if ( + trailingSeparatorToken.TrailingTrivia.Any(o => o.IsComment()) + || closingToken != null + && closingToken.Value.LeadingTrivia.Any(o => o.IsDirective) + ) { docs.Add(Token.Print(trailingSeparatorToken, context)); }