Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Conversation

@srawlins
Copy link
Contributor

Fixes dart-lang/tools#990

This adds trailing commas in various places according to the following rules:

  • in a collection literal if there are more than one element
  • in an argument list if there are more than one argument (positional or named)
  • in a constructor parameter list, method parameter list, or function type parameter list, if there are more than one parameter (positional, optional, or named)

The last one was certainly the most complex, because of the optional/named delimiters ([]/{}).

  • A parameter list with one positional or one optional or one named parameter does not get a trailing comma.
  • A parameter list with multiple positional parameters and no optional or named parameters gets a trailing comma.
  • A parameter list with at least one optional parameter or at least one named parameter, and at least two parameters in total, gets a trailing comma before the closing delimiter (] or }).

@natebosch
Copy link
Contributor

I'm nervous about sending the signal that code_builder is intended to make "pretty" output, although nothing immediately comes to mind where we might need to have ugly output.

I would prefer if we err on the side of being opinionated over being configurable. If we think trailing commas is a better default I'd like to use it everywhere, rather than increase the API surface. WDYT @jakemac53 ?

@natebosch
Copy link
Contributor

I guess one other area that may come up is that we explicitly don't want to add options to have output that conforms to style lints. I would not accept PRs that, for instance, have options to conform to prefer single or double quotes, or to sort members within a class in a certain order.

@srawlins
Copy link
Contributor Author

I would prefer if we err on the side of being opinionated over being configurable.

I don't mind this idea. I thought there might be a question of breakage (internal goldens?) but if code_builder has some contract that we can change the textual content of the output code, then that's great.

I guess one other area that may come up is that we explicitly don't want to add options to have output that conforms to style lints.

Yeah my motivation here is 100% for the performance concerns in dart_style. I took the effort to make a distinction between 1-element lists and multiple-element lists to make the generated output bearable, for developers writing builders, and users ctrl-clicking into generated code. But if it comes up against a principle, I'm find just adding commas anywhere.

@sigurdm
Copy link

sigurdm commented Aug 18, 2022

Maybe it would be worth considering some recursive logic. If the single argument of an invocation itself uses trailing comma, then it should probably do as well...

Compare:

    foo(bar(
      takes,
      a,
      lot,
      of,
      arguments,
    ));

to

    foo(
      bar(
        takes,
        a,
        lot,
        of,
        arguments,
      ),
    );

I much prefer the second...

@jakemac53
Copy link
Contributor

I am fine with just always using them, at least for argument lists and list/map/set literals?

The place I really dislike the style is for parameter lists.

@srawlins
Copy link
Contributor Author

I've updated the code to always consider adding trailing commas. Trailing commas are still only added if there are multiple elements in a list. I deleted the tests I added as I think everything is covered with existing tests.

This feature still applies to parameter lists; let me know if it should not.

@jakemac53
Copy link
Contributor

This feature still applies to parameter lists; let me know if it should not.

I think it's fine, I will survive lol. Technically parameter lists could also get quite large, and bob also mentioned that if you have arrow body functions with a large expression, trailing commas in parameter lists will help.

Copy link
Contributor

@natebosch natebosch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a CHANGELOG?

@natebosch
Copy link
Contributor

I thought there might be a question of breakage (internal goldens?) but if code_builder has some contract that we can change the textual content of the output code, then that's great.

Yeah we have made changes to the specific output in the past - we consider a test on the specific content output to be opting in to needing updates.

It will make the next package roll more annoying since we will have to atomically update any golden tests, but that's typically pretty manageable.

@srawlins
Copy link
Contributor Author

Changelog added; thanks!

@srawlins srawlins merged commit f635ab6 into master Aug 18, 2022
@srawlins srawlins deleted the trailing-commas branch August 18, 2022 21:32
srawlins added a commit to dart-lang/mockito that referenced this pull request Sep 13, 2022
The big change affecting all of the goldens is that we now add trailing commas.
The motivation is that code with trailing commas can avoid pathological corners
of the formatter, which can save entire seconds in generating some Dart source
code.

