|
| 1 | +part of 'check_for_equals_in_render_object_setters_rule.dart'; |
| 2 | + |
| 3 | +class _Visitor extends GeneralizingAstVisitor<void> { |
| 4 | + final _declarations = <_DeclarationInfo>[]; |
| 5 | + |
| 6 | + Iterable<_DeclarationInfo> get declarations => _declarations; |
| 7 | + |
| 8 | + @override |
| 9 | + void visitClassDeclaration(ClassDeclaration node) { |
| 10 | + final classType = node.extendsClause?.superclass.type; |
| 11 | + if (!isRenderObjectOrSubclass(classType)) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + final settersVisitor = _SettersVisitor(); |
| 16 | + node.visitChildren(settersVisitor); |
| 17 | + |
| 18 | + _declarations.addAll(settersVisitor.declarations); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class _SettersVisitor extends GeneralizingAstVisitor<void> { |
| 23 | + final _declarations = <_DeclarationInfo>[]; |
| 24 | + |
| 25 | + Iterable<_DeclarationInfo> get declarations => _declarations; |
| 26 | + |
| 27 | + @override |
| 28 | + void visitMethodDeclaration(MethodDeclaration node) { |
| 29 | + if (!node.isSetter) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + final body = node.body; |
| 34 | + |
| 35 | + var notFirst = false; |
| 36 | + |
| 37 | + if (body is BlockFunctionBody) { |
| 38 | + for (final statement in body.block.statements) { |
| 39 | + if (statement is IfStatement) { |
| 40 | + if (notFirst) { |
| 41 | + _declarations.add(_DeclarationInfo( |
| 42 | + node, |
| 43 | + 'Equals check should come first in the block.', |
| 44 | + )); |
| 45 | + |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + final returnVisitor = _ReturnVisitor(); |
| 50 | + statement.visitChildren(returnVisitor); |
| 51 | + |
| 52 | + final condition = statement.condition; |
| 53 | + if (condition is BinaryExpression) { |
| 54 | + if (!(_usesParameter(condition.leftOperand) || |
| 55 | + _usesParameter(condition.rightOperand) && |
| 56 | + returnVisitor.isValid)) { |
| 57 | + _declarations.add(_DeclarationInfo( |
| 58 | + node, |
| 59 | + 'Equals check compares a wrong parameter or has no return statement.', |
| 60 | + )); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (statement is! AssertStatement && |
| 68 | + statement is! VariableDeclarationStatement) { |
| 69 | + notFirst = true; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (notFirst) { |
| 75 | + _declarations.add(_DeclarationInfo( |
| 76 | + node, |
| 77 | + 'Equals check is missing.', |
| 78 | + )); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + bool _usesParameter(Expression expression) => |
| 83 | + expression is SimpleIdentifier && |
| 84 | + expression.staticElement is ParameterElement; |
| 85 | +} |
| 86 | + |
| 87 | +class _ReturnVisitor extends RecursiveAstVisitor<void> { |
| 88 | + bool _hasValidReturn = false; |
| 89 | + |
| 90 | + bool _hasValidAssignment = false; |
| 91 | + |
| 92 | + bool get isValid => _hasValidReturn || _hasValidAssignment; |
| 93 | + |
| 94 | + @override |
| 95 | + void visitReturnStatement(ReturnStatement node) { |
| 96 | + super.visitReturnStatement(node); |
| 97 | + |
| 98 | + final type = node.expression?.staticType; |
| 99 | + |
| 100 | + if (type == null || type.isVoid) { |
| 101 | + _hasValidReturn = true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @override |
| 106 | + void visitAssignmentExpression(AssignmentExpression node) { |
| 107 | + super.visitAssignmentExpression(node); |
| 108 | + |
| 109 | + _hasValidAssignment = true; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +class _DeclarationInfo { |
| 114 | + final Declaration node; |
| 115 | + final String errorMessage; |
| 116 | + |
| 117 | + const _DeclarationInfo(this.node, this.errorMessage); |
| 118 | +} |
0 commit comments