|
| 1 | +part of 'list_all_equatable_fields_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 | + |
| 12 | + final isEquatable = _isEquatableOrSubclass(classType); |
| 13 | + final isMixin = node.withClause?.mixinTypes |
| 14 | + .any((mixinType) => _isEquatableMixin(mixinType.type)) ?? |
| 15 | + false; |
| 16 | + final isSubclassOfMixin = _isSubclassOfEquatableMixin(classType); |
| 17 | + if (!isEquatable && !isMixin && !isSubclassOfMixin) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + final fieldNames = node.members |
| 22 | + .whereType<FieldDeclaration>() |
| 23 | + .whereNot((field) => field.isStatic) |
| 24 | + .map((declaration) => |
| 25 | + declaration.fields.variables.firstOrNull?.name.lexeme) |
| 26 | + .whereNotNull() |
| 27 | + .toSet(); |
| 28 | + |
| 29 | + if (isMixin) { |
| 30 | + fieldNames.addAll(_getParentFields(classType)); |
| 31 | + } |
| 32 | + |
| 33 | + final props = node.members.firstWhereOrNull((declaration) => |
| 34 | + declaration is MethodDeclaration && |
| 35 | + declaration.isGetter && |
| 36 | + declaration.name.lexeme == 'props') as MethodDeclaration?; |
| 37 | + |
| 38 | + if (props == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + final literalVisitor = _ListLiteralVisitor(); |
| 43 | + props.body.visitChildren(literalVisitor); |
| 44 | + |
| 45 | + final expression = literalVisitor.literals.firstOrNull; |
| 46 | + if (expression != null) { |
| 47 | + final usedFields = expression.elements |
| 48 | + .whereType<SimpleIdentifier>() |
| 49 | + .map((identifier) => identifier.name) |
| 50 | + .toSet(); |
| 51 | + |
| 52 | + if (!usedFields.containsAll(fieldNames)) { |
| 53 | + final missingFields = fieldNames.difference(usedFields).join(', '); |
| 54 | + _declarations.add(_DeclarationInfo( |
| 55 | + props, |
| 56 | + 'Missing declared class fields: $missingFields', |
| 57 | + )); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + Set<String> _getParentFields(DartType? classType) { |
| 63 | + // ignore: deprecated_member_use |
| 64 | + final element = classType?.element2; |
| 65 | + if (element is! ClassElement) { |
| 66 | + return {}; |
| 67 | + } |
| 68 | + |
| 69 | + return element.fields |
| 70 | + .where( |
| 71 | + (field) => |
| 72 | + !field.isStatic && |
| 73 | + !field.isConst && |
| 74 | + !field.isPrivate && |
| 75 | + field.name != 'hashCode', |
| 76 | + ) |
| 77 | + .map((field) => field.name) |
| 78 | + .toSet(); |
| 79 | + } |
| 80 | + |
| 81 | + bool _isEquatableOrSubclass(DartType? type) => |
| 82 | + _isEquatable(type) || _isSubclassOfEquatable(type); |
| 83 | + |
| 84 | + bool _isSubclassOfEquatable(DartType? type) => |
| 85 | + type is InterfaceType && type.allSupertypes.any(_isEquatable); |
| 86 | + |
| 87 | + bool _isEquatable(DartType? type) => |
| 88 | + type?.getDisplayString(withNullability: false) == 'Equatable'; |
| 89 | + |
| 90 | + bool _isEquatableMixin(DartType? type) => |
| 91 | + // ignore: deprecated_member_use |
| 92 | + type?.element2 is MixinElement && |
| 93 | + type?.getDisplayString(withNullability: false) == 'EquatableMixin'; |
| 94 | + |
| 95 | + bool _isSubclassOfEquatableMixin(DartType? type) { |
| 96 | + // ignore: deprecated_member_use |
| 97 | + final element = type?.element2; |
| 98 | + |
| 99 | + return element is ClassElement && element.mixins.any(_isEquatableMixin); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class _ListLiteralVisitor extends RecursiveAstVisitor<void> { |
| 104 | + final literals = <ListLiteral>{}; |
| 105 | + |
| 106 | + @override |
| 107 | + void visitListLiteral(ListLiteral node) { |
| 108 | + super.visitListLiteral(node); |
| 109 | + |
| 110 | + literals.add(node); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class _DeclarationInfo { |
| 115 | + final Declaration node; |
| 116 | + final String errorMessage; |
| 117 | + |
| 118 | + const _DeclarationInfo(this.node, this.errorMessage); |
| 119 | +} |
0 commit comments