From 7150806c33b601f47f6aa2ef0422d099ec4af26c Mon Sep 17 00:00:00 2001 From: Phil Quitslund Date: Thu, 22 Sep 2022 14:49:36 -0700 Subject: [PATCH] migrate away from `traverseNodesInDFS()` (dart-lang/linter#3714) --- ...oid_null_checks_in_equality_operators.dart | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/src/rules/avoid_null_checks_in_equality_operators.dart b/lib/src/rules/avoid_null_checks_in_equality_operators.dart index 31a930d7ccf1..bcb748ecc790 100644 --- a/lib/src/rules/avoid_null_checks_in_equality_operators.dart +++ b/lib/src/rules/avoid_null_checks_in_equality_operators.dart @@ -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 && @@ -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 { final LintRule rule; final bool nnbdEnabled; @@ -113,15 +138,6 @@ class _Visitor extends SimpleAstVisitor { 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)); } }