better_require_trailing_commas 2.2.0-dev.1 copy "better_require_trailing_commas: ^2.2.0-dev.1" to clipboard
better_require_trailing_commas: ^2.2.0-dev.1 copied to clipboard

A lint rule that enforces trailing commas behaves like ESLint’s comma-dangle with the always-multiline option.

better_require_trailing_commas #

Dart linter plugin to enforce trailing commas (enhanced version of linter built-in require_trailing_commas)

The rule is simple. If you opened parentheses intending to write multiline contents, it requires trailing commas. It's like always-multilinesetting of comma-dangle rule in ESLint.

Rules #

better_require_trailing_commas #

Enforces trailing commas for multiline collections, function calls, function definitions, and other constructs that support trailing commas.

✅ Good

Foo(
  a: 1,
  b: 2,
);

Foo(Bar(
  a: 1,
  b: 2,
));

useEffect(() {
  // ...
}, []);

❌️ Bad

Foo(
  a: 1,
  b: 2 // <-
);

Foo(Bar(
  a: 1,
  b: 2 // <-
));

useEffect(
  () {
    // ...
  },
  [] // <-
);

// Records are supported
final (String, String) foo = (
  "bar",
  "baz" // <-
);

// Switch expressions are supported
final foo = switch (state) {
  State.success => 0,
  State.failure => 1 // <-
}

// Enums are supported
enum FooState {
  bar,
  baz // <-
}

enum BarState {
  taro("taro"),
  hanako("hanako") // <-
  ;
  
  const BarState(this.name);

  final String name;
}

avoid_unnecessary_commas #

Disallows unnecessary trailing commas for single-line collections, function calls, function definitions, and other constructs that support trailing commas.

✅ Good

Foo(a: 1, b: 2);
Foo(Bar(
  baz: "baz",
));

❌️ Bad

Foo(a: 1, b: 2,); // <-
Foo(Bar(
  baz: "baz",
),); // <-

Installation #

Dart 3.10 or later (Flutter 3.38 or later) is required to use this plugin.

Edit your analysis_options.yaml like below. You don't need to add this to your dependencies (for more information, see here).

# ...
# Note: `plugins` property is TOP LEVEL.
plugins:
  # Note: Do not put `riverpod_lint` after this plugin.
  #       This may cause unexpected behavior.
  # riverpod_lint: ^3.2.0 # Put this BEFORE better_require_trailing_commas
  better_require_trailing_commas: ^2.2.0
# ...

Each rule can be disabled by diagnostics section like below:

plugins:
  better_require_trailing_commas:
    version: ^2.2.0
    diagnostics:
      avoid_unnecessary_commas: false

Running the linter with command line:

dart analyze
1
likes
150
points
335
downloads

Publisher

verified publisherchikach.net

Weekly Downloads

A lint rule that enforces trailing commas behaves like ESLint’s comma-dangle with the always-multiline option.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

analysis_server_plugin, analyzer, analyzer_plugin, meta

More

Packages that depend on better_require_trailing_commas