See details at: dart-archive/code_builder#376

  - f635ab6e2776955d057cf75757905f73f76adbd9 Add support for trailing commas in Emitter (#376) by Sam Rawlins <srawlins@google.com>
  - e082adb3e01d619370e7ec14b5f8ae6ac8ecf903 Fix spelling in README.md (#360) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>
  - bed3ca90fb5a42b2b4c7fc93a8d73652a06d4314 Fix spelling in CHANGELOG.md (#361) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>

PiperOrigin-RevId: 469597645
srawlins added a commit to dart-lang/mockito that referenced this pull request Sep 13, 2022
The big change affecting all of the goldens is that we now add trailing commas.
The motivation is that code with trailing commas can avoid pathological corners
of the formatter, which can save entire seconds in generating some Dart source
code.

See details at: dart-archive/code_builder#376

  - f635ab6e2776955d057cf75757905f73f76adbd9 Add support for trailing commas in Emitter (#376) by Sam Rawlins <srawlins@google.com>
  - e082adb3e01d619370e7ec14b5f8ae6ac8ecf903 Fix spelling in README.md (#360) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>
  - bed3ca90fb5a42b2b4c7fc93a8d73652a06d4314 Fix spelling in CHANGELOG.md (#361) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>

PiperOrigin-RevId: 469597645
srawlins added a commit to dart-lang/mockito that referenced this pull request Sep 13, 2022
The big change affecting all of the goldens is that we now add trailing commas.
The motivation is that code with trailing commas can avoid pathological corners
of the formatter, which can save entire seconds in generating some Dart source
code.

See details at: dart-archive/code_builder#376

  - f635ab6e2776955d057cf75757905f73f76adbd9 Add support for trailing commas in Emitter (#376) by Sam Rawlins <srawlins@google.com>
  - e082adb3e01d619370e7ec14b5f8ae6ac8ecf903 Fix spelling in README.md (#360) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>
  - bed3ca90fb5a42b2b4c7fc93a8d73652a06d4314 Fix spelling in CHANGELOG.md (#361) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>

PiperOrigin-RevId: 469597645
srawlins added a commit to dart-lang/mockito that referenced this pull request Sep 14, 2022
The big change affecting all of the goldens is that we now add trailing commas.
The motivation is that code with trailing commas can avoid pathological corners
of the formatter, which can save entire seconds in generating some Dart source
code.

See details at: dart-archive/code_builder#376

  - f635ab6e2776955d057cf75757905f73f76adbd9 Add support for trailing commas in Emitter (#376) by Sam Rawlins <srawlins@google.com>
  - e082adb3e01d619370e7ec14b5f8ae6ac8ecf903 Fix spelling in README.md (#360) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>
  - bed3ca90fb5a42b2b4c7fc93a8d73652a06d4314 Fix spelling in CHANGELOG.md (#361) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>

PiperOrigin-RevId: 469597645
mosuem pushed a commit to dart-lang/test that referenced this pull request Oct 17, 2024
The big change affecting all of the goldens is that we now add trailing commas.
The motivation is that code with trailing commas can avoid pathological corners
of the formatter, which can save entire seconds in generating some Dart source
code.

See details at: dart-archive/code_builder#376

  - f635ab6e2776955d057cf75757905f73f76adbd9 Add support for trailing commas in Emitter (dart-lang/mockito#376) by Sam Rawlins <srawlins@google.com>
  - e082adb3e01d619370e7ec14b5f8ae6ac8ecf903 Fix spelling in README.md (dart-lang/mockito#360) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>
  - bed3ca90fb5a42b2b4c7fc93a8d73652a06d4314 Fix spelling in CHANGELOG.md (dart-lang/mockito#361) by Saint Gabriel <53136855+chineduG@users.noreply.github.com>

PiperOrigin-RevId: 469597645
mosuem pushed a commit to dart-lang/tools that referenced this pull request Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Option to generate trailing commas after lists of parameters

5 participants