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
/
no_equal_arguments.dart
84 lines (67 loc) · 2.37 KB
/
no_equal_arguments.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
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:code_checker/rules.dart';
class NoEqualArguments extends Rule {
static const String ruleId = 'no-equal-arguments';
static const _documentationUrl = 'https://git.io/JUlBH';
static const _warningMessage = 'The argument has already been passed';
NoEqualArguments({Map<String, Object> config = const {}})
: super(
id: ruleId,
documentation: Uri.parse(_documentationUrl),
severity: Severity.fromJson(config['severity'] as String) ??
Severity.warning,
);
@override
Iterable<Issue> check(ProcessedFile file) {
final _visitor = _Visitor();
file.parsedContent.visitChildren(_visitor);
return _visitor.arguments
.map((argument) => createIssue(
this,
nodeLocation(argument, file),
_warningMessage,
null,
))
.toList(growable: false);
}
}
class _Visitor extends RecursiveAstVisitor<void> {
final _arguments = <Expression>[];
Iterable<Expression> get arguments => _arguments;
@override
void visitMethodInvocation(MethodInvocation node) {
super.visitMethodInvocation(node);
_visitArguments(node.argumentList.arguments);
}
@override
void visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
super.visitFunctionExpressionInvocation(node);
_visitArguments(node.argumentList.arguments);
}
void _visitArguments(NodeList<Expression> arguments) {
for (final argument in arguments) {
final lastAppearance = arguments.lastWhere((arg) {
if (argument is NamedExpression &&
arg is NamedExpression &&
argument.expression is! Literal &&
arg.expression is! Literal) {
return argument.expression.toString() == arg.expression.toString();
}
if (_bothLiterals(argument, arg)) {
return argument == arg;
}
return argument.toString() == arg.toString();
});
if (argument != lastAppearance) {
_arguments.add(lastAppearance);
}
}
}
bool _bothLiterals(Expression left, Expression right) =>
left is Literal && right is Literal ||
(left is PrefixExpression &&
left.operand is Literal &&
right is PrefixExpression &&
right.operand is Literal);
}