Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

feat: add method call chains support on ban-name ident #1034

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,32 @@ class BanNameRule extends CommonRule {

source.unit.visitChildren(visitor);

return visitor.nodes
final filteredNodeList = visitor.nodes.where((node) {
if (node.endNode != null) {
final inlineNode = source.content
.substring(
node.node.offset,
node.endNode!.end,
)
.replaceAll('\n', '');

if (!inlineNode.contains(node.fullName)) {
return false;
}
}

return true;
});

return filteredNodeList
.map(
(node) => createIssue(
rule: this,
location: nodeLocation(node: node.node, source: source),
location: nodeLocation(
node: node.node,
source: source,
endNode: node.endNode,
),
message: node.message,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ part of 'ban_name_rule.dart';
class _Visitor extends GeneralizingAstVisitor<void> {
final Map<String, _BanNameConfigEntry> _entryMap;

final _nodes = <_NodeWithMessage>[];
final _nodes = <_NodeInfo>[];

Iterable<_NodeWithMessage> get nodes => _nodes;
Iterable<_NodeInfo> get nodes => _nodes;

final _nodeBreadcrumb = <String, AstNode>{};

_Visitor(List<_BanNameConfigEntry> entries)
: _entryMap = Map.fromEntries(entries.map((e) => MapEntry(e.ident, e)));
Expand Down Expand Up @@ -39,18 +41,51 @@ class _Visitor extends GeneralizingAstVisitor<void> {
}

void _visitIdent(AstNode node, String name) {
final prevNode =
_nodeBreadcrumb.isNotEmpty ? _nodeBreadcrumb.values.last : null;
if (node.offset - 1 == prevNode?.end) {
_nodeBreadcrumb.addAll({name: node});
} else {
_nodeBreadcrumb.clear();
}

if (_nodeBreadcrumb.isEmpty) {
_nodeBreadcrumb.addAll({name: node});
}

if (_entryMap.containsKey(name)) {
_nodes.add(_NodeWithMessage(
_nodes.add(_NodeInfo(
node,
'${_entryMap[name]!.description} ($name is banned)',
fullName: name,
message: '${_entryMap[name]!.description} ($name is banned)',
));

return;
}

final breadcrumbString = _nodeBreadcrumb.keys.join('.');
if (_entryMap.containsKey(breadcrumbString)) {
_nodes.add(_NodeInfo(
_nodeBreadcrumb.values.first,
fullName: breadcrumbString,
message:
'${_entryMap[breadcrumbString]!.description} ($breadcrumbString is banned)',
endNode: _nodeBreadcrumb.values.last,
));
}
}
}

class _NodeWithMessage {
class _NodeInfo {
final AstNode node;
final String fullName;
final String message;
final AstNode? endNode;

_NodeWithMessage(this.node, this.message);
_NodeInfo(
this.node, {
required this.fullName,
required this.message,
this.endNode,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ void main() {
{'ident': 'showDialog', 'description': 'Please use myShowDialog'},
{'ident': 'strangeName', 'description': 'The name is too strange'},
{'ident': 'AnotherStrangeName', 'description': 'Oops'},
{
'ident': 'StrangeClass.someMethod',
'description': 'Please use NonStrangeClass.someMethod instead',
},
{
'ident': 'DateTime.now',
'description': 'Please use clock.now instead',
},
],
}).check(unit);

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [6, 7, 9, 12, 15, 16],
startColumns: [3, 12, 7, 1, 1, 12],
startLines: [7, 8, 10, 13, 16, 17, 20, 23, 24, 26],
startColumns: [3, 12, 7, 1, 1, 12, 1, 1, 1, 28],
locationTexts: [
'showDialog',
'showDialog',
Expand All @@ -43,6 +51,10 @@ void main() {
' late var strangeName; // LINT\n'
'}',
'strangeName',
'StrangeClass.someMethod();',
'DateTime.now();',
'DateTime.now()',
'DateTime.now',
],
messages: [
'Please use myShowDialog (showDialog is banned)',
Expand All @@ -51,6 +63,10 @@ void main() {
'The name is too strange (strangeName is banned)',
'Oops (AnotherStrangeName is banned)',
'The name is too strange (strangeName is banned)',
'Please use NonStrangeClass.someMethod instead (StrangeClass.someMethod is banned)',
'Please use clock.now instead (DateTime.now is banned)',
'Please use clock.now instead (DateTime.now is banned)',
'Please use clock.now instead (DateTime.now is banned)',
],
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class StrangeClass {
void someMethod();
}

class NonStrangeClass {
void someMethod();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dialog.dart';
import 'dialog.dart' as material;
import 'classes.dart';

void func() {
myShowDialog('some_arguments', 'another_argument');
Expand All @@ -15,3 +16,13 @@ void strangeName() {} // 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 now = DateTime(2022);
21 changes: 21 additions & 0 deletions website/docs/rules/common/ban-name.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Example: When you add some extra functionalities to built-in Flutter functions (
:::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').

:::

Expand All @@ -30,12 +31,28 @@ void strangeName() {} // 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:**

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

### ⚙️ Config example {#config-example}
Expand All @@ -53,4 +70,8 @@ dart_code_metrics:
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
```