question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No trailing comma for function calls with only a single argument

See original GitHub issue

This is a formatting request. When I enable trailingCommas: 'all' it always bothers me that Prettier adds a trailing comma to a function call with a single item. I think it would look aesthetically better if Prettier did not include a trailing comma only for function calls with a single argument. Below are two examples of code I wish didn’t include the trailing comma.

Prettier 1.18.2 Playground link

--parser babylon
--trailing-comma all

Input:

client.execute(Post.selectAll().where(Post.id.eq(42)).where(Post.published.eq(true)));

array.map(item => item.reallyReallyReallyReallyReallyReallyReallyReallyLongMethodName());

Output:

client.execute(
  Post.selectAll()
    .where(Post.id.eq(42))
    .where(Post.published.eq(true)),
);

array.map(item =>
  item.reallyReallyReallyReallyReallyReallyReallyReallyLongMethodName(),
);

Desired behavior:

client.execute(
  Post.selectAll()
    .where(Post.id.eq(42))
    .where(Post.published.eq(true))
);

array.map(item =>
  item.reallyReallyReallyReallyReallyReallyReallyReallyLongMethodName()
);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kaicataldocommented, Aug 6, 2019

Do you mind explaining why the number of arguments should matter here? The argument I usually see for enforcing trailing commas is to minimize diff noise, and it feels like doing this would defeat the purpose of using trailing commas at all.

1reaction
fiskercommented, Jun 3, 2020

I don’t think we can accept this, we print , for array and objects

Prettier 2.0.5 Playground link

--arrow-parens avoid
--parser babel
--trailing-comma all

Input:

a = [
  item => item.reallyReallyReallyReallyReallyReaongMethodNamereallyReallyLongMethodName()
];

a = {
  foo: item => item.reallyReallyReallyReallyReallyReaongMethodNamereallyReallyLongMethodName()
}

Output:

a = [
  item =>
    item.reallyReallyReallyReallyReallyReaongMethodNamereallyReallyLongMethodName(),
];

a = {
  foo: item =>
    item.reallyReallyReallyReallyReallyReaongMethodNamereallyReallyLongMethodName(),
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is a trailing comma in a function call not a syntax error?
Most languages accept trailing commas in comma-separated lists like argument lists or tuple or array literals. This is helpful if you split ...
Read more >
PHP 7.3: Allow trailing comma in function and method calls
This is a simple change, which suggests allowing trailing commas in function and method calls. This does not affect declarations.
Read more >
Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
Best practices for using trailing commas in JavaScript
A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of...
Read more >
Trailing comma for function arguments and call parameters
Thing to note: rest params. After the rest there should be no trailing comma in the function arguments list, but still can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found