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

Trailing commas for enhanced enums are not preserved #1678

Closed
jellynoone opened this issue Mar 18, 2025 · 5 comments · May be fixed by #1703
Closed

Trailing commas for enhanced enums are not preserved #1678

jellynoone opened this issue Mar 18, 2025 · 5 comments · May be fixed by #1703

Comments

@jellynoone
Copy link

enum MyEnum {
  pending('pending'),
  awesome('awesome'),
  completed('completed'),
  ;

  const MyEnum(this.value);

  final String value;
}

This gets formatted into, even when trailing commas are preserved:

enum MyEnum {
  pending('pending'),
  awesome('awesome'),
  completed('completed');

  const MyEnum(this.value);

  final String value;
}
@munificent
Copy link
Member

munificent commented Mar 19, 2025

This is working as intended. The enum values are always split, which is the intent of preserving trailing commas. It looks strange to have the ; hanging out by itself doing nothing, so the formatter removes the last trailing comma and puts the ; instead, which is the idiomatic style.

I understand this does make it a tiny bit harder to edit the last value in the enum, but I believe the above output is what most users actually prefer so that's what it does.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2025
@jellynoone
Copy link
Author

jellynoone commented Apr 3, 2025

@munificent After rereading my initial comment here I think I wasn't as clear as I could have been.

My proposal here is that with the trailing_commas: preserve config flag, commas added to the last case of enhanced enums are preserved if they were intentionally placed there.

Correct, me if I'm wrong but doesn't the new formatter currently always split enhanced enums? So there is no difference between trailing_commas: preserve and trailing_commas: automate in this case?

I came to add this comment because it seems I'm not the only one who formats enums this way: #1660 (comment)

@munificent
Copy link
Member

@munificent After rereading my initial comment here I think I wasn't as clear as I could have been.

My proposal here is that with the trailing_commas: preserve config flag, commas added to the last case of enhanced enums are preserved if they were intentionally placed there.

Correct, me if I'm wrong but doesn't the new formatter currently always split enhanced enums?

It does not. You might be thinking of switch expressions, which are always split. But the new formatter will happily give you:

enum E { a, b }

I came to add this comment because it seems I'm not the only one who formats enums this way: #1660 (comment)

At the scale of our userbase, there are probably people who prefer just about every possible style option, but I have to be sensitive to using my time well. In this case, I suspect that there are not enough people for whom this style is important.

@jellynoone
Copy link
Author

It does not. You might be thinking of switch expressions, which are always split. But the new formatter will happily give you:

enum E { a, b }

I seem to got my terminology wrong then. I've always called enum E {a, b} an enum, a simple enum, and enums with members an enhanced enum.

At the scale of our userbase, there are probably people who prefer just about every possible style option, but I have to be sensitive to using my time well. In this case, I suspect that there are not enough people for whom this style is important.

I can understand this, and if you don't mind I've given this issue a stab at solving, and have opened a PR #1703.
The changes required for this were stupidly minimal and won't affect most users.

@munificent
Copy link
Member

It does not. You might be thinking of switch expressions, which are always split. But the new formatter will happily give you:
enum E { a, b }

I seem to got my terminology wrong then. I've always called enum E {a, b} an enum, a simple enum, and enums with members an enhanced enum.

Or, sorry, you're right.

At the scale of our userbase, there are probably people who prefer just about every possible style option, but I have to be sensitive to using my time well. In this case, I suspect that there are not enough people for whom this style is important.

I can understand this, and if you don't mind I've given this issue a stab at solving, and have opened a PR #1703. The changes required for this were stupidly minimal and won't affect most users.

I'm very busy the next couple of weeks, but I'll take a look when I get a chance. Thanks for the PR!

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