Skip to content

Commit

Permalink
migrate away from traverseNodesInDFS() (dart-lang/linter#3714)
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored Sep 22, 2022
1 parent c022e03 commit 7150806
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions lib/src/rules/avoid_null_checks_in_equality_operators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ bool _isComparingParameterWithNull(BinaryExpression node, Element? parameter) =>
bool _isParameter(Expression expression, Element? parameter) =>
expression.canonicalElement == parameter;

bool _isParameterWithQuestion(AstNode node, Element? parameter) =>
(node is PropertyAccess &&
node.operator.type == TokenType.QUESTION_PERIOD &&
node.target.canonicalElement == parameter) ||
(node is MethodInvocation &&
node.operator?.type == TokenType.QUESTION_PERIOD &&
node.target.canonicalElement == parameter);

bool _isParameterWithQuestionQuestion(
BinaryExpression node, Element? parameter) =>
node.operator.type == TokenType.QUESTION_QUESTION &&
Expand All @@ -86,6 +78,39 @@ class AvoidNullChecksInEqualityOperators extends LintRule {
}
}

class _BodyVisitor extends RecursiveAstVisitor {
final Element? parameter;
final LintRule rule;
_BodyVisitor(this.parameter, this.rule);

@override
visitBinaryExpression(BinaryExpression node) {
if (_isParameterWithQuestionQuestion(node, parameter) ||
_isComparingParameterWithNull(node, parameter)) {
rule.reportLint(node);
}
super.visitBinaryExpression(node);
}

@override
visitMethodInvocation(MethodInvocation node) {
if (node.operator?.type == TokenType.QUESTION_PERIOD &&
node.target.canonicalElement == parameter) {
rule.reportLint(node);
}
super.visitMethodInvocation(node);
}

@override
visitPropertyAccess(PropertyAccess node) {
if (node.operator.type == TokenType.QUESTION_PERIOD &&
node.target.canonicalElement == parameter) {
rule.reportLint(node);
}
super.visitPropertyAccess(node);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
final bool nnbdEnabled;
Expand Down Expand Up @@ -113,15 +138,6 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}

bool checkIfParameterIsNull(AstNode node) =>
_isParameterWithQuestion(node, parameter) ||
(node is BinaryExpression &&
(_isParameterWithQuestionQuestion(node, parameter) ||
_isComparingParameterWithNull(node, parameter)));

node.body
.traverseNodesInDFS()
.where(checkIfParameterIsNull)
.forEach(rule.reportLint);
node.body.accept(_BodyVisitor(parameter, rule));
}
}

0 comments on commit 7150806

Please sign in to comment.