dart-code-metrics
dart-code-metrics copied to clipboard
[New rule] ban-element (or some better naming)
Please describe what the rule should do:
I'd like to have a more specialized version of ban-name rule – instead of banning by the name, it should rather ban usage of certain methods.
E.g., I would like to ban usage of List.sort() methods, but I'm totally fine with using other methods named sort.
It should provide a method to use instead. As an advanced version, it could provide a replacement as well.
If your rule is inspired by other please provide link to it:
ban-name but for specific methods.
What category of rule is this? (place an "X" next to just one item)
[ ] Warns about a potential error (problem) [x] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):
[1, 3, 2].sort(); // lint: `sort` methods is banned, use `sorted` instead
dart_code_metrics:
...
rules:
...
- ban-element:
entries:
- ident: dart:core/list.dart List.sort
description: Use `sorted` instead
Need to think about ident format though 🤔
Are you willing to submit a pull request to implement this rule?
Yes, probably.
I'd suggest to split method and library configuration ident: dart:core/list.dart List.sort to separate entries.
Also, have you considered extending the capabilities of ban-name instead of creating a separate rule? Or it's better to have it separated?
@incendial makes sense, we can tweak ban-name rule to e.g. have these parameters:
identifierName– current functionality ofban-name;elementName– to filter by element name,List.sort;library– to filter by library,dart:core/list.dart;
Probably, there is some better way to differentiate between identifierName (which would be just sort in the case of the List.sort) and elementName (which would be List.sort in this case). 🤔
Of even create a new rule based on ban-name like avoid-banned-names and improve the config however you want.
Probably, there is some better way to differentiate between identifierName (which would be just sort in the case of the List.sort) and elementName (which would be List.sort in this case). 🤔
I'm also curious, should there be any syntax difference between static class members and regular ones? Coz List.sort somehow looks more like a static method reference, than an instance one.
Available in Teams 1.3.0, https://dcm.dev/docs/rules/common/banned-usage/