This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
visitor.dart
116 lines (92 loc) · 3.25 KB
/
visitor.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
part of 'prefer_moving_to_variable_rule.dart';
class _Visitor extends RecursiveAstVisitor<void> {
final _nodes = <AstNode>[];
Iterable<AstNode> get nodes => _nodes;
@override
void visitBlockFunctionBody(BlockFunctionBody node) {
final visitor = _BlockVisitor();
node.visitChildren(visitor);
_nodes.addAll(visitor.duplicates);
}
}
class _BlockVisitor extends RecursiveAstVisitor<void> {
final Map<String, AstNode> _visitedInvocations = {};
final Set<AstNode> _visitedNodes = {};
final Set<AstNode> _duplicates = {};
Set<AstNode> get duplicates => _duplicates;
_BlockVisitor();
@override
void visitPropertyAccess(PropertyAccess node) {
if (node.target == null) {
return;
}
final hasDuplicates = _checkForDuplicates(node, node.target);
if (!hasDuplicates) {
super.visitPropertyAccess(node);
}
}
@override
void visitMethodInvocation(MethodInvocation node) {
if (node.parent is CascadeExpression ||
(node.staticType?.isVoid ?? false)) {
return;
}
final hasDuplicates = _checkForDuplicates(node, node.target);
if (!hasDuplicates) {
super.visitMethodInvocation(node);
}
}
bool _checkForDuplicates(AstNode node, Expression? target) {
final access = node.toString();
final visitedInvocation = _visitedInvocations[access];
final isDuplicate =
visitedInvocation != null && _isDuplicate(visitedInvocation, node);
if (isDuplicate) {
_duplicates.addAll([visitedInvocation!, node]);
}
if (_visitedNodes.contains(node)) {
return isDuplicate;
}
_visitedInvocations[access] = node;
_visitAllTargets(target);
return false;
}
bool _isDuplicate(AstNode visitedInvocation, AstNode node) {
final visitedSwitch =
visitedInvocation.thisOrAncestorOfType<SwitchStatement>();
final visitedBlock = visitedInvocation.thisOrAncestorOfType<Block>();
final parentBlock = node.thisOrAncestorOfType<Block>();
final grandParentBlock = parentBlock?.thisOrAncestorMatching(
(block) => block is Block && block != parentBlock,
);
final visitedFunctionExpression = visitedInvocation.thisOrAncestorMatching(
(astNode) =>
astNode is FunctionExpression || astNode is FunctionDeclaration,
);
final parentFunctionExpression = node.thisOrAncestorMatching((astNode) =>
astNode is FunctionExpression || astNode is FunctionDeclaration);
return visitedInvocation != node &&
visitedSwitch == null &&
(visitedBlock == parentBlock || visitedBlock == grandParentBlock) &&
(visitedFunctionExpression == null &&
parentFunctionExpression == null ||
visitedFunctionExpression == parentFunctionExpression);
}
void _visitAllTargets(Expression? target) {
var realTarget = target;
while (realTarget != null) {
_visitedNodes.add(realTarget);
final access = realTarget.toString();
if (!_visitedInvocations.containsKey(access)) {
_visitedInvocations[access] = realTarget;
}
if (realTarget is MethodInvocation) {
realTarget = realTarget.realTarget;
} else if (realTarget is PropertyAccess) {
realTarget = realTarget.realTarget;
} else {
realTarget = null;
}
}
}
}