@@ -56,14 +56,6 @@ bool _isComparingParameterWithNull(BinaryExpression node, Element? parameter) =>
56
56
bool _isParameter (Expression expression, Element ? parameter) =>
57
57
expression.canonicalElement == parameter;
58
58
59
- bool _isParameterWithQuestion (AstNode node, Element ? parameter) =>
60
- (node is PropertyAccess &&
61
- node.operator .type == TokenType .QUESTION_PERIOD &&
62
- node.target.canonicalElement == parameter) ||
63
- (node is MethodInvocation &&
64
- node.operator ? .type == TokenType .QUESTION_PERIOD &&
65
- node.target.canonicalElement == parameter);
66
-
67
59
bool _isParameterWithQuestionQuestion (
68
60
BinaryExpression node, Element ? parameter) =>
69
61
node.operator .type == TokenType .QUESTION_QUESTION &&
@@ -86,6 +78,39 @@ class AvoidNullChecksInEqualityOperators extends LintRule {
86
78
}
87
79
}
88
80
81
+ class _BodyVisitor extends RecursiveAstVisitor {
82
+ final Element ? parameter;
83
+ final LintRule rule;
84
+ _BodyVisitor (this .parameter, this .rule);
85
+
86
+ @override
87
+ visitBinaryExpression (BinaryExpression node) {
88
+ if (_isParameterWithQuestionQuestion (node, parameter) ||
89
+ _isComparingParameterWithNull (node, parameter)) {
90
+ rule.reportLint (node);
91
+ }
92
+ super .visitBinaryExpression (node);
93
+ }
94
+
95
+ @override
96
+ visitMethodInvocation (MethodInvocation node) {
97
+ if (node.operator ? .type == TokenType .QUESTION_PERIOD &&
98
+ node.target.canonicalElement == parameter) {
99
+ rule.reportLint (node);
100
+ }
101
+ super .visitMethodInvocation (node);
102
+ }
103
+
104
+ @override
105
+ visitPropertyAccess (PropertyAccess node) {
106
+ if (node.operator .type == TokenType .QUESTION_PERIOD &&
107
+ node.target.canonicalElement == parameter) {
108
+ rule.reportLint (node);
109
+ }
110
+ super .visitPropertyAccess (node);
111
+ }
112
+ }
113
+
89
114
class _Visitor extends SimpleAstVisitor <void > {
90
115
final LintRule rule;
91
116
final bool nnbdEnabled;
@@ -113,15 +138,6 @@ class _Visitor extends SimpleAstVisitor<void> {
113
138
return ;
114
139
}
115
140
116
- bool checkIfParameterIsNull (AstNode node) =>
117
- _isParameterWithQuestion (node, parameter) ||
118
- (node is BinaryExpression &&
119
- (_isParameterWithQuestionQuestion (node, parameter) ||
120
- _isComparingParameterWithNull (node, parameter)));
121
-
122
- node.body
123
- .traverseNodesInDFS ()
124
- .where (checkIfParameterIsNull)
125
- .forEach (rule.reportLint);
141
+ node.body.accept (_BodyVisitor (parameter, rule));
126
142
}
127
143
}
0 commit comments