require-trailing-commas
Same as require_trailing_commas in Dart lints
- Group
- Style
- Path
lint/style/require-trailing-commas- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 431 of 512
Flags a multi-line call, declaration, assert, or collection literal that is missing a trailing comma.
When the opening and closing delimiters of an argument list, parameter list,
assert, or list/set/map literal sit on different lines, the last element
must be followed by a comma so the formatter keeps one element per line:
foo(a, b); // fine: fits on one line
foo(
a,
b // flagged at `)`
);
void f({
required int a // flagged at `}`
}) {}
A positional last element that itself spans several lines is exempt when it
is a block-bodied function literal, a string literal, an immediately
invoked function literal whose arguments fit on one line, or a list, set, or
map literal — the classic test('name', () { ... }); shape. Named
arguments and parameters never get the exemption. Record literals, type
argument lists, and extension type representation clauses are not checked.
Delimiters inside string interpolations ('${f(a, b)}') are matched like
any others.
Like the official lint, the rule is off in libraries at language version 3.7 or later, where the formatter's tall style owns trailing commas.
Invalid
// Tall-style formatting (language 3.7+) disables the lint; pin an older version.
// @dart = 3.6
void call(Object a, [Object? b, Object? c]) {}
void named({Object? a, Object? b}) {}
class Point {
final int x;
final int y;
Point(
this.x,
this.y)
: assert(x >= 0,
'x must be non-negative');
Point.origin({
this.x = 0,
this.y = 0});
}
void declaration(
int a,
int b) {}
void optional(
int a, [
int b = 0]) {}
void f() {
call('a',
'b');
named(a: 1,
b: () {
print('named function literals get no exemption');
});
final list = [
1,
2];
final map = {
'a': 1,
'b': 2};
assert(list.isNotEmpty,
map.toString());
call(1, 2,
3);
}
typedef Callback = void Function(
int a,
int b);
List<Object> nestedLast() => [
[0, 'a'],
[1, 'b']
];
String nestedInterpolation() => '${call(
1,
2)}'; this.x, this.y) : assert(x >= 0, 'x must be non-negative'); this.x = 0, this.y = 0}); int a, int b) {} int a, [ int b = 0]) {} call('a', 'b'); print('named function literals get no exemption'); }); 1, 2]; 'a': 1, 'b': 2}; assert(list.isNotEmpty, map.toString()); call(1, 2, 3); int a, int b); [1, 'b'] ]; 1, 2)}';Valid
// Tall-style formatting (language 3.7+) disables the lint; pin an older version.
// @dart = 3.6
void call(Object a, [Object? b, Object? c]) {}
void named({Object? a, Object? b}) {}
void test(String name, void Function() body) {}
class Point {
final int x;
final int y;
Point(
this.x,
this.y,
) : assert(x >= 0, 'x must be non-negative');
Point.origin({
this.x = 0,
this.y = 0,
});
}
void declaration(int a, int b) {}
void f() {
call('a', 'b');
// A multi-line block-bodied function literal as the last argument is exempt.
test('name', () {
print('body');
});
// So is a multi-line string literal ...
call('a', '''
first line
second line
''');
// ... and a multi-line collection literal.
call(1, [
2,
3,
]);
call(1, {
'a': 2,
});
final list = [
1,
2,
];
final map = {'a': 1, 'b': 2};
print(map);
named(
a: 1,
b: 2,
);
named();
}
final list = <int>[];
typedef Callback = void Function(
int a,
int b,
);
void interpolated() {
// Calls inside string interpolations are checked on their own delimiters.
test('${'x'.padLeft(2, ' ')}', () {});
test('group', () {
call(1);
});
}Configure require-trailing-commas
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"require-trailing-commas": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/require-trailing-commas: <reason>More style rules
- sized-box-shrink-expand
sized_box_shrink_expandrecommendedflutterPrefers
SizedBox.shrink()/SizedBox.expand()over aSizedBoxsized to zero or infinity. - slash-for-doc-comments
slash_for_doc_commentsrecommendedFlags
/** ... - sort-child-properties-last
sort_child_properties_lastrecommendedflutterSort Widget-valued child properties last, except before closure arguments.
- sort-constructors-first
sort_constructors_firstFlags a constructor declared after a non-constructor member.
- sort-unnamed-constructors-first
sort_unnamed_constructors_firstFlags an unnamed constructor declared after a named constructor.
- tighten-type-of-initializing-formals
tighten_type_of_initializing_formalsFlags a nullable
this.xorsuper.xinitializing formal whose constructor assertsx != null. - type-annotate-public-apis
type_annotate_public_apisrecommendedRequires type annotations on public declarations.
- type-init-formals
type_init_formalsrecommendedFlags a *redundant* type annotation on an initializing formal or super parameter (
type-init-formals, adopted from package:lints).