belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Inconsistent Formatting for new() Operator Compared to Explicit Object Constructors #1364

Closed pfo-omicsstudio closed 3 weeks ago

pfo-omicsstudio commented 1 month ago

Input:

ProblemDetails problemDetails = new()
{
    Status = StatusCodes.Status500InternalServerError,
    Title = "An unexpected error occurred. Please try again later.",
    Detail = environment.IsDevelopment() ? exception.Message : null,
    Instance = context.Request.Path,
};

Output:

ProblemDetails problemDetails =
    new()
    {
        Status = StatusCodes.Status500InternalServerError,
        Title = "An unexpected error occurred. Please try again later.",
        Detail = environment.IsDevelopment() ? exception.Message : null,
        Instance = context.Request.Path,
    };

Expected behavior:

Currently, when using the new() operator, the formatting applies differently than when using the explicit object constructor. The explicit constructor formats like this:

ProblemDetails problemDetails = new ProblemDetails
{
    Status = StatusCodes.Status500InternalServerError,
    Title = "An unexpected error occurred. Please try again later.",
    Detail = environment.IsDevelopment() ? exception.Message : null,
    Instance = context.Request.Path,
};

It would be more consistent to apply the same formatting to the new() operator, like this:

ProblemDetails problemDetails = new()
{
    Status = StatusCodes.Status500InternalServerError,
    Title = "An unexpected error occurred. Please try again later.",
    Detail = environment.IsDevelopment() ? exception.Message : null,
    Instance = context.Request.Path,
};

The current behavior appears to contradict the formatting used for explicit object constructors.