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

Preserve Trailing Commas ! #1652

Open
stan-at-work opened this issue Feb 17, 2025 · 2 comments
Open

Preserve Trailing Commas ! #1652

stan-at-work opened this issue Feb 17, 2025 · 2 comments

Comments

@stan-at-work
Copy link

stan-at-work commented Feb 17, 2025

TL;DR: The new Dart formatter (3.7.0) automatically removes trailing commas, which changes code formatting in an undesirable way. This contradicts the require_trailing_commas linter rule and removes the ability to force line breaks. A workaround exists (adding // comments), but it's inconvenient. A setting to restore the old behavior or prevent automatic comma removal would be greatly appreciated.


Could there be an option to preserve the trailing commas, so they are not automatically removed? That way, developers who rely on them for readability or structure would still be able to use them without the formatter interfering. This would give us more control over the formatting style while still maintaining some level of automation.

I really liked the ability to force a line wrap by placing a comma at the end of a line. However, with the new formatter, this trailing comma is automatically removed.

This feels awkward and changes the code style in a way that I really don’t like.

It would be great if there were an option to prevent the automatic removal of trailing commas.

I'm not the only one raising concerns about this:

Reddit discussion
GitHub issue comment 1
GitHub issue comment 2


As jamesderlin pointed out, there’s an annoying workaround:

I think I'm going to have to resign myself to adding a superfluous empty // comment at every site where I don't want the formatter to remove an explicit trailing comma.

Sure, this keeps the comma, but having to do this for every line? That’s just monkey business... 🐵🍌


As intraector mentioned:

The formatter should not remove trailing commas, but it should add them.

Consider the following example:

Old style:

Material(
  color: Colors.transparent,
  child: InkWell(
    customBorder: const CircleBorder(),
    child: Padding(
      padding: const EdgeInsets.all(2),
      child: child,
    ),
    onTap: () {
      context.read<ScGalleryVm>().crop();
    },
  ),
)

New style:

Material(
  color: Colors.transparent,
  child: InkWell(
    customBorder: const CircleBorder(),
    child: Padding(padding: const EdgeInsets.all(2), child: child),
    onTap: () {
      context.read<ScGalleryVm>().crop();
    },
  ),
)

In some cases, the new formatting even contradicts the require_trailing_commas linter rule.


I completely agree require_trailing_commas should take precedence over this new change. It would be great if this behavior could be reconsidered.


Oneother example:

Old way:

Image

New way (BAD):
Image

My IDE even shows errors because it says a trailing comma is required. 🤕
Sure, the new approach removes seven lines, but the formatting is much harder to read.


👍 Yes, I like the proposal.

👎 No, I don't like the proposal.

@stan-at-work stan-at-work changed the title Option to Restore Old Dart Formatter (Pre-3.7.0) – Preserve Trailing Commas ! Preserve Trailing Commas ! Feb 17, 2025
@p4-k4
Copy link

p4-k4 commented Feb 17, 2025

The formatter should not remove trailing commas, but it should add them.

The formatter would have nothing to remove (reversibility) if it didn't add anything in the first place.
Therefore, it shouldn't add anything. Instead...

  • Let the formatter format and only format (not to be confused with adding/removing text).
  • Let the formatter acknowledge developer intent, rather than steal it from us.
  • Code actions are probably more suitable for removing or adding text.

@FaFre
Copy link

FaFre commented Feb 18, 2025

Running dart fix --code=require_trailing_commas after the format will restore that state

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

No branches or pull requests

3 participants