Skip to main content

ban-name

added in: 4.12.0
warning
⚙️

Configure some names that you want to ban.

Example: When you add some extra functionalities to built-in Flutter functions (such as logging for showDialog), you may want to ban the original Flutter function and use your own version.

caution

When trying to ban some methods in your package, it also triggers on imported from an external package code. But you can specify the name to ban (ex: 'DateTime.now' instead of 'now').

Example

❌ Bad:

// suppose the configuration is the one shown below

showDialog('some_arguments', 'another_argument'); // LINT
material.showDialog('some_arguments', 'another_argument'); // LINT

var strangeName = 42; // LINT

void strangeName() {} // LINT

// LINT
class AnotherStrangeName {
late var strangeName; // LINT
}

StrangeClass.someMethod(); // LINT
NonStrangeClass.someMethod();

DateTime.now(); // LINT
DateTime.now().day; // LINT
class DateTimeTest {
final currentTimestamp = DateTime.now(); // LINT
}
DateTime currentTimestamp = DateTime('01.01.1959');

✅ Good:

myShowDialog();
NonStrangeClass.someMethod();
clock.now();
clock.now().day;
class DateTimeTest {
final currentTimestamp = clock.now();
}

⚙️ Config example

dart_code_metrics:
...
rules:
...
- ban-name:
entries:
- ident: showDialog
description: Please use myShowDialog in this package
- ident: strangeName
description: The name is too strange
- ident: AnotherStrangeName
description: Oops
- ident: StrangeClass.someMethod
description: Please use a NonStrangeClass.someMethod instead
- ident: DateTime.now
description: Please use a clock.now instead