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

Commit b25d4c2

Browse files
committed
chore: change calculation for ban-name chain calls
1 parent 5c06f67 commit b25d4c2

File tree

5 files changed

+59
-65
lines changed

5 files changed

+59
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* feat: add static code diagnostic [`arguments-ordering`](https://dartcodemetrics.dev/docs/rules/common/arguments-ordering).
6+
* feat: add method call chains support for [`ban-name`](https://dartcodemetrics.dev/docs/rules/common/ban-name).
67

78
## 5.0.1
89

lib/src/analyzers/lint_analyzer/rules/rules_list/ban_name/ban_name_rule.dart

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,11 @@ class BanNameRule extends CommonRule {
4242

4343
source.unit.visitChildren(visitor);
4444

45-
final filteredNodeList = visitor.nodes.where((node) {
46-
if (node.endNode != null) {
47-
final inlineNode = source.content
48-
.substring(
49-
node.node.offset,
50-
node.endNode!.end,
51-
)
52-
.replaceAll('\n', '');
53-
54-
if (!inlineNode.contains(node.fullName)) {
55-
return false;
56-
}
57-
}
58-
59-
return true;
60-
});
61-
62-
return filteredNodeList
45+
return visitor.nodes
6346
.map(
6447
(node) => createIssue(
6548
rule: this,
66-
location: nodeLocation(
67-
node: node.node,
68-
source: source,
69-
endNode: node.endNode,
70-
),
49+
location: nodeLocation(node: node.node, source: source),
7150
message: node.message,
7251
),
7352
)

lib/src/analyzers/lint_analyzer/rules/rules_list/ban_name/visitor.dart

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,92 @@ class _Visitor extends GeneralizingAstVisitor<void> {
77

88
Iterable<_NodeInfo> get nodes => _nodes;
99

10-
final _nodeBreadcrumb = <String, AstNode>{};
10+
final _visitedNodes = <AstNode>{};
1111

1212
_Visitor(List<_BanNameConfigEntry> entries)
1313
: _entryMap = Map.fromEntries(entries.map((e) => MapEntry(e.ident, e)));
1414

1515
@override
1616
void visitSimpleIdentifier(SimpleIdentifier node) {
17-
_visitIdent(node, node.name);
17+
_visitIdent(node, node);
1818
}
1919

2020
@override
2121
void visitPrefixedIdentifier(PrefixedIdentifier node) {
22-
_visitIdent(node, node.identifier.name);
23-
_visitIdent(node, node.prefix.name);
22+
_visitIdent(node, node.identifier);
23+
_visitIdent(node, node.prefix);
2424
}
2525

2626
@override
2727
void visitLibraryIdentifier(LibraryIdentifier node) {
2828
for (final component in node.components) {
29-
_visitIdent(node, component.name);
29+
_visitIdent(node, component);
3030
}
3131
}
3232

3333
@override
3434
void visitDeclaration(Declaration node) {
3535
final name = node.declaredElement?.displayName;
3636
if (name != null) {
37-
_visitIdent(node, name);
37+
_checkBannedName(node, name);
3838
}
3939

4040
super.visitDeclaration(node);
4141
}
4242

43-
void _visitIdent(AstNode node, String name) {
44-
final prevNode =
45-
_nodeBreadcrumb.isNotEmpty ? _nodeBreadcrumb.values.last : null;
46-
if (node.offset - 1 == prevNode?.end) {
47-
_nodeBreadcrumb.addAll({name: node});
48-
} else {
49-
_nodeBreadcrumb.clear();
43+
void _visitIdent(AstNode node, SimpleIdentifier name) {
44+
_checkBannedName(name, name.name);
45+
46+
if (node is PrefixedIdentifier) {
47+
_checkBannedName(node, node.name);
5048
}
5149

52-
if (_nodeBreadcrumb.isEmpty) {
53-
_nodeBreadcrumb.addAll({name: node});
50+
_traverseParents(name.parent);
51+
}
52+
53+
void _traverseParents(AstNode? node) {
54+
if (node is MethodInvocation) {
55+
_checkBannedName(
56+
node,
57+
'${node.realTarget}.${node.methodName}',
58+
);
59+
_traverseParents(node.parent);
5460
}
5561

56-
if (_entryMap.containsKey(name)) {
57-
_nodes.add(_NodeInfo(
62+
if (node is PropertyAccess) {
63+
_checkBannedName(
5864
node,
59-
fullName: name,
60-
message: '${_entryMap[name]!.description} ($name is banned)',
61-
));
65+
'${node.realTarget}.${node.propertyName}',
66+
);
67+
_traverseParents(node.realTarget);
68+
}
69+
70+
if (node is ConstructorName) {
71+
_traverseParents(node.parent);
72+
}
6273

63-
return;
74+
if (node is InstanceCreationExpression) {
75+
_checkBannedName(node, node.constructorName.toString());
6476
}
77+
}
6578

66-
final breadcrumbString = _nodeBreadcrumb.keys.join('.');
67-
if (_entryMap.containsKey(breadcrumbString)) {
79+
void _checkBannedName(AstNode node, String name) {
80+
if (_entryMap.containsKey(name) && !_visitedNodes.contains(node)) {
81+
_visitedNodes.add(node);
6882
_nodes.add(_NodeInfo(
69-
_nodeBreadcrumb.values.first,
70-
fullName: breadcrumbString,
71-
message:
72-
'${_entryMap[breadcrumbString]!.description} ($breadcrumbString is banned)',
73-
endNode: _nodeBreadcrumb.values.last,
83+
node,
84+
message: '${_entryMap[name]!.description} ($name is banned)',
7485
));
7586
}
7687
}
7788
}
7889

7990
class _NodeInfo {
8091
final AstNode node;
81-
final String fullName;
8292
final String message;
83-
final AstNode? endNode;
8493

8594
_NodeInfo(
8695
this.node, {
87-
required this.fullName,
8896
required this.message,
89-
this.endNode,
9097
});
9198
}

test/src/analyzers/lint_analyzer/rules/rules_list/ban_name/ban_name_rule_test.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void main() {
4040

4141
RuleTestHelper.verifyIssues(
4242
issues: issues,
43-
startLines: [7, 8, 10, 13, 16, 17, 20, 23, 24, 26],
44-
startColumns: [3, 12, 7, 1, 1, 12, 1, 1, 1, 28],
43+
startLines: [7, 8, 10, 13, 16, 17, 21, 24, 25, 26, 32],
44+
startColumns: [3, 12, 7, 1, 1, 12, 3, 3, 3, 3, 28],
4545
locationTexts: [
4646
'showDialog',
4747
'showDialog',
@@ -51,10 +51,11 @@ void main() {
5151
' late var strangeName; // LINT\n'
5252
'}',
5353
'strangeName',
54-
'StrangeClass.someMethod();',
55-
'DateTime.now();',
54+
'StrangeClass.someMethod()',
55+
'DateTime.now()',
56+
'DateTime.now()',
57+
'DateTime.now()',
5658
'DateTime.now()',
57-
'DateTime.now',
5859
],
5960
messages: [
6061
'Please use myShowDialog (showDialog is banned)',
@@ -67,6 +68,7 @@ void main() {
6768
'Please use clock.now instead (DateTime.now is banned)',
6869
'Please use clock.now instead (DateTime.now is banned)',
6970
'Please use clock.now instead (DateTime.now is banned)',
71+
'Please use clock.now instead (DateTime.now is banned)',
7072
],
7173
);
7274
});

test/src/analyzers/lint_analyzer/rules/rules_list/ban_name/examples/example.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ class AnotherStrangeName {
1717
late var strangeName; // LINT
1818
}
1919

20-
StrangeClass.someMethod(); // LINT
21-
NonStrangeClass.someMethod();
20+
void main() {
21+
StrangeClass.someMethod(); // LINT
22+
NonStrangeClass.someMethod();
23+
24+
DateTime.now(); // LINT
25+
DateTime.now().day; // LINT
26+
DateTime.now().day.isFinite; // LINT
27+
28+
DateTime now = DateTime(2022);
29+
}
2230

23-
DateTime.now(); // LINT
24-
DateTime.now().day; // LINT
2531
class DateTimeTest {
2632
final currentTimestamp = DateTime.now(); // LINT
2733
}
28-
DateTime now = DateTime(2022);

0 commit comments

Comments
 (0)