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

Quick Fix: Add trailing commas #27084

Closed
sethladd opened this issue Aug 16, 2016 · 18 comments
Closed

Quick Fix: Add trailing commas #27084

sethladd opened this issue Aug 16, 2016 · 18 comments
Labels
analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. customer-flutter P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@sethladd
Copy link
Contributor

sethladd commented Aug 16, 2016

Now that the formatter respects trailing commas, our users are asking if they can apply a "quick fix" to add trailing commas to their code.

See the nice effect of having trailing commas here: https://github.com/flutter/flutter/wiki/Early-Access:-Auto-Formatting

(This quick fix should also be plumbed into Atom and IntelliJ)

Thanks!!

@sethladd
Copy link
Contributor Author

cc @pq

@dvdwasibi
Copy link

This will be very useful for us since we are using the dart linter and formatter in our precommit-ci workflow.

Trailing commas provide better formatting for Flutter codebases and we want to enforce this.

@bwilkerson bwilkerson added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-quick-fix P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Aug 16, 2016
@pq pq added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. and removed area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Aug 16, 2016
@bwilkerson bwilkerson added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Aug 16, 2016
@bwilkerson
Copy link
Member

Trailing commas provide better formatting for Flutter codebases and we want to enforce this.

Wouldn't it be better to fix the formatter so that it works equally well either with or without a trailing comma, rather than to require all the extra trailing commas just to work around a formatter behavior you dislike?

@munificent

@dvdwasibi
Copy link

@bwilkerson, you're right.

Maybe I should I reword: it would be a useful "flag" to enable for us.

@lrhn
Copy link
Member

lrhn commented Aug 16, 2016

I don't think it's a matter of formatting. The Flutter codebase wants to keep every parameter/argument on a line by itself (at least for some code) and require a trailing comma so it's easy to move lines around.

@zoechi
Copy link
Contributor

zoechi commented Aug 16, 2016

dart-lang/dart_style#446 contains some of the discussion

@bwilkerson
Copy link
Member

Just to be clear, I have no objection to adding a quick-fix / assist, I just question whether there might be a better way to support this use case in the formatter than requiring extra characters in the code.

@munificent
Copy link
Member

That's a good question, but a hard one to hash out on a GitHub issue. If you want, throw a VC on my calendar and I'd be happy to talk about it. :)

@pq
Copy link
Member

pq commented Jun 29, 2018

A bunch of things have changed on this front. In particular, we now generate code more liberally with trailing commas. Given that I'm wondering if we are "good enough" or would a cleanup / fix still add value?

@zoechi
Copy link
Contributor

zoechi commented Jun 29, 2018

I'm still continually adding lots of commas on old code to improve formatting.

@pq
Copy link
Member

pq commented Jun 29, 2018

@munificent : would this be something you'd consider for dartfmt in it's current incarnation?

@munificent
Copy link
Member

Ugh, this is a surprisingly deep rathole of an issue. It is not at all clear which places an ideal dartfmt should or should not place trailing commas. If you look inside Flutter's own framework code, which is presumably pretty scrupulous about this style, there is no clear rule for which argument lists are supposed to have them and which aren't.

@devxpy
Copy link

devxpy commented Jun 13, 2019

@munificent I wonder if we could take some inspiration from black.

It seems to add a trailing comma, only if the line of code exceeds the character limit.

So while this

func("foobar", "barfoo", "asdf1234", "orange", "apple", "banana", "foobar", "barfoo", "asdf1234", "orange", "apple", "banana")

Gets formatted to this

func(
    "foobar",
    "barfoo",
    "asdf1234",
    "orange",
    "apple",
    "banana",
    "foobar",
    "barfoo",
    "asdf1234",
    "orange",
    "apple",
    "banana",
)

This stays as-is.

func("foobar", "barfoo")

Similar rules are applied to parameter declarations, collection literals, etc.

OTOH, dartfmt produces this:

func(
      "foobar",
      "barfoo",
      "asdf1234",
      "orange",
      "apple",
      "banana",
      "foobar",
      "barfoo",
      "asdf1234",
      "orange",
      "apple",
      "banana");

Which IMO looks weird.

@srawlins
Copy link
Member

There is now a quick fix for the require_trailing_commas lint rule

@ctsstc
Copy link

ctsstc commented Oct 7, 2022

Can this autofix during the formatter?

@srawlins
Copy link
Member

srawlins commented Oct 7, 2022

No, sorry. Just during dart fix.

@mclean25
Copy link

There is now a quick fix for the require_trailing_commas lint rule

@srawlins With what version of Dart?

@srawlins
Copy link
Member

It looks like this landed 12 months ago. I'm not sure what version of Dart that would be. 532c6d9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. customer-flutter P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests