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

Extra newline added when using a collection expression with { get; } #1063

Closed
SapiensAnatis opened this issue Dec 9, 2023 · 3 comments · Fixed by #1065
Closed

Extra newline added when using a collection expression with { get; } #1063

SapiensAnatis opened this issue Dec 9, 2023 · 3 comments · Fixed by #1065
Assignees
Milestone

Comments

@SapiensAnatis
Copy link
Contributor

I see some of the formatting issues with C# 12 collection expressions were fixed in #1047 but I think I have found another one.

I'm using version 0.26.4 and have found that the following code:

public List<HttpRequestMessage> Requests { get; } =
    [
        new()
        {
            Content = new StringContent("content"),
            Headers = { { "header", "value" } },
            Method = HttpMethod.Post
        }
    ];

will get formatted to

public List<HttpRequestMessage> Requests { get; } =

    [
        new()
        {
            Content = new StringContent("content"),
            Headers = { { "header", "value" } },
            Method = HttpMethod.Post
        }
    ];

i.e. it leaves a blank line between the property declaration and the value. This isn't consistent with the behaviour for fields or auto-properties (=>) which don't add a blank line so I assume it's unintentional.

@SapiensAnatis
Copy link
Contributor Author

SapiensAnatis commented Dec 9, 2023

I'd be happy to contribute a fix by the way. From a very brief look it looks like this could be fixed by modifying the declaration of separator in CollectionExpression.cs such that it includes EqualsValueClauseSyntax with a parent of PropertyDeclarationSyntax, so:

                 or EqualsValueClauseSyntax
                 {
-                    Parent: not (PropertyDeclarationSyntax or VariableDeclaratorSyntax)
+                    Parent: not VariableDeclaratorSyntax
                 }
             ? Doc.Null

but since this seems to have been explicitly excluded, I'd like to understand why that is before I work on a pull request that would end up breaking stuff 😅

@belav
Copy link
Owner

belav commented Dec 9, 2023

I don't remember exactly what that does, but the tests should hopefully catch anything that you break. I was thinking it had to do with the ExpressionBody property in there, but I don't believe that contains an EqualsValueClause.

I also use this script to test changes against https://github.com/belav/csharpier-repos. The script probably requires changes for someone else to run.

TLDR, I put things in place to make sure that I also don't end up breaking stuff, so feel free to give it a shot!

@SapiensAnatis
Copy link
Contributor Author

.assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants