Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

migrate avoid_null_checks_in_equality_operators away from traverseNodesInDFS() #3714

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}