linter
linter copied to clipboard
proposal: `unnecessary_trailing_commas`
unnecessary_trailing_commas
Description
Do not use unnecessary trailing commas
Details
DO NOT use trailing commas for function calls and declarations if the function call or definition, from the start of the function name up to the closing parenthesis, fits in a single line.
Kind
Style
Good Examples
void func(bool param) {}
final list = [0];
final map = {0: 0};
Bad Examples
void func(
bool param,
) {}
final list = [
0,
];
final map = {
0: 0,
};
Discussion
Complement to require_trailing_commas
Extraneous commas increase the line count of files
Discussion checklist
- [x] List any existing rules this proposal modifies, complements, overlaps or conflicts with.
- [ ] List any relevant issues (reported here, the SDK Tracker, or elsewhere).
- [ ] If there's any prior art (e.g., in other linters), please add references here.
- [ ] If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn’t any corresponding advice, should there be?)
- [x] If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.
I find this proposal very useful as it happends to me very often to have just one parameter and linter adds this "unnecessary trailing comma". Looking forward for the update on this.
@goderbauer, @a14n: curious how compatible this advice is with current Flutter style guidelines?
AFAIK, we have no rule for this one and I don't think we'd want to enforce one. I don't think any of the "Good Examples" given above are necessarily better than the "Bad Examples" given.
For example, when writing a map literal it often helps with readebility to break that up into mutliple lines even when it would fit in a single line to make it easier to scan the keys and see what key-values belong together:
Good:
final Map<String, int> map = {
"key1": 100,
"key2": 200,
}
Also, we would probably never want this to apply to widget trees. It is almost always more readable to break that up into multiple lines even if it is not strictly necessary:
Good:
return FooWidget(
child: BarWidget(
value: "hello",
),
);
Same for function calls with required named parameters. Even though they may fit in one line, breaking them up into multiple with a trailing comma can make them more readable in my experience.
Should we close this?