-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Enforce trailing commas in object and collection initializer #668
Comments
I had considered this early on - #23. I think it was closed because we decided to not have csharpier changing anything besides whitespace. But there are good arguments for transforming code in some cases, and there are good arguments for forcing tailing commas. The discussions I found around making trailing commas the default in prettier - prettier/prettier#9369 and prettier/prettier#68 I think I am in favor of adding this. If it were to follow the prettier way, it would only include the trailing comma when the initializer breaks. |
Conversely, it would also be nice if CSharpier removed the trailing comma when the object initializer fits on single line. I think it's a common style guide to not have the final comma on a single line, and include it on multiple lines, i.e. this format: Dictionary<string, string> headers;
// No trailing comma after If-Match
headers = new() { { "OData-Version", "4.0" }, { "If-Match", "*" } };
// Trailing comma after If-Match
headers = new()
{
{ "OData-Version", "4.0" },
{ "Accept", "application/json;odata.metadata=minimal" },
{ "If-Match", "*" },
}; I think this makes a strong argument for CSharpier adding/removing that comma automatically:
|
Also csharpier could act as Black (popular python code formatter) about trailing commas. It forces multiline output if sees trailing comma. def foo(a: int, b: str,) -> None: will be formatted to def foo(
a: int,
b: str,
) -> None And vice versa this code def foo(
a: int,
b: str
) -> None: will be formatted to def foo(a: int, b: str) -> None: because it doesn't have trailing comma and statement fits to one line. It is really useful behaviour and gives power to users to force multiline output when it improves readability. |
@tkukushkin I'm personally against that. The biggest benefit of Prettier is to simply stop people from arguing about style and just get things done. Introducing choices especially to the degree of being able to vary place by place, will devolve it back into arguing about when should we break into multiple lines or not. |
I understand your point and I can say from my experience, that I've never seen any arguments over someone splitting one statement into several lines when it fits into one. Nobody cares about such things. But this is my personal experience and maybe your experience is different. I think Black was created with the same principle in mind, only few basic configuration options, it is uncompromising, opinionated. And this behaviour works for Black, then why wouldn't it work for csharpier? |
I'm not sure that's a good reasoning, on the flip side "Prettier doesn't allow you the force a line split at all, why wouldn't it work for CSharpier?" |
I'm gonna side with @NonSpicyBurrito on this one. I like that CSharpier (or Prettier) handles like length for me. Managing line breaks manually just feels wrong. Consider: after I (globally) rename a type in my solution, some method declaration may have significantly changed length. |
If new name is longer and some statement (method call for example) doesn't fit in one line anymore, it will be formatted as multiline, no changes to current behaviour here. If new name is shorter and some statement is already multiline, has trailing comma and could fit in one line, it will not be unwrapped, yes. And it is not common case IMHO, I have never reviewed each change manually, because it is not so important. There is no problem for me that some statements remain multiline. I think we can stop arguing about this, I understand your point and respect your decision. |
It would be really nice to have this as a configuration option. |
This pull request addresses the issue described in [Issue #668](#668) and [Issue #1285 ](#1285). A flag UsePrettierStyleTrailingCommas is introduced which ensures a trailing comma for anonymous object/collection/initializer/switch expressions, list patterns and enum declarations in the case a line break occurs. I hope the implementation of the feature isn't too invasive. Obviously it would be easier to implement the feature non-configurable. I'm not sure how to deal with the tests, for now I enabled the flag and adjusted the corresponding tests. Thanks for any feedback! --------- Co-authored-by: Bela VanderVoort <bela.vandervoort@optimizely.com>
Is it posible to turn off this feature ? I really thinks it downgrades the quality of source the code |
@martinib77 there will not be a configuration option added for this. Can you elaborate why you don't like it? There are plenty of arguments for why they should be included but I have not come across any against it. |
Sorry, code quality was not the best sentence. I wanted to refer to something more abstract, about the beauty and presentation of the code. Sentences that end in a comma and then close the block do not seem very pleasant to me. |
Could it be argued that since prettier has an option for that (
I share the feeling :( |
From my understanding, the reason Prettier has an option for |
I just noticed csharpier doing this and it really got my back up. Prettier has a config for this and our project has it turned off as trailing commas are gross for anyone with any sense of tidyness! |
Somehow many people, who lack any sense of tidyness apparently, like them. Including Prettier authors. One key philosophy of Prettier/CSharpier is to remove style discussion in project teams, by not providing options. Personally, there are other formatting choices from Prettier that I find way worse than a trailing comma, yet I just learnt to accept them and go along. Why? because overall the formatting is good, I like not having to bother with formatting and I value consistency in projects. Convenience wins over personal nitpicks. I think that if you want full control over tiny details like trailing commas, CSharpier might not be the right tool for you. |
Take the following code:
If later a new item is added:
This causes the following issues:
+2/-1
even though there isn't really any code deletion.These issues also occur when deleting/reordering the last item in initializers.
These issues can be fixed by enforcing trailing commas.
Relevant Prettier option: https://prettier.io/docs/en/options.html#trailing-commas
The text was updated successfully, but these errors were encountered: