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 a few more edge cases with trailing comments caught by csharpi… #1319

Merged
merged 1 commit into from
Aug 17, 2024
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
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
Loading