Skip to content

Commit

Permalink
Fixing a few more edge cases with trailing comments caught by csharpi… (
Browse files Browse the repository at this point in the history
#1319)

…er-repos formatting
  • Loading branch information
belav authored Aug 17, 2024
1 parent 6af0e81 commit f2fa42d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions Src/CSharpier.Tests/DisabledTextComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,6 @@ CallMethod(_ =>
]
);

int[] someArray =
[
1
#if DEBUG
,
2
#endif
];

int[] someArray =
[
1
#if !DEBUG
,
2
#endif
];

class MyClass
{
private readonly List<string> _items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion Src/CSharpier/DisabledTextComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ private static Doc Print<T>(
{
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));
}
Expand Down

0 comments on commit f2fa42d

Please sign in to comment.