void visitConditionalExpression()

in lib/src/rules/prefer_null_aware_operators.dart [52:97]


  void visitConditionalExpression(ConditionalExpression node) {
    var condition = node.condition;
    if (condition is BinaryExpression &&
        (condition.operator.type == TokenType.EQ_EQ ||
            condition.operator.type == TokenType.BANG_EQ)) {
      // ensure pattern `xx == null ? null : yy` or `xx != null ? yy : null`
      if (condition.operator.type == TokenType.EQ_EQ) {
        if (node.thenExpression is! NullLiteral) {
          return;
        }
      } else {
        if (node.elseExpression is! NullLiteral) {
          return;
        }
      }

      // ensure condition is a null check
      Expression expression;
      if (condition.leftOperand is NullLiteral) {
        expression = condition.rightOperand;
      } else if (condition.rightOperand is NullLiteral) {
        expression = condition.leftOperand;
      } else {
        return;
      }

      Expression? exp = condition.operator.type == TokenType.EQ_EQ
          ? node.elseExpression
          : node.thenExpression;
      while (exp is PrefixedIdentifier ||
          exp is MethodInvocation ||
          exp is PropertyAccess) {
        if (exp is PrefixedIdentifier) {
          exp = exp.prefix;
        } else if (exp is MethodInvocation) {
          exp = exp.target;
        } else if (exp is PropertyAccess) {
          exp = exp.target;
        }
        if (exp.toString() == expression.toString()) {
          rule.reportLint(node);
          return;
        }
      }
    }
  }