Skip to main content

unnecessary-trailing-comma

added in: 1.2.0
⚙️🛠
Pro+
preset: recommended

Checks for unnecessary trailing commas for arguments, parameters, enum values and collections.

Removing those commas results in dart format producing code that fits on one line.

⚙️ Config

Set max-width (default is 80) to configure a custom max line width.

Set multiline (default is false) to highlight an unnecessary comma for arguments that take more than one line and are last (example).

Set trailing-lists (default is false) to highlight an unnecessary comma for list, set or map literals that are the last argument of an invocation (example).

analysis_options.yaml
dart_code_metrics:
rules:
- unnecessary-trailing-comma:
max-width: 80
multiline: false
trailing-lists: false

Example

❌ Bad:

void function(
String first,
String second, // LINT: Avoid unnecessary trailing comma. Try removing it.
) {
return;
}

✅ Good:

void function(String first, String second) {
return;
}

Example with "multiline"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- unnecessary-trailing-comma:
multiline: true

❌ Bad:

Invocation().thisOrAncestorMatching(
(someObject) => someObject is List<int>
? someObject.length
: int.tryParse(someObject.toString()), // LINT: Avoid unnecessary trailing comma. Try removing it.
);

✅ Good:

Invocation().thisOrAncestorMatching((someObject) => someObject is List<int>
? someObject.length
: int.tryParse(someObject.toString()));

Example with "trailing-lists"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- unnecessary-trailing-comma:
trailing-lists: true

❌ Bad:

withNamedList(
name: '',
values: [
true,
false,
], // LINT: Avoid unnecessary trailing comma. Try removing it.
);

✅ Good:

withNamedList( name: '', values: [
true,
false,
]);